VBA If Cell Equals Certain Value Then Delete Values of Corresponding Named Range

jaylotheman

New Member
Joined
Oct 5, 2011
Messages
7
I need help creating a VBA IF statement.

For example this could be used for a budget, a forecast etc.

If the value in cell K17 = "F00" of Sheet1 then clear the values (not formats) of the cells in the Named Range "OneElevenSalary" of Sheet2.
If the value in cell K17 = "F01" of Sheet1 then clear the values (not formats) of the cells in the Named Range "Two10Salary" of Sheet2.
If the value in cell K17 = "F02" of Sheet1 then clear the values (not formats) of the cells in the Named Range "Three9Salary" of Sheet2.
Etc.

Does that make sense? Anybody able to shed light on how to do this? Thanks in advance!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Code:
Sub Macro1()

    If Worksheets("Sheet1").Range("K17") = "F00" Then
        Worksheets("Sheet2").Range("OneElevenSalary") = ""
    ElseIf Worksheets("Sheet1").Range("K17") = "F01" Then
        Worksheets("Sheet2").Range("Two10Salary") = ""
    ElseIf Worksheets("Sheet1").Range("K17") = "F02" Then
        Worksheets("Sheet2").Range("Three9Salary") = ""
    End If

End Sub
 
Upvote 0
Thank-you so much fibonacci1101, this worked perfect!
Mainly for information purposes, here is another way to write that macro...

Code:
Sub Macro2()
  If [Sheet1!K17] Like "F0[0-2]" Then
    [Sheet2].Range(Choose(Right([Sheet1!K17], 1) + 1, "OneElevenSalary", "Two10Salary", "Three9Salary")).ClearContents
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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