Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

macro

ResolvedVersion 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:

How to select certain data in an Excel 2010 workbook

If you want to select the correct data set in a page full of data, the most accurate and efficient way of doing this is to use the ''Shift and Click'' technique.

For example: If you want to select all data in cells A2 to E10, then click on cell A2, hold down the Shift key and click on cell E10 and all the data you want to see is highlighted.

Keep holding down the Shift key and you can move from cell E10 to any other cell in the spreadsheet.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.