VBA ActiveWindow problem

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
691
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?)
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,327
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
You could try ActiveWorkbook.Windows(1) instead of ActiveWindow?
 

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
691
rorya-

Still getting Unable to get the Top property of the Range class.
 

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
691
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?
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
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
 

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
691
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...
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
On your form's proprties set the "Modal" property to FALSE, this will let the form operate when it does not have the focus.
 

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
691
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...
 

Forum statistics

Threads
1,081,793
Messages
5,361,325
Members
400,627
Latest member
Mcomeaux

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top