Activating an inactive workbook when the name is not known - vba

Is there a way to make an open (but inactive) workbook active when the name of the workbook is not known? I'd like to build a macro in one workbook that when run brings up another open workbook and then executes the rest of the macro on that workbook.
Also, is there a way to select from among several open, inactive workbooks if the user has more than one open?
Thanks in advance!

You can loop through all workbooks in the application's workbooks collection:
var wb as workbook
For Each wb In Application.Workbooks
If instr(1, wb.name, "hi") Then --activate a workbook if it's name has "hi" in it
wb.activate
End If
'or
If wb.name <> ThisWorkbook.name Then --find the first workbook that isn't this workbook
wb.activate
End If
Next
Basically, inside the loop you can test each workbook found (stored in the wb variable) and activate it depending on whatever property you want to test. This is about as specific as I can answer since you don't describe how you, as a human, would identify which workbook should be activated. You only say that you can't identify it by it's name.

Related

Tranfser data between two closed workbooks without creating a new one

how do I tranfser data between two closed workbooks without creating a new one? I have the following scenario:
2 excel workbooks(wb1 and wb2) with different structures
I need certain data from wb1 to be transfered to wb2
I can't modify wb1 & wb2 with macros
My question: Is it possible to activate a macro from a seperate excel workbook - lets name it wb3? So, triggering the macro in wb3 would transfer all the relevant data from wb1 to wb2....how would a macro like this look like?
Thanks!
Sub CommandButton1_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Application.workbooks.open("C:\Users\PlutoX\MyExcelfile1.xlsx")
Set wb2 = Application.workbooks.open("C:\Users\PlutoX\MyExcelfile1.xlsx")
wb2.Sheets("Sheet1").Range("A1") = wb1.Sheets("Sheet1").Range("A1")
wb1.Close False
wb2.Close True
End Sub
Almost there! You need to change three things:
If you want to open a workbook, you need to state the full path
Set wb1 = Application.workbooks.open("C:\Users\PlutoX\MyExcelfile1.xlsx")
If stuff needs to be transferred to wb2, the order is wb2 = wb1 so
wb2.sheets("Sheet1").Range("A1").value = wb1.sheets("Sheet1").Range("A1").value
If you close the workbooks, make sure you save at least the one with changes
wb2.Close True

Copy and combine sheets to a workbook

I need VBA code for Excel which: will be activated by a button in an empty workbook, loop through open workbooks, copies only sheets called "specificsheetname" from workbooks and pastes it into a new worksheet in the button activator workbook. So idea is that it will combine many worksheets from different workbooks into a one workbook. I tried this:
Sub workbookFetcher()
Dim book As Workbook, sheet, wsNew, wsCurr As Worksheet
Set wsCurr = ActiveSheet
For Each book In Workbooks
For Each sheet In book.Worksheets
If sheet.Name = "COOLING_RAW" Then
Set wsNew = Sheets.Add(After:=wsCurr)
book.Worksheets("COOLING_RAW").Copy
Set wsNew = book.Worksheets("COOLING_RAW")
End If
Next sheet
Next book
End Sub
It kind of works but it pastes all the copied worksheets to a new workbook. That's not what I want, I want them to pasted in the same workbook.
As I said in my comment:
Sub workbookFetcher()
Dim book As Workbook, sheet as Worksheet
For Each book In Workbooks
For Each sheet In book.Worksheets
If sheet.Name = "COOLING_RAW" Then
book.Worksheets("COOLING_RAW").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
End If
Next sheet
Next book
End Sub
If you want it to be after the ActiveSheet and the ActiveSheet is in the middle of other sheets, you can still use your wsCurr and just increment the index.
If you've got Excel 2016, then the newly bundled PowerQuery functionality under the Get & Transform part of the ribbon is by far the best way to do this. Suggest you google something like PowerQuery Combine Workbooks and you'll see heaps of great tutorials showing you exactly what to do. It pretty much makes lots of VBA redundant, and it is childs-play to learn compared to VBA.
If you've got any other version of Excel from 2010 up and have admin rights on your machine, you can download and install PowerQuery from Microsoft's site...it's a free add-in
you don't need to iterate through each single workbook worksheets, while you just try to get the wanted sheet and copy it if it actually exists
moreover you want to avoid searching ThisWorkbook itsel for wanted worksheet, too!
Option Explicit
Sub workbookFetcher()
Dim book As Workbook, sht As Worksheet
For Each book In Workbooks
If book.Name <> ThisWorkbook.Name Then ' skip ThisWorkbook and avoid possible worksheet duplication
If GetWorksheet(book, "COOLING_RAW", sht) Then sht.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) ' if currently searched workbook has wanted worksheet then copy it to ThisWorkbook
End If
Next
End Sub
Function GetWorksheet(book As Workbook, shtName As String, sht As Worksheet) As Boolean
On Error Resume Next ' prevent subsequent statement possible error from stoping the function
Set sht = book.Worksheets(shtName) ' try getting the wanted sheet in the passed workbook
GetWorksheet = Not sht Is Nothing ' return 'True' if successfully got your sheet in the passed workbook
End Function

