VBA ActiveWindow problem

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
699
I'm using this code to update a userform (this is code that is part of a progress bar):
Code:
Private Sub UserForm_Activate()
Dim lTop As Long, lLeft As Long
Dim lRow As Long, lCol As Long
    With ActiveWindow.VisibleRange
        lRow = .Rows.Count / 2
        lCol = .Columns.Count / 2
    End With
    With Cells(lRow, lCol)
        lTop = .Top
        lLeft = .Left
    End With
    With Me
        .Top = lTop
        .Left = lLeft
    End With
End Sub

Is there a way to do the same thing without having to focus on the activewindow (if I go to surf the net while this script is called in one of my subroutines I get an error---I'm assuming excel can't find the activewindow?)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You could try ActiveWorkbook.Windows(1) instead of ActiveWindow?
 
Upvote 0
Apologies if this is simple....but I cannot figure this out.

If my routine looses focus of my active window which apparentley is my userform named frame_progress

How can I rewrite so that I don't receive this error when outside of excel while it runs?
 
Upvote 0
Hi,

I can not reprocude your problem.
While Excel is not active, the code runs without any problem.
There could be another reason. Feel free to email me your workbook - delete all irrelevant parts - so I can test it.

This is how I tested.
Run "test" then got to another programme before "test2" is launched by the OnTime.
Code:
Sub test()
Application.OnTime Now + 5 / 24 / 60 / 60, "test2"
End Sub
Sub test2()
UserForm1.Show
End Sub
kind regards,
Erik
 
Upvote 0
Odd-

The workbook I was preparing to send to you I was also unable to reproduce and this is just copying pasting code and form...

Alas, that means it lies somewhere other than what is apparent.

I cannot seem to remove enough to make it so that I would be allowed to send to you...
 
Upvote 0
On your form's proprties set the "Modal" property to FALSE, this will let the form operate when it does not have the focus.
 
Upvote 0
Rich (BB code):
Private Sub UserForm_Activate()
Dim lTop As Long, lLeft As Long
Dim lRow As Long, lCol As Long
    With ActiveWindow.VisibleRange
        lRow = .Rows.Count / 2
        lCol = .Columns.Count / 2
    End With
    With Cells(lRow, lCol)
        lTop = .Top
        lLeft = .Left
    End With
    With Me
        .Top = lTop
        .Left = lLeft
    End With
 
End Sub
Run-Time error '1004':
Unable to get the Top property of the Range class

Changing modal = False did not resolve for whatever reason

The odd thing is that when I switch back over to excel and even select the active window then try to resume the code (via the VBA screen), the code still errors out so I must start the routines over. Perhaps this is because when I select the VBA screen it deselects the active window the routine is looking for?

I am puzzled...
 
Upvote 0

Forum statistics

Threads
1,213,562
Messages
6,114,326
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top