Thisworkbook.activate does not work

drom

Well-known Member
Joined
Mar 20, 2005
Messages
543
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and thanks in advance!

I have the following code:
Code:
Sub AAA()
On Error Resume Next:                       Application.ScreenUpdating = True
                                            'ActiveWindow.Visible = True
  Dim wActWkb As String:                    wActWkb = ActiveWorkbook.Name
  Dim wThisWkb As String:                   wThisWkb = ThisWorkbook.Name
                                            'Application.ScreenUpdating = True
                                            '  Workbooks(wThisWkb).Activate
                                            '  Windows(wThisWkb).Activate
                                            '  ThisWorkbook.Activate
                                            'Application.ScreenUpdating = False
    Application.Wait Now() + TimeValue("00:00:01")
    Debug.Print wThisWkb, ActiveWorkbook.Name, WKBnew.Name, ActiveWorkbook.Name
    'I get:
    'AAA.xlsm            AAA.xlsm            BBB.xlsm            AAA.xlsm
    If wThisWkb <> ActiveWorkbook.Name Or WKBnew.Name = ActiveWorkbook.Name Then
      ThisWorkbook.Activate
    End If
      X = X
    Debug.Print wThisWkb, ActiveWorkbook.Name, WKBnew.Name, ActiveWorkbook.Name   'XXXXXXX
    'I get:
    'AAA.xlsm            AAA.xlsm            BBB.xlsm            AAA.xlsm
    'BUT If I put a Stop within the row: 'XXXXXXX and I press F8 step byt Step I get
    'AAA.xlsm            BBB.xlsm            BBB.xlsm            BBB.xlsm

What's going ON???

I am trying to Activate the book ; thisworkbook, but any of the following codes work:
Code:
Sub AAA()
  Dim wActWkb As String:                    wActWkb = ActiveWorkbook.Name
  Dim wThisWkb As String:                   wThisWkb = ThisWorkbook.Name                                          
                                            Application.ScreenUpdating = True
                                              Workbooks(wThisWkb).Activate
                                              Windows(wThisWkb).Activate
                                              ThisWorkbook.Activate
end sub

No matter If I use:
Code:
Sub AAA()
  Dim ActWkb As Workbook:                    set ActWkb = ActiveWorkbook.Name
  Dim ThisWkb As Workbook:                   set ThisWkb = ThisWorkbook.Name                                          
                                            Application.ScreenUpdating = True
                                              ThisWkb.Activate
                                              Windows(ThisWkb.name).Activate
                                              Windows(ThisWkb.fullname).Activate
                                              ThisWorkbook.Activate

end sub

I only get the desired action when using F8 inside my VBA code
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
As above what =ODIN= said, but also what is WKBnew supposed to be as its not defined or set to anything?
 
Upvote 0
Sorry, please can you be more specific as to the line of code that is the problem?

I have two books open
the Activeworkbook and thisworkbook

but I only can activate thisworkbook using F8 within the VBA window

I know how to use Scrreupdating, application.wait...

but I can't get thisworkbook.activate and do not know why

you can use any of the macros attached,

what's going on is explained in the comments lines see 'I get:

Thanks!
 
Last edited:
Upvote 0
Hi sorry,
I am using 2 different books, WKBnew = ActWKB and ThisWorkbook , but for this exanple I was modifying the code, to make it easier

wActWkb or wWKBnew can be the same or

ActWkb =
WKBnew
 
Last edited:
Upvote 0
Your code is written in a very confusing way, but

try maybe this i what you mean.
Code:
 Dim ActWkb As Workbook:                    set ActWkb = ActiveWorkbook
  Dim ThisWkb As Workbook:                   set ThisWkb = ThisWorkbook
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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