Activating Workbooks written a cell

I'm working on an app that has to use two more excel files for its function. Names of those files change every day so I decided that both of those files are going to be opened from cells that contain functions to change the name based on the date like this.
Workbooks.Open Range("C2")
Windows("App.xlsm").Activate
Workbooks.Open Range("C16")
Now the problem is that I don't know how to switch between the books that I've opened. Normally I'd use something like this:
Windows("A.xlsx").Activate
But I clearly can't do that now. My idea was to activate App.xlsm and then activate a workbook written in the cell that I used like his:
Windows(Workbooks("App.xlsm").Sheets("Pom").Range("C16").Value).Activate
However that doesn't work. Now I'm not sure whether my code is wrong or whether this method just isn't possible. Can someone help me please ?
Use variables to have a handle on the workbooks
Dim wb1 as workbook
dim wb2 as workbook
set wb1 = Workbooks.Open (Range("C2"))
set wb2 = Workbooks.Open (Range("C16"))
' Activate wb1
wb1.activate
' or activate wb2
wb2.activate

Paste sheet copied from an open workbook into an existing non open workbook

I am trying to copy "Sheet1" from the workbook I have open, labelled "source.xlsm", and paste after the last sheet of my existing workbook which is not open at the time of running, labelled "target.xlsx".
I have the below code and it seems the whole "C:\" directory does nothing. Is it even possible to put a directory in? I cannot find a way to do this without having the Target.xlsx open.
ActiveSheet.Select
ActiveSheet.Copy After:=Workbooks("C:\Target.xlsx").Sheets("FirstSheet")
You already figured it out, but a suggested edit:
Dim wb As WorkBook
''Open 2nd Workbook
Set wb = Workbooks.Open(Filename:="C:\Archive.xlsx")
''Copy To Different Workbook
Workbooks("Source.xlsx").Sheets("Source").Copy _
After:=wb.Sheets("Archive")
''Close 2nd Workbook
wb.Save
wb.Close
Answered my own question with the helpful information provided by Tim.
''Open 2nd Workbook
Workbooks.Open Filename:="C:\Archive.xlsx"
''Copy To Different Workbook
Workbooks("Source.xlsx").Sheets("Source").Activate
ActiveSheet.Copy After:=Workbooks("Archive.xlsx").Sheets("Archive")
''Close 2nd Workbook
Workbooks("Archive.xlsx").Sheets("Archive").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close

Active workbook is randomly changed after macro execution

After I run a macro(which selects records from sql server, no reference to another workbook), one of the other open workbooks is randomly activate.
The macro is in book1, I run the macro from book1.
At the end, book2(or book3 etc) is activate. Why ?!
I tried to put just before End Sub
Dim Wb As Workbook
Set Wb = Active/ThisWorkbook
Wb.Activate
or
msgbox "ok"
but still fly to another open workbook(the message box pop up on book2)
This thing not happens every time, just sometimes, randomly.
Thank you
update: Since I fixed a cirrcular refference in book2, seems to stop.
See Difference Between ActiveWorkbook and ThisWorkbook
Sub Bus()
Dim Wb As Workbook
Set Wb = ActiveWorkbook
Debug.Print Wb.Name
Set Wb = ThisWorkbook
Debug.Print Wb.Name
End Sub
If you put above code in Book2. Module 1 and Select Book1 and run it
You will get following output in the immediate Window:
Book1
Book2