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:
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)
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:
but nothing I come up with works. Error city. Thanks
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