Hello,
I am using Excel 2013 and have used the macro recorder to apply a custom sort on column D first then column C. This code is straight from the macro recorder. I added the Dim statements, Set wb=ActiveWorkbook, and wkshtname = Activesheet.Name lines.
The original macro code had the worksheet name wherever there is code like this below, but I tried to replace the actual worksheet name with the variable 'wkshtname'.
I get the above 'Run-time error '91': Obj. variable or With block variable not set' for this line of code in the full code below. I have activeworkbook open and there are no other workbooks open.
I've looked at many, many websites and have tried to fix this:
Can anyone help to resolve this? I've included a test file below. The code is in Module 1. I appreciate the help.
I am using Excel 2013 and have used the macro recorder to apply a custom sort on column D first then column C. This code is straight from the macro recorder. I added the Dim statements, Set wb=ActiveWorkbook, and wkshtname = Activesheet.Name lines.
The original macro code had the worksheet name wherever there is code like this below, but I tried to replace the actual worksheet name with the variable 'wkshtname'.
Code:
ActiveWorkbook.Worksheets("Actual Worksheet Name"), etc.
I get the above 'Run-time error '91': Obj. variable or With block variable not set' for this line of code in the full code below. I have activeworkbook open and there are no other workbooks open.
Code:
ActiveWorkbook.Worksheets(wkshtname).AutoFilter.Sort.SortFields.Clear
I've looked at many, many websites and have tried to fix this:
- I tried 'Set wkshtname = = ActiveSheet.Name' but I get an error that 'Set' is not correct.
- I added 'Dim wb, etc.', but I don't see why I need to 'Dim wb, etc.' b/c I don't use the variable. I just didn't know if Excel was not recognizing the ActiveWorkbook part of the line.
- I tried using the Worksheet number index like below but that didn't work either:
Code:
ActiveWorkbook.Worksheets(1).AutoFilter.Sort.SortFields.Clear
Can anyone help to resolve this? I've included a test file below. The code is in Module 1. I appreciate the help.
Code:
Sub macrosort()
Dim wb As Workbook
Dim wkshtname As String
Dim lnglastrow as Long
Set wb = ActiveWorkbook
wkshtname = ActiveSheet.Name
lnglastrow = ActiveSheet.UsedRange.Rows.Count
'sort by Hub column D and then by Week column C before run color code below so will color correctly
ActiveWorkbook.Worksheets(wkshtname).AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets(wkshtname).AutoFilter.Sort. _
SortFields.Add Key:=Range("D2:D" & lnglstRow), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(wkshtname).AutoFilter.Sort. _
SortFields.Add Key:=Range("C2:C" & lnglstRow), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(wkshtname).AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub