【发布时间】:2021-06-01 17:36:07
【问题描述】:
任何想法为什么我在myrange.Copy 行收到此错误?
谢谢
Sub nsort()
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
Sheets.Add.Name = "summary"
Set Tws = wb.Sheets("summary")
myrange.Copy
Tws.Range("A1").PasteSpecial
range_i.Copy
Tws.Range(Cells(1 + counter, 1), Cells(1 + counter, 13)).PasteSpecial
End Sub
【问题讨论】:
-
你的循环实际上并没有运行,因为你从来没有给
TrE赋值(你注释掉了这样做的行:TrE = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row应该在它自己的行上