Working with multiple workbooks using VBA

JennV

New Member
Joined
May 9, 2019
Messages
34
Hello,

I currently have this code:

Dim FileName As String
FileName = Sheets("Tracker").Range("A4")

Dim FileName1 As String
FileName1 = Sheets("Tracker").Range("A5")

Set wB2 = Workbooks.Open("file location")
If wB2.ReadOnly Then
ActiveWorkbook.Close
MsgBox "Cannot update as someone is currently updating. Please try again."

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
Exit Sub
End If

Sheets(FileName).Select
Cells.Find(What:=FileName1, After:=Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows).Offset(1, 0).Select
ActiveSheet.Paste



I keep getting a 'subscript out of range' error when it hits the red text above. My guess is because it's not understanding that I'm trying to reference the other workbook (wb1).

What I have in wb1 in sheet 'Tracker' is:
A4: EDM12345
A5: John Smith

So after I copy data from wb1, I want to use the value in A4 to select the sheet EDM12345 in wb2. In addition, it look for A5 (John Smith) within sheet EDM12345 in wb2.

I could do this:

Sheets("EDM12345").Select
Cells.Find(What:="John Smith", After:=Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows).Offset(1, 0).Select
ActiveSheet.Paste

But since wb2 is a database and there will be multiple wb1 with different search criterias (instead of John Smith, say I want to look for Jane Doe or instead of EDM12345, I want to go into sheet EDM54321), I would like to save the hassle of going into wb1 to change the VBA coding.

Any help is much appreciated.
 

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,185
Office Version
  1. 2016
Platform
  1. Windows
At the start of your code, is wb1 the active workbook ?
Check the actual values being assigned to FileName and FileName1 ?
For debugging purposes, don't set DisplayAlerts to False - so you can see if any problems
 

Jon Peltier

MrExcel MVP
Joined
May 14, 2003
Messages
4,943
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
You probably want Workbooks(FileName), not Sheets(FileName).
 

JennV

New Member
Joined
May 9, 2019
Messages
34
According to the info supplied, FileName is a worksheet name, not a workbook name.

Thank you! I have resolved the issue. What I didn't do is set the active workbook (wb1) at the beginning of the code so that's why I was getting the error. Have a good day :)
 

Watch MrExcel Video

Forum statistics

Threads
1,123,230
Messages
5,600,424
Members
414,383
Latest member
kevinlarey

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
Top