【发布时间】:2018-08-04 04:02:44
【问题描述】:
我有一个宏,它允许我根据文件名打开多个文件并根据条件复制工作表(如果“X”列中有值,则复制行但只复制一些列“F、G、P、 Q,W,X,Y) 到另一个独特的工作簿。 问题出在 F 列中,我有颜色,我想检索颜色索引,但宏将其留空
[1] Get data from A1:Z{n}
n = ws.Range("A" & Rows.Count).End(xlUp).Row ' find last row number n
v = ws.Range("A10:Y" & n).Value2 ' get data cols A:Y and omit header row
[2] build array containing found rows
a = buildAr2(v, 24) ' search in column X = 24
' [3a] Row Filter based on criteria
v = Application.Transpose(Application.Index(v, _
a, _
Application.Evaluate("row(1:" & 26 & ")"))) ' all columns from A to Z
[3b] Column Filter F,G,P,Q,W,X,Y
v = Application.Transpose(Application.Transpose(Application.Index(v, _
Application.Evaluate("row(1:" & UBound(a) - LBound(a) + 1 & ")"), _
Array(6, 7, 16, 17, 23, 24, 25)))) ' only cols F,G,P,Q,W,X,Y
Function buildAr2(v, ByVal vColumn&, Optional criteria) As Variant
' Purpose: Helper function to check in Column X
' Note: called by main function MultiCriteria in section [2]
Dim found&, found2&, i&, j&, n&, ar: ReDim ar(0 To UBound(v) - 1)
howMany = 0 ' reset boolean value to default
For i = LBound(v) To UBound(v)
If Len(Trim(v(i, vColumn))) > 0 Then
ar(n) = i
n = n + 1
End If
Next i
If n < 2 Then
howMany = n: n = 2
Else
howMany = n
End If
ReDim Preserve ar(0 To n - 1)
buildAr2 = ar
End Function
【问题讨论】:
-
“我想检索颜色索引,但宏将其留空” 我在您的代码中没有看到您尝试检索颜色索引的位置。请查看如何提供minimal reproducible example。
-
@Pᴇʜ 我只是在这里复制数组中的值,我不知道怎么说这个特定列复制颜色索引而不是它的值
-
请添加带有示例数据的minimal reproducible example 以供输入。
-
v = ws.Range("A10:Y" & n).Value2此方法只会复制值。另一种方法是将整个数据复制到数组中,如果您有特定的单元格将具有颜色,那么您可以直接替换数组中的相关值。 -
源代码提示:稍微缩短的代码基于我对Multi criteria selection with VBA 的回答,我在其中评论说分配给(数据字段)数组的(范围)值不包含格式信息并引用明确向 MCVE。