How to select an open Excel file with different file ending name - vba

I have an Excel Macro which look for specific file name Jobs in Lab.xlsx and copy to other macro file.
However, on certain event, the file naming may change to Jobs in Lab (0 - 195).xlsx.
How do I implement Excel Left Syntax into below VBA?
Sub Test()
Windows("Jobs in Lab").Activate
End Sub

You can use the LIKE operator.
Sub Sample()
Dim wb As Workbook
For Each wb In Application.Workbooks
If wb.Name Like "Jobs in Lab*" Then
wb.Activate
Exit Sub
End If
Next wb
End Sub
The code assumes that the file is opened in the same Excel instance.
EDIT:
However if you still want to use LEFT then you can use it like this. Notice the use of Ucase. It converts the text into upper case and then does a comparison. You can use LCASE as well.
Sub Sample()
Dim wb As Workbook
For Each wb In Application.Workbooks
If UCase(Left(wb.Name, 11)) = "JOBS IN LAB" Then
wb.Activate
Exit Sub
End If
Next wb
End Sub

Related

Excel VBA Code to select opened Excel file

I've done lots of search through various website but to no avail.
Previously I create simple VBA to select XLS from automatic-generated-XLS which created by a software as below. This excel does not have any file type at it's end because it was not saved anywhere yet and i'm not planning to save it.
Sub Test()
Windows("All Jobs").Activate
End Sub
Lately, the software generate new file name "All Jobs (0 - XX)" which XX could be any number from 1 to 100. So, the code above does not work anymore. Adding * does not help.
Any suggestion?
Thanks in advance.
Zaiem
You can loop thru the Windows collection to check the caption of each window. Try this:
Private Sub Test()
Dim myWindow As Window
For Each myWindow In Windows
If Left(myWindow.Caption, 8) = "All Jobs" Then myWindow.Activate: Exit Sub
Next myWindow
End Sub
You can loop through all Workbooks in Application (Excel), and if the current workbook's name is Like "All Jobs" & "*" >> this is your desired workbook.
Note: verify that you need to Activate the workbook, usually it is not needed.
Code
Option Explicit
Sub Test()
Dim WB As Workbook
Dim AllJobSWB As Workbook
For Each WB In Application.Workbooks
If WB.Name Like "All Jobs" & "*" Then
Set AllJobSWB = WB ' set the matched workbook
Exit For
End If
Next WB
' if you must activate the workbook, use the line below
AllJobSWB.Activate
End Sub

Call "ThisWorkbook"

I am trying to switch between a template (hard coded) and a dynamic report which changes name weekly (ThisWorkbook). I am struggling with calling the variable x to bring focus to the workbook. I am copying the template formulas and pasting them into the dynamic report.
Sub wkbk()
Dim x As Excel.Workbook
Set x = ThisWorkbook
Dim pth As String
pth = x.FullName
Windows(pth).Activate
End Sub
Here is the VBA code I am using:
Windows("BBU_CMD_TEMPLATE.xlsx").Activate
Cells.Select
Selection.Copy
Windows(pth).Activate
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
Why not just use ThisWorkbook.Activate? There's generally no need to assign a variable to represent a built-in like ThisWorkbook so the rest of those variables are unnecessary unless you're using them elsewhere in that procedure (from the snippet provided, you aren't, so you don't need them).
Sub wkbk()
ThisWorkbook.Activate
End Sub
However, what's the point of wkbk procedure? If solely to activate the workbook, that's not needed either and there are plenty of reasons to avoid Activate.
Sub CopySheetFromTemplateToThisWorkbook()
Dim tmplt As Workbook
On Error Resume Next
Set tmplt = Workbooks("BBU_CMD_TEMPLATE.xlsx")
If tmplt Is Nothing Then
MsgBox "Template file needs to be open..."
Exit Sub
End If
On Error GoTo 0
With ThisWorkbook
tmplt.ActiveSheet.Copy After:=.Sheets(.Sheets.Count)
End With
End Sub

Excel VBA: How to activate a workbook without its complete name?

I have multiple excel workbooks open. I want to activate a workbook which has "Final" in its name.
Example: I have three open workbooks called "Workbook1.xlsx", "worKbook2.xlsm" and "workbookFinal.xlsx" open at the same time.
My VBA code is in "Macro.xlsm". Using VBA I want to activate the workbook which has "Final" in it. FYI .. all workbooks are in different paths.
Try the code below, using the Like Operator with the wild-card *.
Option Explicit
Sub FindFinalWorkbook()
Dim wb As Workbook
' loop through all open workbooks
For Each wb In Application.Workbooks
If wb.Name Like "*Final*" Then '< -- check if workbook name is Like *Final*
wb.Activate
Exit For
End If
Next wb
End Sub
loop through Workbooks collection until finding the right named workbook:
Sub wbs()
Dim wb As Workbook
For Each wb In Workbooks
If InStr(wb.Name, "Final") > 0 Then
wb.Activate
Exit For
End If
Next
End Sub

Excel Macro: Setting a variable for a workbooks location?

