Macro for User Input with relative references

lkern777

New Member
Joined
Feb 24, 2018
Messages
6
I need help correcting a Macro. I have a sample spreadsheet that I can attach, but do not see how to do that.

I want the user to be able to press the "Clear Form" button on any recipe in a recipe pricing spreadsheet and clear the individual recipe. I recorded the following macro with relative cell references so that I can use one macro for all recipes. The sample spreadsheet only has 2 recipe sections, but the actual spreadsheet where I will be using this has 100.

Sub ClearRecipe()
'
' ClearRecipe Macro
'

'
ActiveCell.Offset(0, 1).Range("A1:C1").Select
Selection.ClearContents
ActiveCell.Offset(3, -1).Range("A1:D16").Select
Selection.ClearContents
ActiveCell.Offset(18, 3).Range("A1:A2").Select
Selection.ClearContents
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.ClearContents
ActiveCell.Offset(-20, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "FALSE"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "FALSE"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "FALSE"
ActiveCell.Offset(5, -1).Range("A1").Select
End Sub


This works as long as the user knows to click the Recipe # before clicking the "Clear Form" button.

I want the user to be able to click the "Clear Form" button no matter where the cursor is located, so I added the following code to ask for an input cell, hoping it would then start the relative references later in the macro with the user input. It did not work.

Sub ClearRecipe_inputRange()
'
' ClearRecipe_inputRange Macro
'

'
Dim inputRange As Range
On Error Resume Next
Set inputRange = Application.InputBox("Click Recipe # to delete.", Type:=8)
On Error GoTo 0
If Nothing Is inputRange Then
MsgBox "No Recipe # Chosen."
Else
MsgBox "Recipe " & inputRange.Value & " was chosen."
End If
ActiveCell.Offset(0, 1).Range("A1:C1").Select
Selection.ClearContents
ActiveCell.Offset(3, -1).Range("A1:C16").Select
Selection.ClearContents
ActiveCell.Offset(18, 3).Range("A1:A2").Select
Selection.ClearContents
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.ClearContents
ActiveCell.Offset(-20, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "FALSE"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "FALSE"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "FALSE"
ActiveCell.Offset(-3, -4).Range("A1").Select
End Sub

I tried changing all of the "ActiveCell" references to "inputRange", but the macro errors out on the line before the "FALSE" lines begin.

Can someone help me correct this?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Are the recipe numbers in column A?

Can you give one example with absolute cell references? So say the user selects cell A3, then what cells do you want cleared and what cells get the "False" text?
 
Last edited:
Upvote 0
Thank you for the reply.

Here is an example with absolute references. In this recipe, the Recipe # is in cell B8. The "False" fields are connected to check-boxes that I want un-checked when the form is cleared (if they are checked). They are not ActiveX checkboxes.

It would make a lot more sense if you could see the actual recipe form, but I do not know how to upload it.

Code:
Sub ClearRecipe_AbsoluteReferences()
'
' ClearRecipe_AbsoluteReferences Macro
'


'
    Range("C8:E8").Select
    Selection.ClearContents
    Range("B11:E26").Select
    Selection.ClearContents
    Range("E29:E30").Select
    Selection.ClearContents
    Range("F29").Select
    Selection.ClearContents
    Range("G9").Select
    ActiveCell.FormulaR1C1 = "FALSE"
    Range("G10").Select
    ActiveCell.FormulaR1C1 = "FALSE"
    Range("G11").Select
    ActiveCell.FormulaR1C1 = "FALSE"
    Range("F16").Select
End Sub
 
Upvote 0
I had posted this on another forum and this was the solution that worked for me:

Code:
[COLOR=#333333]Sub ClearRecipe_inputRange()[/COLOR]<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">'
' ClearRecipe_inputRange Macro
'

'
    Dim inputRange As Range
    On Error Resume Next
    Set inputRange = Application.InputBox("Click Recipe # you want to delete.", Type:=8)
    On Error GoTo 0
    If Nothing Is inputRange Then
    MsgBox "No Recipe # Chosen."
Else
    MsgBox "Recipe " & inputRange.Value & " was chosen."
End If
    inputRange.Offset(0, 1).Range("A1:C1").Select
    Selection.ClearContents
    inputRange.Offset(3, 0).Range("A1:C16").Select
    Selection.ClearContents
    inputRange.Offset(21, 3).Range("A1:A2").Select
    Selection.ClearContents
    inputRange.Offset(21, 4).Range("A1").Select
    Selection.ClearContents
    inputRange.Offset(1, 5).Range("A1").Select
    ActiveCell.FormulaR1C1 = "FALSE"
    inputRange.Offset(2, 5).Range("A1").Select
    ActiveCell.FormulaR1C1 = "FALSE"
    inputRange.Offset(3, 5).Range("A1").Select
    ActiveCell.FormulaR1C1 = "FALSE"
    inputRange.Offset(8, 4).Range("A1").Select </code>[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,576
Messages
6,120,350
Members
448,956
Latest member
Adamsxl

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