VBA Vlookup to Another Workbook using pathname & Variables

ABCDH

New Member
Joined
Sep 30, 2009
Messages
12
I am new to VBA. The code below works, except for two issues I cannot solve.

(1) When I run macro, I have to manually select the path to the file (ProviderCounts) in vlookup. If I try Workbooks.Open; then the file opens, but stays on that file and resets the macro to the beginning.

(2) Within the Vlookup, I want to replace cell reference C36 with a variable. This is part of a larger macro and the row number may change. Ie. replace C36 with variable X. X = Range("C" & RowNumber + 3).Value

Variable X (or C36) is a DATE. I've read that declaring variable X as Date does not work. I've been unsuccessful declaring as long or integer.

Any help is much appreciated. Thank you!

Code:
Sub MembershipCount()
Dim CountRows As Long, RowNumber As Long

'            Workbooks.Open ("S:\A\Provider counts\ProviderCounts.xlsx")

With Sheets("Income Statement")
    CountRows = .Range("A" & Rows.Count).End(xlUp).Row
    For RowNumber = CountRows To 1 Step -1
        If Cells(RowNumber, 1) = "Net Income" Then
            Range("C" & RowNumber + 4).Formula = "=Vlookup(C36, 'S:\A\Provider Counts\[ProviderCounts.xlsx]Sheet1'!$A:$B, 2, False)"
        End If
    Next RowNumber
End With
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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