Execute macro on drop-down list change

SpectreHUN

New Member
Joined
Aug 15, 2014
Messages
14
So I have a Sheet, where I want to show only the data which is relevant based on the user's selection in the drop-down list (G3). My code so-far:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G3")) Is Nothing Then
Application.Run "HideRows"
End If
End Sub
Sub HideRows()
On Error Resume Next
LTRW = Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To LTRW
    If Cells(i, 7).Value = "Import" Then
    For j = 60 To 82
        Cells(j, 20).EntireRow.Hidden = True
      
    Next j
    End If
    If Cells(i, 7).Value = "Közösségen belüli beszerzés" Then
    For j = 60 To 82
        Cells(j, 20).EntireRow.Hidden = False
      
    Next j
    End If
Next i
End Sub

It works, but it's not automatic. I have to manually run the macro every time there is a change in G3. Any help would be appreciated!
 
Did it!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Cells(3, 7).Value = "Import" Then
   Rows("60:82").Hidden = True
Else
   Rows("60:82").Hidden = False
End If
End Sub

I still don't understand why the others didn't work though. Anyway, thanks for the help! Wouldn't have been able to succeed without it!
 
Upvote 0

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
Found out what's been the problem the whole time. The sheet was protected.. :laugh:

The final code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Cells(3, 7).Value = "Import" Then
   ActiveSheet.Unprotect '
   
   Rows("60:82").Hidden = True
   
   ActiveSheet.Protect , DrawingObjects:=True, Contents:=True, Scenarios:=True
Else
   ActiveSheet.Unprotect

   Rows("60:82").Hidden = False
   
   
   ActiveSheet.Protect , DrawingObjects:=True, Contents:=True, Scenarios:=True

End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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