|
-
Sep 30th, 2012, 07:11 PM
#1
Thread Starter
New Member
Getting Runtime error 1004: Application Defined or object defined error
HI,
I am using following code to copy multiple sheets into single sheet and I am getting Run time error 1004:
Could please help me to resolve this issue: I have highlighted the line....
Sub CopyFromWorksheets()
Dim wrk As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Master Worksheet
Dim rng As Range 'Range object
Dim colCount As Integer 'Column count in tables in the worksheets
Set wrk = ActiveWorkbook 'Working in active workbook
For Each sht In wrk.Worksheets
If sht.Name = "Master" Then
MsgBox "There is a worksheet called as 'Master'." & vbCrLf & _
"Please remove or rename this worksheet since 'Master' would be" & _
"the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
Exit Sub
End If
Next sht
'We don't want screen updating
Application.ScreenUpdating = False
'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
'Rename the new worksheet
trg.Name = "Master"
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Worksheets(1)
colCount = sht.Cells(1, 255).End(xlToLeft).Column
'Now retrieve headers, no copy&paste needed
With trg.Cells(1, 1).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value
'Set font as bold
.Font.Bold = True
End With
'We can start loop
For Each sht In wrk.Worksheets
'If worksheet in loop is the last one, stop execution (it is Master worksheet)
If sht.Index = wrk.Worksheets.Count Then
Exit For
End If
'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
'Put data into the Master worksheet
trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
Next sht
'Fit the columns in Master worksheet
trg.Columns.AutoFit
'Screen updating should be activated
Application.ScreenUpdating = True
End Sub
-
Oct 1st, 2012, 02:37 AM
#2
New Member
Re: Getting Runtime error 1004: Application Defined or object defined error
It looks like you're going to the last row available and then trying to go one past it, causing an error. Your best bet at debugging it, however, is to split that complex statement up into its constituents, one per line - that way you can see at which point in the statement it is failing.
-
Oct 1st, 2012, 10:27 AM
#3
Thread Starter
New Member
Re: Getting Runtime error 1004: Application Defined or object defined error
I did debugging, and for rng.value it is giving me proper count.
I checked rng.Rows.Count values -- this is also correct and for rng.Columns.Count - its value also correct.
I think error is in trg.Cells(65536, 1).End(xlUp).Offset(1)
It is copying till 14th row and then i t copied values for first 3 columns and then it stopped.
is there any better way to use other than this expression.
Last edited by soniamalhotra; Oct 1st, 2012 at 10:29 AM.
Reason: adding more commetns
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|