PDA

View Full Version : Runtime err 1004



Jonjon25
02-28-2013, 04:05 AM
Ahoi!

I am messing with this code (which I nicked off the inet and then adjusted) for far too long now... I need help

It is suppose to run through 20 sheets copy the data i want and post it into one summary

It work like a charm for the first 10 and then gives out an 1004 error

I dont get my head around why though

Can someone help please? Much appreciated

Sub PrepareSOFPuploadCSV()
'
' PrepareSOFPuploadCSV Macro
'
'Sub MergeAllWorkbooks()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
'this how it was before
'Dim SourceRange As Range
'Dim DestRange As Range

'this is what I changed it to: - I am trying to set several source and destination ranges to copy paste only the columns I need
Dim SourceRange As Range
Dim SourceRange2 As Range
Dim SourceRange3 As Range
Dim SourceRange4 As Range
Dim SourceRange5 As Range
Dim DestRange As Range
Dim DestRange2 As Range
Dim DestRange3 As Range
Dim DestRange4 As Range
Dim DestRange5 As Range


'Dim DATstrMthLong As StrinG
'Dim DATstrMthShort As String

'DATstrMthLong = Format((Date - 1), "mmm")
'DATstrMthShort = Format((Date - 1), "mm")
'DATstrYrShort = Format(Date,"yyyy")
'S:\Finance\2012\Pd" & DATstrMthShort & " - " & DATstrMthLong &"\Louise\DB Reporting
'adjusted by JK - the above can filter for the current month in files
Application.ScreenUpdating = False

' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)

' Modify this folder path to point to the files you want to use.
FolderPath = "S:\Finance\2012\Pd11 - Nov\Louise\DB Reporting\"
'this needs adjusting by JK to "S:\Finance\2012\Pd11 - Nov\Louise\DB Reporting\"

' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1

' Call Dir the first time, pointing it to all Excel files in the folder path.

' adjusted from "*.xl*" to "\*NSYS.xls"
FileName = Dir(FolderPath & "\*NSYS.xls")


' Loop until Dir returns an empty string.
Do While FileName <> ""

' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)

' Set the cell in column A to be the file name.
SummarySheet.Range("A" & NRow).Value = FileName

' Set the source range to be A9 through C9.
' Modify this range for your workbooks.
' It can span multiple rows.
'This is how it was
'Set SourceRange = WorkBk.Worksheets(4).Range("E6:E7092,g6:g7092,i6:i7092,p6:P7092,aP6:aP7092") '.SpecialCells(xlCellTypeVisible)
'this is what i changed it to
Set SourceRange = WorkBk.Worksheets(4).Range("E5:E7092") '.SpecialCells(xlCellTypeVisible)
Set SourceRange2 = WorkBk.Worksheets(4).Range("G5:G7092") '.SpecialCells(xlCellTypeVisible)
Set SourceRange3 = WorkBk.Worksheets(4).Range("I5:I7092") '.SpecialCells(xlCellTypeVisible)
Set SourceRange4 = WorkBk.Worksheets(4).Range("P5:P7092") '.SpecialCells(xlCellTypeVisible)
Set SourceRange5 = WorkBk.Worksheets(4).Range("AP5:AP7092") '.SpecialCells(xlCellTypeVisible)

'ActiveCell.Range("A1,A:A,C:C,E:E").Select
'This works to select different Colms use currentregion and count to make sure only data containing cells are copied
'Worksheets (1) this means that the first worksheet in the workbook is selected - this has been adjusted to 4
'Selection.SpecialCells(xlCellTypeVisible).Select - selects only visible cells

' Set the destination range to start at column B and
' be the same size as the source range.

'this is how it was
'Set DestRange = SummarySheet.Range("B" & NRow)

'this is what i changed it to
Set DestRange = SummarySheet.Range("B" & NRow)
Set DestRange2 = SummarySheet.Range("C" & NRow)
Set DestRange3 = SummarySheet.Range("D" & NRow)
Set DestRange4 = SummarySheet.Range("E" & NRow)
Set DestRange5 = SummarySheet.Range("F" & NRow)

'this is how it was
'Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)

'this is what i changed it to
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)

Set DestRange2 = DestRange2.Resize(SourceRange2.Rows.Count, _
SourceRange2.Columns.Count)

Set DestRange3 = DestRange3.Resize(SourceRange3.Rows.Count, _
SourceRange3.Columns.Count)

Set DestRange4 = DestRange4.Resize(SourceRange4.Rows.Count, _
SourceRange4.Columns.Count)

Set DestRange5 = DestRange5.Resize(SourceRange5.Rows.Count, _
SourceRange5.Columns.Count)



' Copy over the values from the source to the destination.


'this is how it was
'DestRange.Value = SourceRange.Value

'this is what I changed it to
DestRange.Value = SourceRange.Value
DestRange2.Value = SourceRange2.Value
DestRange3.Value = SourceRange3.Value
DestRange4.Value = SourceRange4.Value
DestRange5.Value = SourceRange5.Value


' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count

' Close the source workbook without saving changes.
WorkBk.Close Savechanges:=False


' Use Dir to get the next file name.
FileName = Dir()


Loop

' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
Application.ScreenUpdating = True
'
End Sub