Formula with reference to variable workbook

George Lee

New Member
Joined
Oct 26, 2020
Messages
2
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In the following macro I want to replace 'Portfolio_Position_Jul-10-2020.csv' with a variable Fname2
as this changes daily.
I have tried to replace with Fname2
ActiveCell.FormulaR1C1 = _
"=COUNTIF(Fname2!R3C2:R6C2,RC[-12])"
where Fname 2 equals "'Portfolio_Position_Jul-10-2020.csv'",'Portfolio_Position_Jul-10-2020.csv',"'[Portfolio_Position_Jul-10-2020.csv]'"
Could use some help as I'm fairly new to vba

Sub test1()
Dim Fname2 As String
Fname2 = "'Portfolio_Position_Jul-10-2020.csv'"

Range("n3").Activate

ActiveCell.FormulaR1C1 = _
"=COUNTIF('Portfolio_Position_Jul-10-2020.csv'!R3C2:R6C2,RC[-12])"
Range("N4").Activate
ActiveCell.FormulaR1C1 = _
"=COUNTIF('Portfolio_Position_Jul-10-2020.csv'!R3C2:R6C2,RC[-12])"
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Welcome to the forum. :)

You need to concatenate the variable into the formula string:

Code:
ActiveCell.FormulaR1C1 = _
"=COUNTIF(" & FName2 & "!R3C2:R6C2,RC[-12])"
 
Upvote 0
Welcome to the forum. :)

You need to concatenate the variable into the formula string:

Code:
ActiveCell.FormulaR1C1 = _
"=COUNTIF(" & FName2 & "!R3C2:R6C2,RC[-12])"
Thank you very much
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

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