Anyone know why the below code halts on range_i.Copy
with error? This is related to this question but you don't need to review that question to know the answer to this one I don't think! :-) Thanks
object variable or with block not set
Sub resort()
Dim wb As Workbook, ws As Worksheet, myrange As Range
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet1")
Set range_i = Nothing
counter = 0
'Find last row
TrE = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'Start loop assuming data starts in row 2 and 13 columns wide as in example
For Tr = 2 To TrE
If Not myrange Is Nothing Then
If ws.Cells(Tr, 13) = 0 Then
Set myrange = Union(myrange, Range(ws.Cells(Tr, 1), ws.Cells(Tr, 13)))
counter = counter + 1
End If
Else
If ws.Cells(Tr, 13) = 0 Then
Set myrange = Range(ws.Cells(Tr, 1), ws.Cells(Tr, 13))
counter = counter + 1
End If
End If
If Not range_i Is Nothing Then
If ws.Cells(Tr, 13) > 0 Then
Set range_i = Union(range_i, Range(ws.Cells(Tr, 1), ws.Cells(Tr, 13)))
End If
Else
If ws.Cells(Tr, 13) > 0 Then
Set range_i = Range(ws.Cells(Tr, 1), ws.Cells(Tr, 13))
End If
End If
Next Tr
'Create summary sheet
Sheets.Add.Name = "summary"
Set Tws = wb.Sheets("summary")
'Copy ranges into new sheet
offset_i = 2 + counter
myrange.Copy
Tws.Range("A2").PasteSpecial
range_i.Copy
Tws.Range(Cells(offset_i, 1), Cells(offset_i, 13)).PasteSpecial
'Now sort the pasted data for range_i
Tws.Range(Cells(offset_i, 1), Cells(TrE - 1, 13)).Sort key1:=Range("A:A"), _
order1:=xlAscending, Header:=xlNo
'Copy the headers as well
ws.Range("A1:M1").Copy
Tws.Range("A1:M1").PasteSpecial
End Sub