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 chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,086
Messages
6,128,734
Members
449,466
Latest member
Peter Juhnke

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