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?
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
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:

lkern777

New Member
Joined
Feb 24, 2018
Messages
6
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
 

lkern777

New Member
Joined
Feb 24, 2018
Messages
6
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]
 

Forum statistics

Threads
1,082,283
Messages
5,364,268
Members
400,787
Latest member
bs04c

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top