Use variable to define range name

Paul Sansom

Board Regular
Joined
Jan 28, 2013
Messages
178
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Hi
I have a simple macro that I copy paste a range defined from a name
The range in the Macro is "Recipe1". I would like this to be defined from a cell that varies (from my Combo Box)
Recipe2 Recipe3 etc. I have already defined these ranges but....

I am struggling with the code to replace the .Range("Recipe1") at the begining of the macro below
The vaiable range text to repalce above is at cell $R$4 on "sheet3"

Does this make sense??? and can anyone help please???
Paul

Code:
Sub SampleRecipeLoad()
'
' SampleRecipeLoad Macro
'   Load Chosen Recipe into the raws selection area
'

'   Load Chosen Recipe
    Workbooks("Recipe.xlsm").Worksheets("Sample Recipes").[B]Range("Recipe1").C[/B]opy _
        Destination:=Workbooks("Recipe.xlsm").Worksheets("Recipe").Range("C9")
    
'   Set Chocolate type to Milk and counrty check to EU
    Workbooks("Recipe.xlsm").Worksheets("Recipe").Range("Input_ChocType") = "Milk"
    Workbooks("Recipe.xlsm").Worksheets("Recipe").Range("Country_Output") = "EU"
    
'   Close the Selection Panel Window
    Unload UserForm2
    
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Assuming Sheet3 R4 = Recipe1
Try
Rich (BB code):
Dim MyRange As String
MyRange = Sheets("Sheet3").Range("R4").Value
    Workbooks("Recipe.xlsm").Worksheets("Sample Recipes").Range(MyRange).Copy _
        Destination:=Workbooks("Recipe.xlsm").Worksheets("Recipe").Range("C9")
 
Upvote 0
Sub SampleRecipeLoad()
'
' SampleRecipeLoad Macro
' Load Chosen Recipe into the raws selection area
'

Dim recipeName
RecipeName = Sheets("Sheet3").Range("R4").Value

' Load Chosen Recipe
Workbooks("Recipe.xlsm").Worksheets("Sample Recipes").Range(RecipeName).Copy _
Destination:=Workbooks("Recipe.xlsm").Worksheets("Recipe").Range("C9")

' Set Chocolate type to Milk and counrty check to EU
Workbooks("Recipe.xlsm").Worksheets("Recipe").Range("Input_ChocType") = "Milk"
Workbooks("Recipe.xlsm").Worksheets("Recipe").Range("Country_Output") = "EU"

' Close the Selection Panel Window
Unload UserForm2

End Sub
 
Upvote 0
Thanks JonMo
Worked well , just had to set dim Myrange without string datatype
Dim MyRange
:)
 
Upvote 0

Forum statistics

Threads
1,203,605
Messages
6,056,254
Members
444,853
Latest member
sam69

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