Human_doing
Board Regular
- Joined
- Feb 16, 2011
- Messages
- 137
Hi all,
Can anyone please assist with rectifying the 'Run time error 1004' I get when running this VBA. I have a very basic understanding of what causes this but it but can't figure out the exact coding issue.
The purpose of the macro is to allow the user to select a workbook from which sheet 1 is copied to an all-new workbook (first part of code), then the second part of the code copies cells from the range on the newly created sheet 1 to different worksheets in the workbook. The second part of the code works when I run it independently so I'm not sure what the issue is when run altogether?
Any help much appreciated,
Thanks
Can anyone please assist with rectifying the 'Run time error 1004' I get when running this VBA. I have a very basic understanding of what causes this but it but can't figure out the exact coding issue.
The purpose of the macro is to allow the user to select a workbook from which sheet 1 is copied to an all-new workbook (first part of code), then the second part of the code copies cells from the range on the newly created sheet 1 to different worksheets in the workbook. The second part of the code works when I run it independently so I'm not sure what the issue is when run altogether?
Any help much appreciated,
Thanks
Code:
Sub TestIt()
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the file with raw data for the report")
If NewFN = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file"
Exit Sub
Else
Workbooks.Open Filename:=NewFN
End If
Cells.Select
Selection.Copy
Workbooks.Add
Range("a1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
Dim lastrow As Long, LastCol As Integer, i As Long, iStart As Long, iEnd As Long
Dim ws As Worksheet
Application.ScreenUpdating = False
With ActiveSheet
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
.Range(.Cells(2, 1), Cells(lastrow, LastCol)).Sort Key1:=Range("A2"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
iStart = 2
For i = 2 To lastrow
If .Range("A" & i).Value <> .Range("A" & i + 1).Value Then
iEnd = i
Sheets.Add After:=Sheets(Sheets.Count)
Set ws = ActiveSheet
On Error Resume Next
ws.Name = .Range("A" & iStart).Value
On Error GoTo 0
ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
With ws.Rows(1)
.HorizontalAlignment = xlCenter
With .Font
.ColorIndex = 5
.Bold = True
End With
End With
.Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy Destination:=ws.Range("A2")
iStart = iEnd + 1
End If
Next i
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Last edited: