Activate workbook

fddekker

Board Regular
Joined
Jun 30, 2008
Messages
63
What is the correct syntax to activate an open workbook?

This is the scenario:

I am send a file
I obtain the file name with:

Dim vFileNames As Variant
vFileNames = Application.GetOpenFilename

While this file is open, I open another file (a static file that will always have the same name), copy information from this second file and then paste it into the original file.

How do I reactivate the original file.
(The macro recorder comes up with Windows(XXXX.XLS).activate, which will not work for variants in the file names.

After pasting the information, the static file is closed.

I tried
Workbook.vFilename.Activate
 

fddekker

Board Regular
Joined
Jun 30, 2008
Messages
63
Still bombs out at Workbooks(vFilename).Activate

Sub AssignNumber()


Dim vFileNames As String
vFileNames = ThisWorkbook.Name

Application.ScreenUpdating = False
Workbooks.Open Filename:="C:\Excel test Folder\Counter.xls"
Range("B4").Select
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("B2").Select
Selection.Copy
Workbooks(vFilename).Activate
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("counter.xls").Activate
ActiveWorkbook.Save
ActiveWindow.Close
Range("C4").Select
End Sub
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
Try:

Dim vFileNames As Object
Set vFileNames = ActiveWorkbook

'Other code here!

vFileNames.Select
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
Sub isWBNowOpen()
'Standard module code, like: Module1.
Dim wb&
Dim myWB As Boolean
Dim myWorkbookName$

myWorkbookName = "AAA.xls"
myWB = False

For wb = 1 To Workbooks.Count
If Workbooks(wb).Name = myWorkbookName Then myWB = True
Next wb

If myWB = False Then Workbooks.Open (myWorkbookName)
Windows(myWorkbookName).Activate

'Now what ever Workbook name is in "myWorkbookName" will be active and ready to work with.
End Sub
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
Why not create references to the workbooks?

For example:
Code:
Set wbThis = ThisWorkboook ' create reference to workbook the code is in
Code:
Set wbAct  = ActiveWorkbook ' create reference to the active workbook
Code:
Set wbOpen = Workbooks.Open("C:\MyPath\MyBook.xls") ' open workbook and create a reference to it
Code:
Set wbNew = Workbooks.Add ' create new workbook and a reference to it
 

fddekker

Board Regular
Joined
Jun 30, 2008
Messages
63
Sorry, my mistake,

Workbooks(vFileNames).Activate (with the s) ... works perfectly

Thanks for all the suggestions. I will try them as well.
 

Forum statistics

Threads
1,082,342
Messages
5,364,783
Members
400,815
Latest member
gangstar67

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