[Solved] Passing dynamic names ... as string?

BrianDP1977

Board Regular
Joined
Nov 5, 2005
Messages
146
I have some buttons that call various subs (not sure the term) on seperate moduels. Example ...

Button code on sheet:

Code:
Private Sub CommandButton3_Click()
  Call Module3.Sort_Data
End Sub

This example calls some code on a module that sorts certain named ranges of data (named ranges/variables are Ext_DP6_P1 / Sort_DP6_P1_Start, Ext_DP6_2 / Sort_DP6_P2_Start, etc)

Code:
Sub Sort_Data()
    Range("Ext_DP6_P1").Sort Key1:=Range("Sort_DP6_P1_Start"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=2, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal

    Range("Ext_DP6_P2").Sort Key1:=Range("Sort_DP6_P2_Start"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=2, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal

.
.
.
.
      End If
End Sub

I've added the dots toward the bottom of the code to represent the fact that due to my lack of excel knowledge, I currently have to re-write the execution code for each of the ranges being sorted. Instead, I was wondering if it's possible to consolidate the code by formating the original call code executed by the button so that it passes the named variables and inserts them into the range reference. I've tried a couple ideas such as:

Code:
Private Sub CommandButton3_Click()
Dim Named_Range As String, Named_Start As String

  Named_Range = Ext_DP6_P1
  Named_Start = Sort_DP6_P1_Start
  Call Module3.Sort_Data(Named_Range, Named_Start)
  
  Named_Range = Ext_DP6_P2
  Named_Start = Sort_DP6_P2_Start
  Call Module3.Sort_Data(Named_Range, Named_Start)
.
.
.

End Sub

Code:
Sub Sort_Data(NRange as String, SVar as String)
    Range("NRange").Sort Key1:=Range("SVar"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=2, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal

   End If
End Sub


but nothing I come up with works. Error city. Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi

Try

Code:
Sub Sort_Data(NRange as String, SVar as String) 
    Range(NRange).Sort Key1:=Range(SVar), Order1:=xlAscending, Header:=xlNo, _ 
        OrderCustom:=2, MatchCase:=False, Orientation:=xlTopToBottom, _ 
        DataOption1:=xlSortNormal 

   End If 
End Sub


Tony
 
Upvote 0

Forum statistics

Threads
1,207,438
Messages
6,078,554
Members
446,348
Latest member
ncm3208

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