Reputation: 281
I wrote some code which works perfectly as it should when I debug it. But when I remove the breakpoint and just run the code, it give a runtime error:
runtime error '1004' Method Range of object_worksheet failed.
It refers to the next line:
Set copyrange = sh.Range("A" & i & ":E" & i & ",I" & i & ":O" & i & ",Q" & i & ",V" & i) 'name column in sheet = Q
But while debugging it, there isn't a problem. Maybe the cache is full?
Private Sub btnGetDevices_Click()
'open every sheet after summary
'copy columns A,B,C,D,E,I,J,K,L,M,N,O, Q,V to summary
Dim sh As Worksheet
Dim copyrange As Range
Application.ScreenUpdating = False
Sheets("Summary").Rows(4 & ":" & Sheets("Summary").Rows.Count).Delete
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> "Database" And sh.Name <> "Template" And sh.Name <> "Help" And sh.Name <> "OVERVIEW" And sh.Name <> "Develop" And sh.Name <> "Schedule" And sh.Name <> "Information" And sh.Name <> "Announcements" And sh.Name <> "Summary" Then
sh.Select
LastRow = ActiveSheet.Range("L1048555").End(xlUp).Row
For i = 14 To LastRow
If sh.Range("Q" & i).Value <> Empty And sh.Range("N" & i).Value <> "Designer" And sh.Range("O" & i).Value <> "Layouter" Then
Set copyrange = sh.Range("A" & i & ":E" & i & ",I" & i & ":O" & i & ",Q" & i & ",V" & i) 'name column in sheet = Q
NameDevice = sh.Range("Q" & i).Value
adressDevice = sh.Range("Q" & i)
copyrange.Copy
Sheets("Summary").Select
LastRowsummary = ActiveSheet.Range("A1048555").End(xlUp).Row
Range("B" & LastRowsummary + 1).Select
ActiveSheet.Paste
Range("A" & LastRowsummary + 1) = sh.Name
Range("A" & LastRowsummary + 1, "O" & LastRowsummary + 1).Borders.LineStyle = xlContinuous
Sheets("Summary").Hyperlinks.Add anchor:=Sheets("Summary").Range("N" & LastRowsummary + 1), Address:="", SubAddress:="'" & sh.Name & "'!A1", TextToDisplay:=NameDevice
End If
Next
End If
Next
Application.ScreenUpdating = True
Sheets("Summary").Activate
End Sub
*edit: After some testing I noticed that the error is gone when I use a full range of columns instead of only some columns.
with error:
Set copyrange = sh.Range("A" & i & ",V" & i)
w/o error:
Set copyrange = sh.Range("A" & i & ":E" & i)
*second edit:
I'm using the code from 'Tim Williams'. There was the same error. Instead of using:
rw.Range("A1:E1,I1:O1,Q1,V1").Copy rng.Offset(0, 1)
I've found a workaround. I split it up.
rw.Range("I1:O1").Copy rng.Offset(0, 6)
rw.Range("Q1").Copy rng.Offset(0, 13)
rw.Range("V1").Copy rng.Offset(0, 14)
Now this works without error. But if anyone knows what causes the problem, you may always share it. Thanks in advance.
*third edit:
I still don't know why it doesn't work. It has something to do with range from different columns. The funny (and very frustrated part) is that I use range this way in another sheet and there I don't have this problem. It is driving me mad. Does someone have an idea?
Upvotes: 0
Views: 585
Reputation: 166126
Compiled but not tested"
Private Sub btnGetDevices_Click()
'open every sheet after summary
'copy columns A,B,C,D,E,I,J,K,L,M,N,O, Q,V to summary
Dim sh As Worksheet, shtsumm As Worksheet
Dim copyrange As Range, arrExclude, rw As Range
Dim lastRow As Long, i As Long, rng As Range
Dim NameDevice, adressDevice
'sheets to ignore
arrExclude = Array("Database", "Template", "Help", "OVERVIEW", _
"Develop", "Schedule", "Information", "Announcements", _
"Summary")
Set shtsumm = Sheets("Summary")
Application.ScreenUpdating = False
shtsumm.Rows(4 & ":" & shtsumm.Rows.Count).Delete
For Each sh In ActiveWorkbook.Worksheets
If IsError(Application.Match(sh.Name, arrExclude, 0)) Then
lastRow = sh.Cells(sh.Rows.Count, "L").End(xlUp).Row
For i = 14 To lastRow
Set rw = sh.Rows(i)
If rw.Cells(1, "Q").Value <> Empty And _
rw.Cells(1, "N").Value <> "Designer" And _
rw.Cells(1, "O").Value <> "Layouter" Then
NameDevice = rw.Range("Q1").Value
adressDevice = rw.Range("Q1").Value '<<<typo ?
'find destination
Set rng = shtsumm.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
rng.Value = sh.Name
'Here Range is relative to *rw*, not to the whole sheet
rw.Range("A1:E1,I1:O1,Q1,V1").Copy rng.Offset(0, 1)
rng.Resize(1, 15).Borders.LineStyle = xlContinuous
shtsumm.Hyperlinks.Add _
anchor:=rng.EntireRow.Cells(1, "N"), _
Address:="", SubAddress:="'" & sh.Name & "'!A1", _
TextToDisplay:=NameDevice
End If
Next
End If
Next
Application.ScreenUpdating = True
shtsumm.Activate
End Sub
Upvotes: 1