从参考文件中复制数据并使副本唯一,然后将这些唯一文本放在新工作表中的列标题循环中。有人知道为什么我在“With wsDB”行中得到错误“下标超出范围”?
Sub uniqueyes()
Dim wsRef As Worksheet
Dim wsDB As Worksheet
Set wsRef = Worksheets("reference1")
Set wsDB = Worksheets("Sheet1")
With wsRef
.Range("F1:F60").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("I1"), unique:=True
Dim arrValues As Variant
arrValues = .Range("I2", .Range("I" & .Rows.Count).End(xlUp))
End With
For i = 1 To ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row
If Cells(i, 4) = "Title" Then
For j = 1 To (UBound(arrValues)) Step 1
With wsDB
.Range(.Cells(i, j * 4 + 2), .Cells(i, j * 4 + 4)).Value = arrValues(j)
End With
Next j
End If
Next i
End Sub
答案 0 :(得分:4)
当你在数组中加载值时,喜欢这样:
arrValues = .Range("I2", .Range("I" & .Rows.Count).End(xlUp))
它变得多维。 (行和列)
在这一行中,您只是提供一个维度(行)为j
并且缺少其他维度(列)。因此错误。
.Range(.Cells(i, j * 4 + 2), .Cells(i, j * 4 + 4)).Value = arrValues(j)