VBA IF Statement to reference a cell in another workbook

Soulidarity

New Member
Joined
Feb 18, 2020
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hi All,

Please be patient with me, I am a novice with VBA. I am trying to use the following code to check if a cell in the current workbook = a cell in another workbook (which is open) then if it does, carry out a copy and paste function. I can do this easily if the data is in one spreadsheet but I have never done it before where it involves another spreadsheet. Any help would be greatly appreciated.

VBA Code:
Dim Path1 As String
Dim myfilename As String
Path1 = Range("I4")
Path2 = Range("E6")
myfilename = Range("I5")
Workbooks.Open Filename:=Path1 & "" & Path2 & myfilename

ThisWorkbook.Activate
Application.ScreenUpdating = False
    'CC 1 January
If (Range("E5") =[B] Cell in the workbook opened above[/B]) And (Range("E3") = "January") Then
'Copy data
Sheet1.Select
Range("E155").Select
Selection.Copy
'Paste data
Workbooks("Master PCA SPB Tracker").Activate
Sheet2.Select
Range("B16").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

Thank you in advance!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
The easiest way is Dim a variable as a workbook, then set the variable when you open it. That way you can reference the workbook directly in your code. It also makes the code mush easier to read with fully qualified references to cells.

Something like this:

VBA Code:
Dim wb As Workbook 'define variable to hold a workbook

Set wb = Workbooks.Open(Filename:=Path1 & "" & Path2 & myfilename)

Is there just 1 work sheet in the newly opened workbook?
 
Upvote 0
Hi Gallen,

Thank you for the response! There are 6 sheets in the workbook, will this change the code you have supplied?
 
Upvote 0
Hi,

In that case it's better to go one step further (and always good practice) to have a worksheet variable too.

VBA Code:
    Dim wb As Workbook 'define variable to hold a workbook
    Dim ws As Worksheet
    
    Set wb = Workbooks.Open(Filename:=Path1 & "" & Path2 & myfilename)
    
    Set ws = wb.Worksheets("YOUR_SHEET_NAME") 'change name to name of sheet
    
    'then you can use ws to qualify the cell references
    ws.Range ("Your range")
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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
Back
Top