I need to write a macro script that will copy data from one xml workbook and paste the values to another workbook. I've written the below macro that works fine, but i need to run this every week for several different documents so it means i have to replace the document name for each run.
Here's what i have so far:
Sub copying()
''''''Section 1''''''
Workbooks("Results_2561").Activate 'workbook i'm copying from
Range("B27:B41").Select
Selection.Copy
Workbooks("Overall_Results").Activate 'workbook i'm pasting to
Range("G2").PasteSpecial
''''''Section 2''''''
Workbooks("Results_2561").Activate
Range("C27:C41").Select
Selection.Copy
Workbooks("Overall_Results").Activate
Range("C2").PasteSpecial
''''''Section 3''''''
Workbooks("Results_2561").Activate
Range("I28:I40").Select
Selection.Copy
Workbooks("Overall_Results").Activate
Range("G17").PasteSpecial
''''''Section 4''''''
Workbooks("Results_2561").Activate
Range("J28:J40").Select
Selection.Copy
Workbooks("Overall_Results").Activate
Range("C17").PasteSpecial
End Sub
...
and that's only half the script. Is there a way i can declare a variable at the start and set it as the Workbooks file path so i can call that instead of typing and retyping it over and over again?
Preferably without using something like
Dim book1 as Workbook
Set book1 = Workbooks.Open("C://Results_2561.xlsm")
..as this keeps opening and closing the document when i run the script.
Thanks
since you're only interested in copying values you could use this helper Sub
Sub CopyValues(rngToCopyFrom As Range, rngToCopyTo As Range)
With rngToCopyFrom
rngToCopyTo.Resize(.Rows.COUNT, .Columns.COUNT).Value = .Value
End With
End Sub
to be exploited in your main code like follows:
Sub main()
Dim wsTo As Worksheet
Set wsTo = Workbooks("Overall_Results").ActiveSheet '<--| set the worksheet to paste values to
With Workbooks("Results_2561").ActiveSheet '<--| reference the worksheet to copy values from
CopyValues .Range("B27:B41"), wsTo.Range("G2")
CopyValues .Range("C27:C41"), wsTo.Range("C2")
CopyValues .Range("I28:I40"), wsTo.Range("G17")
CopyValues .Range("J28:J40"), wsTo.Range("C17")
End With
End Sub
should your relevant workbooks have more than one sheet, then just substitute
ActiveSheet
with
Worksheets("myRelevantShetName") '<--|change "myRelevantShetName" to the actual name of the relevant worksheet in each workbook
First of all, you don't have to Activate workbook every time when you want to copy/paste something. Just declare it in Range() property, for example:
''''''Section 1''''''
Workbooks("Results_2561").Sheets(1).Range("B27:B41").Copy
Workbooks("Overall_Results").Sheets(1).Range("G2").PasteSpecial
You can set Workbook as variable like:
Sub copying()
Dim wb1 As Workbook, wb2 As Workbook
Set wb1 = Workbooks("Results_2561")
Set wb2 = Workbooks("Overall_Results")
''''''Section 1''''''
wb1.Sheets(1).Range("B27:B41").Copy
wb2.Sheets(1).Range("G2").PasteSpecial
End Sub
Finally, as #A.S.H suggested, you can add a file dialog where you point which files you want to use. I have put it in some function (don't forget to put it in the same project as your copying macro):
Function strPath() As String
Dim intResult As Integer
Application.FileDialog(msoFileDialogFilePicker).Title = "Select file"
intResult = Application.FileDialog(msoFileDialogFilePicker).Show
If intResult <> 0 Then
strPath = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
End If
End Function
So your final code for Section 1 would look like:
Sub copying()
Dim wb1 As Workbook, wb2 As Workbook
MsgBox "Show file to copy form."
Set wb1 = Workbooks.Open(strPath())
MsgBox "Show file to paste in."
Set wb2 = Workbooks.Open(strPath())
''''''Section 1''''''
wb1.Sheets(1).Range("B27:B41").Copy
wb2.Sheets(1).Range("G2").PasteSpecial
End Sub

How to copy a file and paste into another sheet

I need to copy a file from external drive and paste it into my excel, I can able to browse a file but I cant paste those values into a new sheet...in a same excel
here my code :
Dim fileStr As String
Sub GetOpenFile()
fileStr = Application.GetOpenFilename()
Worksheets("Sheet2").TextBox1.Value = fileStr
End Sub
Sub Button3_Click()
Workbooks.Open fileStr
ActiveSheet.Paste
End Sub
You're trying to paste to the active sheet without having copied anything to begin with. Also, the active sheet probably isn't the sheet you want to paste to anyway.
Try this instead:
Sub Button3_Click()
Dim wbToCopy As Workbook
Set wbToCopy = Workbooks.Open(fileStr)
wbToCopy.Worksheets("Sheet1").Range("A1:H100").Copy
ThisWorkbook.Worksheets("Sheet1").Range("A1").PasteSpecial
wbToCopy.Close
End Sub
ThisWorkbook always refers to the workbook that your macro is running in