Home » excel » how to check whether Excel window has focus from vba

how to check whether Excel window has focus from vba

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have some excel vba macros that can take minutes to an hour to run. At the end of the run they do a bunch of sorting. The problem is that if Excel doesn’t have the focus (iow, I’m doing something in another window), it has a problem and I have to close excel and reopen it. I’m not sure what the problem is but I know it happens when it tries to run Sort.Apply. Is there anyway I can check whether excel has the focus, and if not, either give it the focus or popup a checkbox?

Update: Here is the code. I just tested it, and it fails. The error is “Method ‘Apply’ of object ‘Sort’ failed”, “Run-time error ‘-2147417848 (80010108)’:”

    With rws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("A7:A" & i _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("C7:C" & i _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("A6:K" & i)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
        .SortFields.Clear
    End With

i is a variable that has the bottom of the sort range, which is gotten either from a number in the sheet or just before the sort.

I don’t think I get it every single time, but I am not sure how to prevent it other than staying in focus. The way I do it now is have a message box alert me that it is sorting, however I am trying to make this fully automated, so I am going to need to do something else.

Update: Here is my updated code.

    With rws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=rws.Range("A7:A" & i _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=rws.Range("C7:C" & i _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange rws.Range("A6:K" & i)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
        .SortFields.Clear
    End With
How to&Answers:

You are getting that error because you have not specified your ranges properly.

Add the sheet name before Range("A7:A" & i) for example

ws.Range("A7:A" & i) 

where ws is is the relevant sheet where the sorting needs to be done. This way you don’t need to activate the window 🙂

EDIT

If you have several Excel windows open and if you want to sort a sheet in the Excel file which has the code then remember to fully qualify the sheet object as well. For example

Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet1")

Answer:

I’ve run into this several times myself and I can’t remember exactly how I handled it. Try changing .SetRange rws.Range("A6:K" & i) to .SetRange Range("A6:K" & i) and leave the rest of the code as is. I’m not sure that you have to qualify Range again with the worksheet object. If this doesn’t do the trick, I’ll check some code I have written at work tomorrow.

EDIT:
@RBarryYoung – You’re right I was going in the wrong direction on this. I don’t think the suggestion above will work.

My only other suggestion would be to run the workbook in a separate instance. For example:

Dim objXL as New Excel.Application
With objxl
.Workbooks.Open("C:\Someworkbook.xlsx")
.Visible = True ''or false
''Your Code Here
.ThisWorkbook.SaveAs("C:\Someworkbook.xlsx")
.Quit
End With

You will have to qualify all of your code between the With objXL and End With (i.e. cells(1,1) will become .cells(1,1), ThisWorkbook will become .ThisWorkbook). Kind of a pain if you have already written a lot of code but this should force Excel to only look at the instance you are running the script in, preventing lost focus.