Macro Error Debug

Toastis

Board Regular
Joined
Apr 20, 2011
Messages
65
I am getting this error script something or other when I run this macro. Can anyone tell what the issue might be? It doesn't appear to be doing the Sort I requested, could that be the issue?

Also, what is the difference of where you save your macro? Someone taught me to run the macro, save the workbook and then open it and the one you need to run the macro on. Is there a way to save it so it's just always available without having to save a master macro version?

Code:
Sub Macro4()
'
' Macro4 Macro
'
' Keyboard Shortcut: Ctrl+b
'
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Customer Group"
Columns("N:O").Select
Selection.Cut
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
Columns("L:R").Select
Selection.Delete Shift:=xlToLeft
Columns("L:L").Select
Selection.Delete Shift:=xlToLeft
Columns("M:O").Select
Selection.Delete Shift:=xlToLeft
Columns("N:O").Select
Selection.Delete Shift:=xlToLeft
Cells.Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveWorkbook.Worksheets("20110504115602").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("20110504115602").Sort.SortFields.Add Key:=Range( _
"C2:C2845"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("20110504115602").Sort
.SetRange Range("A1:AC2845")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 
Last edited by a moderator:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Welcome to the Board!

When the code occurs what line is highlighted, and what's the error message?

As for where to keep the code, you can put it in Personal.xls. Then it will be available to any open workbook. If you don't have one, just record a new macro and in the Save Macro In choices chose "Personal Macro Workbook". Then stop recording and VBA will have created it for you.

HTH,
 
Upvote 0
Rich (BB code):
Sub Macro6()
'
' Macro6 Macro
'
' Keyboard Shortcut: Ctrl+b
'
    Cells.Select
    With Selection
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("N:O").Select
    Selection.Cut
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "Customer Group"
    Columns("F:F").Select
    Selection.Delete Shift:=xlToLeft
    Columns("L:S").Select
    Selection.Delete Shift:=xlToLeft
    Columns("M:O").Select
    Selection.Delete Shift:=xlToLeft
    Columns("N:O").Select
    Selection.Delete Shift:=xlToLeft
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
    Cells.Select
    ActiveWorkbook.Worksheets("20110504131941").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("20110504131941").Sort.SortFields.Add Key:=Range( _
        "C2:C2845"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("20110504131941").Sort
        .SetRange Range("A1:AC2845")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.AutoFilter
End Sub
 
Upvote 0
This seems to work fine:

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> Macro6()<br><SPAN style="color:#007F00">' Keyboard Shortcut: Ctrl+b</SPAN><br><br>    <SPAN style="color:#00007F">With</SPAN> Cells<br>        .WrapText = <SPAN style="color:#00007F">False</SPAN><br>        .Orientation = 0<br>        .AddIndent = <SPAN style="color:#00007F">False</SPAN><br>        .IndentLevel = 0<br>        .ShrinkToFit = <SPAN style="color:#00007F">False</SPAN><br>        .ReadingOrder = xlContext<br>        .MergeCells = <SPAN style="color:#00007F">False</SPAN><br>    <br>        <SPAN style="color:#00007F">With</SPAN> .Borders<br>            .LineStyle = xlNone<br>            .LineStyle = xlContinuous<br>            .ColorIndex = 0<br>            .TintAndShade = 0<br>            .Weight = xlThin<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br>        .Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove<br>        .Columns("N:O").Cut<br>        .Columns("B:B").Insert Shift:=xlToRight<br>        .Range("A2").Value = "Customer Group"<br>        .Columns("F:F").Delete Shift:=xlToLeft<br>        .Columns("L:S").Delete Shift:=xlToLeft<br>        .Columns("M:O").Delete Shift:=xlToLeft<br>        .Columns("N:O").Delete Shift:=xlToLeft<br>        .Rows("1:1").Delete Shift:=xlUp<br>        <br>        <SPAN style="color:#00007F">With</SPAN> Sheets("20110504131941").Sort<br>            .SortFields.Clear<br>            .SortFields.Add Key:=Range("C2:C2845"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _<br>                xlSortNormal<br>            .SetRange Range("A1:AC2845")<br>            .Header = xlYes<br>            .MatchCase = <SPAN style="color:#00007F">False</SPAN><br>            .Orientation = xlTopToBottom<br>            .SortMethod = xlPinYin<br>            .Apply<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>        <SPAN style="color:#00007F">With</SPAN> .Interior<br>            .Pattern = xlNone<br>            .TintAndShade = 0<br>            .PatternTintAndShade = 0<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br>        .AutoFilter<br>    <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Note that if these changes are supposed to apply to the same sheet that's being sorted you'll want to make that reference at the beginning.
 
Upvote 0
So do I just paste in your code over my macro's code? How do I find the macro when I saved it to the Personal Macro area?
Sorry to sound so naive, I am newer to macro's and this new job so it's a tough spot :(
Thank you for your help, I greatly appreciate it!
 
Upvote 0
I'd change the name of my macro and paste it into a separate module just for testing.

As for Personal.xls, you generally don't put code in it until it's been thoroughly tested.

No worries about being a newb, we all were once (and I prove myself quite incompetent on occasion. ;))
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,921
Latest member
BBQKING

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