• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

ThisWorkbook.Activate does not WORK

drom

New Member
Hi and thanks in advance!
Code:
Dim WKBactive As Workbook:                  Set WKBactive = ActiveWorkbook
  Dim wWKBactive As String:                     wWKBactive = WKBactive.name   'pr     wWKBactive  = ActiveWorkbook .name
Dim WKBvba As Workbook:                     Set WKBvba = ThisWorkbook
  Dim wWKBvba As String:                        wWKBvba = ThisWorkbook.Name 'or     wWKBvba =  WKBvba .name
Dim WKBnew As Workbook:                     Set WKBnew = ActiveWorkbook
  Dim wWKBnew As String:                        wWKBnew = WKBnew.Name        'or     wWKBnew =  ActiveWorkbook.name

  'For SURE WKBvba and WKBnew are different'
  'For sure when running my Macros... WKBnew is the Active workbook,  I am opening this File from my PC'
  'After doing so many thing thought my Macros using the  wKBnew File... '

I would Like to ACTIVATE ThisWorkbook FILE but I CAN'T

I am using so many Code to do something I think should be easy, BUT I do not why does not WORK.


Code:
                                              ThisWorkbook.Activate
                                              WKBvba.Activate
                                            Do Until ActiveWorkbook.Name = ThisWorkbook.Name
                                              ThisWorkbook.Activate
                                            Loop
                                         
                                            Application.ScreenUpdating = True
                                            Application.Wait Now() + TimeValue("00:00:04")

If ActiveWorkbook.Name = wWKBvba Then    wWKBactive = ThisWorkbook.Name

                                            Err.Clear
                                            ThisWorkbook.Activate
                                            ThisWorkbook.Activate
                                            Workbooks(wWKBvba ).Activate
                                            Windows(wWKBvba).Activate

                        
                                            Do Until (wWKBactive = wWKBvba And Err = 0)
                                              ThisWorkbook.Activate
                                              Workbooks(wWKBvba).Activate
                                              Windows(wWKBvba).Activate
                                              wWKBactive ="" :   wWKBactive = ThisWorkbook.name
                                            Loop

Nothing works, I am not able to make ThisWorkbook the ACTIVE workbook!!!

ANY HELP
thisworkbook.activate should work but doesn't
 
drom
How many ThisWorkbook.Activate s do You have in that code?
How do You know ... when ThisWorkbook is activated and not?
What is value of ThisWorkbook after every step?
 
I am using many ThisWorkbook.Activate because does not work
How do You know ... when ThisWorkbook is activated and not? Using Debug.print. Activeworkbook.name
What is value of ThisWorkbook after every step? always the same, wWKBnew

When using F8 step by step works fine, otherwise NO
I know Thisworkbook.activate should work, so I do not need anything else, but does not work
 
Your code is very hard to read due to similar variable names, the use of upper and lower case "w" right next to each other. Also, using a variable called "WKBactive" is not really helpful, as it's defined as an object that was active at time of execution, not the one that's always active.

I'm not even sure why you want to keep activating different things. In VB, you can access objects directly w/o flipping back and forth.
 
I'm not even sure why you want to keep activating different things ( I do not want ). In VB, you can access objects directly w/o flipping back and forth. (yes but in my model, does not work, Thisworkbook.Activate does not activate the book containing the macros)
I am using different techniques because, Thisworkbook.activate Does not work
Actually if Thisworkbook.activate works I can get rid of everything else, but unfortunately at present I don't know what to do

WKB = Workbook
wWKB = string name of the WKB (w for word)

actually my main concern i I do not understand why I am not able to activate Thisworkbook
 
drom
Using Debug.print. Activeworkbook.name to check ThisWorkbook.Name ...
Of course those, could be same too.
Why You cannot activate any already known workbook?
ThisWorkbook is always ThisWorkbook... not necessary any fixed.
 
drom
Using Debug.print. Activeworkbook.name to check ThisWorkbook.Name ...
Of course those, could be same too.
Why You cannot activate any already known workbook?
ThisWorkbook is always ThisWorkbook... not necessary any fixed.


Why You cannot activate any already known workbook?
Í do not know, this is why I am asking.
Because Ií do not undetstand why
ThisWorkbook.Activate dos not activate the desired book.


