VBA 没有读取数组中的所有数据
VBA not reading all data in array
我在第一列比较两个 sheet 然后从第 10 列获取值 column.But 它不会循环遍历数据 sheet.
的所有值
Sub Summarize()
Dim i, n As Long, ws1 As Worksheet, ws2 As Worksheet, data, client, mainsht
Set ws1 = ThisWorkbook.sheets("Sheet1")
Set ws2 = ThisWorkbook.sheets("Sheet2")
data = ws2.Range("A2:G" & ws1.Cells(Rows.Count, 2).End(xlUp).Row).Value 'data to array
For n = 2 To ws1.Range("A2").End(xlDown).Row 'loop over rows in the mainsheet (sheet1)
For i = 1 To UBound(data, 1) 'loop over rows in the array in data (sheet2)
Debug.Print data(i, 1)
If ws1.Cells(n, 1) = data(i, 1) Then 'if client name from mainsheet matches to data sheet
ws1.Cells(n, 10) = ws1.Cells(n, 10) & " " & data(i, 2) & ","
End If
Next
Next
End Sub
这可能有助于解决我在您的代码中发现的几个问题。
- 在单独的行中声明每个变量,并尽可能靠近首次使用的位置
你的线路
Dim i, n As Long, ws1 As Worksheet, ws2 As Worksheet, data, client, mainsht
将 i
声明为 Variant
,将 n
声明为 Long
,以及 data
、client
和 mainsht
全部为 Variants
。这可能不是你想要的。
- 始终非常清楚您引用的是哪个工作簿、作品sheet或范围
这一行
data = ws2.Range("A2:G" & ws1.Cells(Rows.Count, 2).End(xlUp).Row).Value
使用当前有效工作sheet中的Rows.Count
。这可能是也可能不是 ws2
作品sheet。所以要小心清楚 link 参考资料。
- 由于您的评论提到了主要 sheet,因此请将其用作变量名。通常,为您的变量使用描述性名称以使您的代码更好地“阅读”(即 self-documenting)。
下面的这个例子展示了如何组合这些点。不确定它是否回答了你的问题,但它确实解决了那些潜在的问题。
Option Explicit
Sub Summarize()
Dim mainWS As Worksheet
Dim dataWS As Worksheet
Set mainWS = ThisWorkbook.Sheets("Sheet1")
Set dataWS = ThisWorkbook.Sheets("Sheet2")
'--- copies the data into a memory-based array
Dim dataLastRow As Long
Dim data As Variant
With dataWS
dataLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
data = dataWS.Range(.Cells(2, 1), .Cells(dataLastRow, "G")).Value
End With
Dim mainLastRow As Long
With mainWS
mainLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim n As Long
Dim i As Long
For n = 2 To mainLastRow
For i = 1 To UBound(data, 1)
Debug.Print data(i, 1)
If .Cells(n, 1).Value = data(i, 1) Then
.Cells(n, 10).Value = .Cells(n, 10).Value & " " & data(i, 2) & ","
End If
Next i
Next n
End With
End Sub
我在第一列比较两个 sheet 然后从第 10 列获取值 column.But 它不会循环遍历数据 sheet.
的所有值Sub Summarize()
Dim i, n As Long, ws1 As Worksheet, ws2 As Worksheet, data, client, mainsht
Set ws1 = ThisWorkbook.sheets("Sheet1")
Set ws2 = ThisWorkbook.sheets("Sheet2")
data = ws2.Range("A2:G" & ws1.Cells(Rows.Count, 2).End(xlUp).Row).Value 'data to array
For n = 2 To ws1.Range("A2").End(xlDown).Row 'loop over rows in the mainsheet (sheet1)
For i = 1 To UBound(data, 1) 'loop over rows in the array in data (sheet2)
Debug.Print data(i, 1)
If ws1.Cells(n, 1) = data(i, 1) Then 'if client name from mainsheet matches to data sheet
ws1.Cells(n, 10) = ws1.Cells(n, 10) & " " & data(i, 2) & ","
End If
Next
Next
End Sub
- 在单独的行中声明每个变量,并尽可能靠近首次使用的位置
你的线路
Dim i, n As Long, ws1 As Worksheet, ws2 As Worksheet, data, client, mainsht
将 i
声明为 Variant
,将 n
声明为 Long
,以及 data
、client
和 mainsht
全部为 Variants
。这可能不是你想要的。
- 始终非常清楚您引用的是哪个工作簿、作品sheet或范围
这一行
data = ws2.Range("A2:G" & ws1.Cells(Rows.Count, 2).End(xlUp).Row).Value
使用当前有效工作sheet中的Rows.Count
。这可能是也可能不是 ws2
作品sheet。所以要小心清楚 link 参考资料。
- 由于您的评论提到了主要 sheet,因此请将其用作变量名。通常,为您的变量使用描述性名称以使您的代码更好地“阅读”(即 self-documenting)。
下面的这个例子展示了如何组合这些点。不确定它是否回答了你的问题,但它确实解决了那些潜在的问题。
Option Explicit
Sub Summarize()
Dim mainWS As Worksheet
Dim dataWS As Worksheet
Set mainWS = ThisWorkbook.Sheets("Sheet1")
Set dataWS = ThisWorkbook.Sheets("Sheet2")
'--- copies the data into a memory-based array
Dim dataLastRow As Long
Dim data As Variant
With dataWS
dataLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
data = dataWS.Range(.Cells(2, 1), .Cells(dataLastRow, "G")).Value
End With
Dim mainLastRow As Long
With mainWS
mainLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim n As Long
Dim i As Long
For n = 2 To mainLastRow
For i = 1 To UBound(data, 1)
Debug.Print data(i, 1)
If .Cells(n, 1).Value = data(i, 1) Then
.Cells(n, 10).Value = .Cells(n, 10).Value & " " & data(i, 2) & ","
End If
Next i
Next n
End With
End Sub