我该如何编辑以获取两个标题行?

时间:2018-12-05 13:31:43

标签: vba excel-vba

下面,我有一个脚本的一部分,该脚本将主文件的标题行捕获到数组中,然后再捕获其他数据并将其打印到新的工作表中。

Option Explicit

Sub CIB_Cuts()
    Dim j As Long, k As Long, x As Long
    Dim varArray() As Variant
    Dim varArray2() As Variant
    ReDim varArray(1 To 19, 1 To 1)

            With ThisWorkbook.Worksheets("Data")
                ReDim varArray(1 To UBound(varArray, 1), 1 To 1)
                x = 1
                For k = 1 To UBound(varArray, 1)
                    varArray(k, x) = .Cells(1, k)
                Next

我将在下面进行哪些更改,以使其先提取第一个(19列),然后提取第二行(19列),然后再提取我想要的其他地方的特定数据? (我有两个标题行,目前只能提取其中一个)

我可以将ReDim varArray(1 To 19, 1 To 1)更改为ReDim varArray(1 To 19, 1 To 2吗?还是我必须编辑下面的with语句?

2 个答案:

答案 0 :(得分:1)

将范围粘贴到数组中

这就是您所需要的(无循环)。

Sub CIB_Cuts()
    Dim varArray As Variant

    With ThisWorkbook.Worksheets("Data")
      varArray = .Range("A1:S2")
    End With

这是一个基于一维的二维“水平”数组,即它包含的列多于行。因此,当您遍历它时,它可能看起来像这样:

For j = LBound(varArray, 2) to UBound(varArray, 2) ' more data
  For i = LBound(varArray) to UBound(varArray)
    x = varArray(i, j)
  Next  
Next

代替经典的“垂直”数组循环:

For i = LBound(varArray) to UBound(varArray) ' more data
  For j = LBound(varArray, 2) to UBound(varArray, 2)
    x = varArray(i, j)
  Next
Next

因此,如果要遍历第一行的数据(现在位于数组的第一列),请执行以下操作:

For i = LBound(varArray) to UBound(varArray) ' more data
  x = varArray(1, i)
Next

,或者对于第二行(现在位于数组的第二列),您将执行以下操作:

For i = LBound(varArray) to UBound(varArray) ' more data
  x = varArray(2, i)
Next

完整的代码如下:

Option Explicit

Sub CIBCutsPaste()
    Dim varArray As Variant

    With ThisWorkbook.Worksheets("Data")
      varArray = .Range("A1:S2")
    End With

    ' Print contents to Immediate Window
    With ThisWorkbook.Worksheets("Data")
      Dim str1 As String
      Dim k As Integer
      Dim x As Integer
      For k = 1 To UBound(varArray)         ' 2 Rows
        For x = 1 To UBound(varArray, 2)    ' 19 Columns
          If x = 1 Then
            str1 = str1 & vbCr & varArray(k, x)
           Else
            str1 = str1 & "," & varArray(k, x)
          End If
        Next
      Next
      Debug.Print str1
    End With

End Sub

如果您坚持要循环,即将工作表的行复制到数组的列中,则可能看起来像这样:

Sub CIBCutsLoop()
    k As Long, x As Long
    Dim varArray() As Variant
    ReDim varArray(1 To 19, 1 To 2)

    With ThisWorkbook.Worksheets("Data")
        For k = 1 To UBound(varArray)         ' 19 Rows
            For x = 1 To UBound(varArray, 2)  ' 2 Columns
                varArray(k, x) = .Cells(x, k)
            Next
        Next
    End With

    ' Print contents to Immediate Window
    With ThisWorkbook.Worksheets("Data")
        Dim str1 As String
        For k = 1 To UBound(varArray)         ' 19 Rows
            For x = 1 To UBound(varArray, 2)  ' 2 Columns
                If x = 1 Then
                    str1 = str1 & vbCr & varArray(k, x)
                  Else
                    str1 = str1 & "," & varArray(k, x)
                End If
            Next
        Next
        Debug.Print str1
    End With

End Sub

答案 1 :(得分:0)

您可以适当地使用静态数组和动态数组。 vDB是静态数组。

Dim j As Long, k As Long, x As Long
Dim varArray() As Variant
Dim varArray2() As Variant
Dim vDB As Variant

ReDim varArray(1 To 2, 1 To 19)

        With ThisWorkbook.Worksheets("Data")
            vDB = .Range("a1").Resize(2, 19) '<~~vDB It is itself a two-line header.
            'if you want to pass data to a varArray array
            For k = 1 To UBound(varArray, 1)
                For x = 1 To UBound(varArray, 2)
                    varArray(k, x) = vDB(k, x)
                Next x
            Next

        End With

工作图像。

enter image description here