我在另一篇文章中回答了类似的问题,稍微修改了一下。为您的情况定制
Sub testing()
Dim resultWs As Worksheet
Dim ws As Worksheet
Dim dataArray As Variant
Dim height As Long
Dim currentHeight As Long
Dim wsName As String
Set resultWs = Worksheets("Productlist")
For Each ws In Worksheets
If InStr(ws.Name, "Contract") Then
With ws
wsName = .Name
height = .Cells(1, 1).End(xlDown).Row 'look til empty row
If height > 1048575 Then
height = 1
End If
ReDim dataArray(1 To height, 1 To 1)
dataArray = .Range(.Cells(1, 1), .Cells(height, 1)).Value
End With
With resultWs
currentHeight = .Cells(.Rows.Count, 1).End(xlUp).Row
If .Cells(1, 1) = "" Then
currentHeight = 0
End If
If VarType(dataArray) <> vbDouble Then
.Range(.Cells(currentHeight + 1, 1), .Cells(currentHeight + UBound(dataArray, 1), 1)).Value = wsName
.Range(.Cells(currentHeight + 1, 2), .Cells(currentHeight + UBound(dataArray, 1), 2)).Value = dataArray
Else
.Cells(currentHeight + 1, 1).Value = wsName
.Cells(currentHeight + 1, 2).Value = dataArray
End If
End With
End If
Next ws
End Sub