VBA code for button on one sheet to filter table in another sheet

t_mo31

New Member
Joined
Jan 23, 2017
Messages
9
Hello,

I have two tables on two sheets in excel, the first asks the user to score a number of criteria in cells D5 to D107. The next sheet is a copy of this table (with the values of D5:D107 being copied from Sheet 1 to Sheet 2), but then has additional columns where the user needs to add more information.

I inserted a command button and drafted a macro (found online) so that the table on Sheet 2 auto filters so only criteria scored as less than 3 in Sheet 1 are shown in Sheet 2.

However, I want the command button to be on Sheet 1, but alter the table on Sheet 2. I dont know how to edit the macro so that it can do this.

Any help greatly appreciated!

See macro:

Private Sub CommandButton1_Click()
Dim range_to_filter As Range
Set range_to_filter = Range("D5:D107")

range_to_filter.AutoFilter Field:=1, Criteria1:="<2"

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try adding the sheet name (called it sSheet2 below)

VBA Code:
Private Sub CommandButton1_Click()
Dim range_to_filter As Range
Set range_to_filter = Sheets("Sheet2").Range("D5:D107")

range_to_filter.AutoFilter Field:=1, Criteria1:="<2"

End Sub
 
Upvote 0
Hi,

I get "runtime error 9: subscript out of range, and when I debug it highlights the row of code with the added "sheets" command.

See attached (and note for ease in my post i just said sheet 1 sheet 2, but they do actually have names hence the slight difference in code)


Thanks!
 

Attachments

  • Capture.PNG
    Capture.PNG
    9.2 KB · Views: 41
Upvote 0
Check for spaces in the Sheet name or try renaming it. A runtime error 9 usually indicates you're referring to a sheet that doesn't exist.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

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