Anyway I AM going to pass some variables (Ranges from ThisWorkbook ) and this way Í do not need to activate ThisWorkbook.


But I still do not know Why I can't.
I should (yes, but I cant)
 
drom
Do You know
... what are You doing?
... what are You 'activating'?
... what sheet & workbook You would like to activate?
Your code loops loops ... activates activates.

Your: ... ThisWorkbook.Activate dos not activate the desired book ...
Why You do not do the_desire_book.activate? ... if You know - what is 'the desire book'.

Could You sit down and think?
... You are in loop Yourself!
 
drom
Do You know
... what are You doing?
... what are You 'activating'?
... what sheet & workbook You would like to activate?
Your code loops loops ... activates activates.

Your: ... ThisWorkbook.Activate dos not activate the desired book ...
Why You do not do the_desire_book.activate? ... if You know - what is 'the desire book'.

Could You sit down and think?
... You are in loop Yourself!

If I have 2 books
A = ThisWorkbok
B = other book = ActiveWorkBook
I am trying to activate the book A using:

ThisWorkBook.Activate
But does not work
Should be working? yes
Why doesn't? I do not know.

And this is why I am asking

To your questions: yes to all of them.
PS:
I know should be working (yes)
But since does not work...

PS:
I am not trying to be bold
My loopings and my many activates is because I don't get the desired task done all along
I could use
Code:
For x = 1 to 50
ThisWorkBook.Activate
Next X
But does not work in my book
 
Last edited by a moderator:
drom
... two workbooks ..
Code:
Workbooks(A).Activate
Workbooks(B).Activate
... and why need to activate?
Workbooks(A).Sheets(AA).Range("A1") = "AAA"
... or You could use
Code:
With Workbooks(A).Sheets(AA) 
' code 
End With
Your sample code is 'looping' ... hmm?
 
Hi !​
ThisWorkbook refers to the workbook which contains the VBA code …​
You are right. It is not clear what OPs code is doing, so I assumed everything is happening in one file, in which case ThisWorkbook = ActiveWorkbook
 
r2c2
Ref #9 ... If I have 2 books ... means at least there are two files.
... and why should use ThisWorkbook & ActiveWorkbook at all?
 
I am having the same problem as OP and I will try to explain it a different way. I have some code which does some intensive work, and so hides Excel while completing it (application.visible = false) and shows a little progress window.

One day, I found out that for some users, after Excel re-appears, it doesn't go back to the main workbook, it reverts to one of their other open ones instead, so they need to click back in to the main workbook. A minor annoyance, which should be easily fixed! I thought simply adding "thisworkbook.activate" to the end will ensure the workbook containing the macro is activated at the end of the script.

Code:
application.visible = false
''My code, which works fine
application.visible = true

thisworkbook.activate

I test this by stepping through, it works as expected. I run it all the through, it does not work.

I try various combinations. Except one, all these work when stepping through, but stop working when it is run through. They also work when I run all the way through and break on "thisworkbook.activate", and step through just that line.

Below are some of the examples I've tried. I've also tried as many combinations including "DoEvents" and ".EnableEvents = True" at different points. The key thing seems to be that a break on thisworkbook.activate works as expected, but it does not work without a break.

Has anyone seen something similar, or have any other ways I can try to force ThisWorkbook to the front?


Code:
' 1. Does not work, .activate appears to have to be after .visible

application.visible = false
''My code, which works fine

thisworkbook.activate
application.visible = true

Code:
'2
application.visible = false
''My code, which works fine

application.visible = true
thisworkbook.activate
application.visible = true

Code:
'3
application.visible = false
''My code, which works fine

application.visible = true
application.wait (Now + timevalue("00:00:01")
thisworkbook.activate
application.visible = true

Code:
'4
application.visible = false
''My code, which works fine

application.visible = true
doevents
thisworkbook.activate
doevents
application.visible = true

Code:
'4
application.visible = false
''My code, which works fine

application.visible = true
DoEvents
thisworkbook.activate
msgbox "Report Complete."
thisworkbook.activate
DoEvents
application.visible = true
 
Back
Top