Excel vbs code for dynamic range referencing

DenniBrink

New Member
Joined
Jul 31, 2016
Messages
46
:) Greetings everyone! The following VBS script has fixed defined ranges. I need the ranges to dynamically change as new rows are added to the worksheets.

Sub SATFILTER()
' SATFILTER Macro
' Filters Master Daily Schedule Worksheet for Employees Assigned to Work Saturday.


Dim SATFILTER$
Dim Wksheet1 As Worksheet, Wksheet2 As Worksheet
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range
Set Wksheet1 = Sheets("SAT_REVIEW")
Set Wksheet2 = Sheets("MASTER DAILY SCHED")
Set Rng1 = Wksheet1.Range("B8:C38") - 'Need to change to dynamic'
Set Rng2 = Wksheet2.Range("$C$4:$I$56") - 'Need to change to dynamic'
Set Rng3 = Wksheet2.Range("B5:C56") - 'Need to change to dynamic'
Set Rng4 = Wksheet1.Range("B8")


Rng1.Clear
Rng2.AutoFilter Field:=1, Criteria1:="<>NS"
Rng3.Copy Rng4
Rng2.AutoFilter Field:=1 'I believe this turns off the ant trail enabled by AutoFilter'
Wksheet1.Activate
MsgBox "Filtering Done!"

End Sub

Worksheet2 table number of columns are fixed ("B:I") and never will change. The table will only grow in size by the addition of rows. Currently lastrow = Range("B56:I56").

How best to implement the capability of dynamic cell referencing and improve the VBS script is my objective. I welcome all Excel enthusiasts collaborative ideas.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Maybe:
Code:
Sub SATFILTER()
    ' SATFILTER Macro
    ' Filters Master Daily Schedule Worksheet for Employees Assigned to Work Saturday.
    Application.ScreenUpdating = False
    Dim SATFILTER$
    Dim Wksheet1 As Worksheet, Wksheet2 As Worksheet
    Dim Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range
    Set Wksheet1 = Sheets("SAT_REVIEW")
    Set Wksheet2 = Sheets("MASTER DAILY SCHED")
    Set Rng1 = Wksheet1.Range("B8:C" & Range("C" & Rows.Count).End(xlUp).Row) 'Need to change to dynamic'
    Set Rng2 = Wksheet2.Range("$C$4:$I$" & Range("I" & Rows.Count).End(xlUp).Row)  'Need to change to dynamic'
    Set Rng3 = Wksheet2.Range("B5:C" & Range("C" & Rows.Count).End(xlUp).Row) 'Need to change to dynamic'
    Set Rng4 = Wksheet1.Range("B8")
    
    
    Rng1.Clear
    Rng2.AutoFilter Field:=1, Criteria1:="<>NS"
    Rng3.Copy Rng4
    Rng2.AutoFilter Field:=1 'I believe this turns off the ant trail enabled by AutoFilter'
    Wksheet1.Activate
    MsgBox "Filtering Done!"
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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