VBA Macro - hides rows based on a single cell which is limited by data validation

macca_18380

New Member
Joined
May 15, 2024
Messages
22
Office Version
  1. 365
Hi,

I'm new to this forum, forgive me if i don't understand the rules of engagement.

I'm still very much an apprentice when it comes to VBA, i did manage to find a thread on another site which addressed my need in part but the answer wouldn't work.

Essentially, I would like a macro to hide rows of data dependent upon text, which is selected via data validation, in a single cell.

Below is the code that i found and have edited to suit my needs, albeit it doesn't actually work.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim iCell As Range: Set iCell = Intersect(Range("F5"), Target)

If iCell Is Nothing Then Exit Sub

If iCell.Value = "PPA Rate" Then

Rows("8:12").Hidden = False

Rows("13:17").Hidden = True

ElseIf iCell.Value = "Dev Fee" Then

Rows("8:12").Hidden = True

Rows("13:17").Hidden = False

'Else ' do nothing

End If

End Sub

The macro doesn't work when i paste this into a module, why is this the case?

Thank you
Michael
 

Attachments

  • Screenshot 2024-05-15 082010.png
    Screenshot 2024-05-15 082010.png
    26.8 KB · Views: 17

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You said:
The macro doesn't work when i paste this into a module, why is this the case?

This script is a sheet change script.
The script runs when you change a value in the sheet:

Right click on the sheet Tab and select view code and past the script in there.

Here use this as a test:
A Message box will popup if you enter the correct text.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim iCell As Range: Set iCell = Intersect(Range("F5"), Target)

If iCell Is Nothing Then Exit Sub

If iCell.Value = "PPA Rate" Then
MsgBox "PPA Rate"
Rows("8:12").Hidden = False

Rows("13:17").Hidden = True

ElseIf iCell.Value = "Dev Fee" Then
MsgBox "Dev Fee"
Rows("8:12").Hidden = True

Rows("13:17").Hidden = False

'Else ' do nothing

End If

End Sub
 
Upvote 0
That works brilliantly, thank you. Is it possible to get the same effect but without a MsgBox popping up?
 
Upvote 0
Actually, I like using select case.

Look at this code and try it if you want.
Eliminates using a lot of if statements.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$F$5" Then

Select Case Target.Value
    Case "PPA Rate"
        Rows("8:12").Hidden = False
        Rows("13:17").Hidden = True

    Case "Dev Fee"
        Rows("8:12").Hidden = True
        Rows("13:17").Hidden = False
End Select

End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

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