Need help with VBA

SLElephant

New Member
Joined
Jul 6, 2023
Messages
5
Office Version
  1. 365
I am trying to get a user defined sheet name to work for a xlookup. When I hard code the sheet name it works But when I try to use a name defined it doesn't

Dim CurMonth As Integer
Dim PreMonth As Integer
Dim PreCycleTime As String
Dim PreCPHRate As String

' InsertSheets
'
Sheets.Add before:=Sheets(1)
CurMonth = InputBox("Enter Two Digit Numarical Current Month and Press Enter")

PreMonth = CurMonth - 1

PreCycleTime = ("Cycle Times 2023." & PreMonth)

'***************************************************************************************************************************

Sheets(1).Name = "CPH Rates 2023." & CurMonth

Sheets.Add After:=Sheets(1)
Sheets(2).Name = "Cycle Times 2023." & CurMonth


Sheets("Cycle Times 2023." & CurMonth).Select
'
'

'
'Cycle times Populate
'

THIS WORKS

ActiveCell.Formula = _
"=XLOOKUP(A2&B2&C2, 'Cycle Times 2023.06'!A:A & 'Cycle Times 2023.06'!B:B & 'Cycle Times 2023.06'!C:C, 'Cycle Times 2023.06'!K:K)"


Not THIS

Range("N2").Select
ActiveCell.Formula = _
"=XLOOKUP(A2&B2&C2, 'PreCycleTime'!A:A&'PreCycleTime'!B:B&'PreCycleTime'!C:C,'PreCycleTime'!L:L)"
'
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Untested but maybe...
VBA Code:
Range("N2").Formula = _
"=XLOOKUP(A2&B2&C2,'" & PreCycleTime & "'!A:A&'PreCycleTime'!B:B&'" & PreCycleTime & "'!C:C,'" & PreCycleTime & "'!L:L)"
 
Upvote 0
Cycle Times 2023.06 is the name of a worksheet. PreCycleTime is a variable containing the name of a worksheet. The two are treated differently when using vba to add a formula.

Minor correction to @MARK858 's formula
VBA Code:
Range("N2").Formula = _
"=XLOOKUP(A2&B2&C2,'" & PreCycleTime & "'!A:A & '" & PreCycleTime & "'!B:B & '" & PreCycleTime & "'!C:C, '" & PreCycleTime & "'!K:K)"

Since from a VBA perspective, a formula is just a string, I often find it convenient to use a string variable to build the formula before writing it to a cell. That way I can use the debugger to inspect it to be sure it is what I want.
VBA Code:
Dim FormulaStr As String
FormulaStr = "=XLOOKUP(A2&B2&C2,'" & PreCycleTime & "'!A:A & '" & PreCycleTime & "'!B:B & '" & PreCycleTime & "'!C:C, '" & PreCycleTime & "'!K:K)"
Debug.Print FormulaStr
Range("N2").Formula = FormulaStr
 
Upvote 0
Solution
Cycle Times 2023.06 is the name of a worksheet. PreCycleTime is a variable containing the name of a worksheet. The two are treated differently when using vba to add a formula.

Minor correction to @MARK858 's formula
VBA Code:
Range("N2").Formula = _
"=XLOOKUP(A2&B2&C2,'" & PreCycleTime & "'!A:A & '" & PreCycleTime & "'!B:B & '" & PreCycleTime & "'!C:C, '" & PreCycleTime & "'!K:K)"

Since from a VBA perspective, a formula is just a string, I often find it convenient to use a string variable to build the formula before writing it to a cell. That way I can use the debugger to inspect it to be sure it is what I want.
VBA Code:
Dim FormulaStr As String
FormulaStr = "=XLOOKUP(A2&B2&C2,'" & PreCycleTime & "'!A:A & '" & PreCycleTime & "'!B:B & '" & PreCycleTime & "'!C:C, '" & PreCycleTime & "'!K:K)"
Debug.Print FormulaStr
Range("N2").Formula = FormulaStr
It worked Thank you so much
 
Upvote 0

Forum statistics

Threads
1,215,703
Messages
6,126,320
Members
449,308
Latest member
Ronaldj

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