Have User Form populate only the active sheet not by sheet name.

Tmoske

Board Regular
Joined
Jan 14, 2009
Messages
145
I have a userform in my workbook with a button to access it on twenty different sheets in the workbook. Currently the userform will only populate the sheet titled "Blank1". How can I change it so it will populate the active sheet without naming it? So no matter what sheet I'm on when I hit the submit button the userform will populate only the sheet I selected the button on? See macro below. Thanks!


Rich (BB code):
Private Sub OKButton_Click()
    Dim NextRow As Long
    Sheets("Blank1").Activate
    
'   Unprotect Sheet
    ActiveSheet.Unprotect
    
' Sort Macro
    ActiveWorkbook.Worksheets("Blank1").ListObjects("Table1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Blank1").ListObjects("Table1").Sort.SortFields.Add _
        Key:=Range("D5:D55"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Blank1").ListObjects("Table1").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
'   Protect Sheet
    ActiveSheet.Protect
    
'   Close Userform
    Unload Me
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I have a userform in my workbook with a button to access it on twenty different sheets in the workbook. Currently the userform will only populate the sheet titled "Blank1". How can I change it so it will populate the active sheet without naming it? So no matter what sheet I'm on when I hit the submit button the userform will populate only the sheet I selected the button on? See macro below. Thanks!


Rich (BB code):
Private Sub OKButton_Click()
    Dim NextRow As Long
    Sheets("Blank1").Activate
    
'   Unprotect Sheet
    ActiveSheet.Unprotect
    
' Sort Macro
    ActiveWorkbook.Worksheets("Blank1").ListObjects("Table1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Blank1").ListObjects("Table1").Sort.SortFields.Add _
        Key:=Range("D5:D55"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Blank1").ListObjects("Table1").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
'   Protect Sheet
    ActiveSheet.Protect
    
'   Close Userform
    Unload Me
End Sub


Delete this line of code:

Code:
Sheets("Blank1").Activate
Code:

Change this:

ActiveWorkbook.Worksheets("Blank1")

To this:

ActiveSheet

In both places. Actually, every place that the first statement appears.
 
Last edited:
Upvote 0
Thanks! that worked fine but now I have an issue with the sorting part of the macro. I get the error "Subscript out of range"
It refers to Table1, but on each of my sheets the Table name is different. Is there a way around this?

Rich (BB code):
ActiveSheet.ListObjects("Table1").Sort.SortFields.Clear
    ActiveSheet.ListObjects("Table1").Sort.SortFields.Add
 
Upvote 0

Forum statistics

Threads
1,216,743
Messages
6,132,463
Members
449,729
Latest member
davelevnt

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