如何引用数组变量中的所有元素

时间:2019-05-09 02:23:58

标签: arrays excel vba element

我希望能够引用数组变量中的所有元素。

这可能吗?

'For example, if I did this:
YD = "=COUNTIFS('Sheet1'!C2,M(k,i),'Sheet1'!C4,""*""&F(l))"

'I want 'Sheet1'!C2 & 'Sheet1'!C4 to be arrays upon which I refer to all _
'the elements in that array.

另外,我如何声明一个公共数组变量供所有子程序使用?

Option Explicit

Public mcco As Worksheet
Public mcfc As Worksheet
Public mcfb As Worksheet
Public mcfv As Worksheet
Public CVTR As Long
Public FCBR As Long
Public FBCC As Long
Public FCMC As Long
Public MBSA As Variant
Public FTNA As Variant
Public FTVA As Variant
Dim i As Long
Dim j As Long
Dim k As Long
Dim l As Long
Dim s As Long
Dim XD
Dim YD
Sub MatrixSet()

Set mcco = Workbooks("PERSONAL.xlsb").Worksheets("Sheet1")
Set mcfc = Workbooks("PERSONAL.xlsb").Worksheets("Sheet2")
Set mcfb = Workbooks("PERSONAL.xlsb").Worksheets("Sheet3")
Set mcfv = Workbooks("PERSONAL.xlsb").Worksheets("Sheet4")
'mcco = Sheet1
'mcfc = Sheet2
'mcfb = Sheet3
'mcfv = Sheet4

'Array Variables
' maybe use this function to do these two
'Join( SourceArray, [Delimiter] )
''Sheet1'!C2
''Sheet1'!C4

CVTR = mcco.Cells(Rows.Count, 2).End(xlUp).Row
FCBR = mcfc.Cells(Rows.Count, 2).End(xlUp).Row - 1
FBCC = Application.WorksheetFunction.Max(mcfc.Columns(6)) + 1
FCMC = mcfc.Cells(1, Columns.Count).End(xlToLeft).Column
'CVTR = Shee1 Row size
'FCBR = Sheet2 & Sheet3 Row Size
'FBCC = Sheet3 Max Columns. Must be done after calculating Column 6
'FCMR = Sheet2 Max Column

''Sheet3'!R" & i & "C" & k & " = MBSA(k,i)
MBSA = WorksheetFunction.Application.Transpose(Range(mcfb.Cells(1, 1), mcfb.Cells(FCBR, FBCC)))
'MBSA = Sheet2 & Sheet3 Array

FTNA = WorksheetFunction.Application.Transpose(WorksheetFunction.Application.Transpose(Range(mcfc.Cells(1, 1), mcfc.Cells(1, FCMC))))
'Use this in place of 'Sheet2'!R1C" & j & "

    For l = 3 To FCMC
        XD = "=SUBSTITUTE(SUBSTITUTE(MID(""" & FTNA(l) & """,FIND(""*"",SUBSTITUTE(""" & FTNA(l) & """,""("",""*"",(LEN(""" & FTNA(l) & """) - LEN(SUBSTITUTE(""" & FTNA(l) & """,""("","""")))))+1,LEN(""" & FTNA(l) & """)),"")"",""""),""("","""")"
        Cells(l, 1).Formula = XD
        mcfv.Cells(l, 1).Value = mcfv.Cells(l, 1).Value
    Next l

FTVA = WorksheetFunction.Application.Transpose(Range(mcfv.Cells(1, 1), mcfv.Cells(FCMC, 1)))
'Use this in place of 'Sheet2'!R1C" & j & "

For i = 2 To CVTR
    For l = 3 To FCMC
        For k = 2 To FBCC
            'I need Sheet1C2 to be an array. How?
            YD = "=COUNTIFS('Sheet1'!C2,""" & MBSA(k,i) & """,'Sheet1'!C4,""*""&""" & FTVA(l) & """)"
        Next k
    Next l
Next i

Stop

End Sub

如上所述,我试图使用YD来对Sheet2列2中的所有行以及数组MBSA中的值进行计数。但是,我也希望工作表2的第2列也成为数组,因为这会使处理过程更快。我该怎么做?

0 个答案:

没有答案