Error: variable in vba formula

kdreed15

New Member
Joined
Oct 27, 2011
Messages
10
I must have messed up the vba syntax here but I can't figure it out. The error is in the last line. Any help would be appreciated!

RangeName = InputBox("Enter the name you used to define the data from the last sheet imported. (Must be exact!)")

ActiveCell.Offset(1, 0).Select
ActiveCell.Formula = "=countifs('[" & RangeName & "]'!,WTX)"
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Phanmore

Board Regular
Joined
Aug 7, 2009
Messages
116
ActiveCell.Formula = "=countifs('[" & RangeName & "]'!,""WTX"")" <!-- / message -->
 

kdreed15

New Member
Joined
Oct 27, 2011
Messages
10
Here is the whole code.



Sub Find_Date()
RangeName = Range("D3").Value

Dim FindString As String
Dim Rng As Range
FindString = InputBox("Enter Date of last entry exactly as it appears in cell")
If Trim(FindString) <> "" Then
With Sheets("Historical Data").Range("4:4")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
'This will insert the necessary formulas

ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=RC[-1]+7"


ActiveCell.Offset(1, 0).Select
ActiveCell.Formula = "=countifs('[" & RangeName & "]'!,""WTX"")"

End Sub
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061

ADVERTISEMENT

What does the formula look like if you manually enter it in the sheet?

Named ranges don't require the '[]'! around them...
so it probably looks like
=COUNTIFS(RangeName,"WTX") right?

try
ActiveCell.Formula = "=countifs(" & RangeName & ",""WTX"")"
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061

ADVERTISEMENT

OK, so..

1. What is the exact formula you expect to be entered in the cell ?
2. What is the value of the variable RangeName ?
 
Last edited:

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,884
ActiveCell.Formula = "=COUNTIFS(" & RangeName & ",""WTX"")"
worked for me.
 

kdreed15

New Member
Joined
Oct 27, 2011
Messages
10
OK, so..

1. What is the exact formula you expect to be entered in the cell ?
2. What is the value of the variable RangeName ?

1. The formula I want is: =COUNTIFS(Data.10.14.11,"WTX")
This formula works if manually entered in.

2. The value that I typed into cell "D2" is Data.10.14.11

Basically what I did was defined a name for a range of cells on a different sheet. I want to be able to make that name a variable because it will change weekly. Maybe there is a completely different way of doing this. I tried an input box but was getting the same error.
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Ok, so if you put Data.10.14.11 in D2...

But your code is written

RangeName = Range("D3").Value

Then RangeName is getting the wrong value..
 

Watch MrExcel Video

Forum statistics

Threads
1,122,818
Messages
5,598,280
Members
414,223
Latest member
Accountant2B

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