macro

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Macro

Macro

resolvedResolved · High Priority · Version 2010

Lee has attended:
Excel Advanced course

Macro

Hi

We receive raw data via email in excel 2010 several times a day. We go through the same process with each data file so we used a macro to record the process. Our macro only ever works on the original sheet it was recorded on despite it being saved in our personal workbook. We can see where the error is when we debug "runtime error 9", but we dont know what steps to take to resolve the isssue.

Each workbook we receive has already been named with our clients reference, therefore when we try and run the macro on another clients data (whom has a different client reference) the macro comes with the error.

The code below is the error we receive (ActiveWorkbook.Worksheets("1241014").Sort.SortFields.Clear), the problem is the ("1241014") as each workbook has been sent to us with a different number. I hope you can help as I'm near pulling out the little hair I have left :).

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+q
'
Cells.Select
Cells.EntireColumn.AutoFit
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft
Columns("C:F").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
ActiveCell.FormulaR1C1 = "Date"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Desc"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Out"
Range("D1").Select
ActiveCell.FormulaR1C1 = "In"
Columns("A:D").Select
ActiveWorkbook.Worksheets("1241014").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("1241014").Sort.SortFields.Add Key:=Range("A2:A621" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("1241014").Sort
.SetRange Range("A1:D621")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub

Thanks
Lee

RE: Macro

Hi Lee,

Thank you for the question.

If the data always are in the first worksheet of the workbook you you can change the code from Worksheets("1241014") to WorkSheet(1) (WorkSheet(2) if the data always are in Sheet 2)

If the data are not in same WorkSheet number you can create a variable.

If you at the top of the macro type:

SheetName=inputbox("Enter the name of the sheet"),

and then change everytime you have Worksheets("1241014") to Worksheets(SheetName).

This will open a inputbox where you have to type the name of the worksheet everytime you run the macro.

I hope this will solve your problem.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Read more: https://www.stl-training.co.uk/post-34560-formula-shows-quarters.html #ixzz2wE8kbKnu

Thu 24 Apr 2014: Automatically marked as resolved.


 

Excel tip:

Printing spreadsheets without opening them first

Here's a fast way of printing a spreadsheet from Windows Explorer/My Computer.

Go to the location where the spreadsheet is saved on your computer, then right-click on the icon next to the document and select Print from the menu.

The spreadsheet will automatically open, print and close itself.

View all Excel hints and tips


Server loaded in 0.08 secs.