excel vba如何将多个非连续范围的值复制到数组中

时间:2012-11-06 08:10:11

标签: excel-vba vba excel

我试图将多个非连续范围的值复制到数组中。我写了这样的代码:

summaryTempArray = .range("A2:D9,A11:D12,A14:D15").Value

但它只复制第一部分(A2:D9)。然后,我尝试了以下操作,我得到错误 - “对象_全局失败的方法联盟” - 我使用联合的方式有什么错误吗?

summaryTempArray = Union(.range("A2:D9"), .range("A11:D12"), .range("A14:D15")).Value

4 个答案:

答案 0 :(得分:10)

不知道你的union出了什么问题,但它会产生相同的范围,这是你在第一次尝试中所说的。

问题是,你现在有多个领域。你可以,据我所知,现在必须解决这个问题。

这是一个示例,它将在所有区域的数组中解析,而不是单独添加每个单元格,而是将每个区域分别添加到摘要数组中:

Public Sub demo()
  Dim summaryTempArray() As Variant
  Dim i As Long

  With Tabelle1
    ReDim summaryTempArray(1 To .Range("A2:D9,A11:D12,A14:D15").Areas.Count)

    For i = 1 To .Range("A2:D9,A11:D12,A14:D15").Areas.Count
      summaryTempArray(i) = .Range("A2:D9,A11:D12,A14:D15").Areas(i)
    Next i
  End With

End Sub

希望这有帮助。

答案 1 :(得分:2)

我相信,如果将源范围放入数组很重要,那么Jook的解决方案就像您将获得的一样好。但是,我认为解决方案应包括从不规则数组中提取值的说明。这并不难,但语法模糊不清。

我也无法让你的Union声明失败。我假设有一些关于导致失败的上下文,我无法复制。

下面的代码显示两个范围相同,并且只有第一个子范围被加载到您报告的数组中。它完成了另一种可能令人满意的方法。

Option Explicit
Sub Test()

  Dim CellValue() As Variant
  Dim rng As Range

  With Worksheets("Sheet1")

    Set rng = .Range("A2:D9,A11:D12,A14:D15")
    Debug.Print rng.Address
    Set rng = Union(.Range("A2:D9"), .Range("A11:D12"), .Range("A14:D15"))
    Debug.Print rng.Address
    ' The above debug statements show the two ranges are the same.

    Debug.Print "Row count " & rng.Rows.Count
    Debug.Print "Col count " & rng.Columns.Count
    ' These debug statements show that only the first sub-range is included the
    ' range counts.

    CellValue = rng.Value

    Debug.Print "Rows " & LBound(CellValue, 1) & " to " & UBound(CellValue, 1)
    Debug.Print "Cols " & LBound(CellValue, 2) & " to " & UBound(CellValue, 2)
    ' As you reported only the first range is copied to the array.

    rng.Copy Destination:=Worksheets("Sheet2").Range("A1")
    ' This shows you can copy the selected sub-ranges.  If you can copy the
    ' required data straight to the desired destination, this might be a
    ' solution.

  End With

End Sub

答案 2 :(得分:0)

我遇到了同样的问题,尝试了几种方法,但都没有成功,直到遇到这个问题:-

private func setupNavigationBar() {
    //Navigation Bar
    let attrs: [NSAttributedStringKey: Any] = [
        NSAttributedStringKey.foregroundColor: UIColor.white,
        NSAttributedStringKey.font: FontFamily.Muller.medium.font(size: 17)
    ]
    UIApplication.shared.statusBarStyle = .lightContent
    UINavigationBar.appearance().isTranslucent = false
    UINavigationBar.appearance().backgroundColor = ColorName.mainBlue.color
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = attrs

    let barButtonItemAppearance = UIBarButtonItem.appearance()
    barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.font: FontFamily.Muller.regular.font(size: 15)], for: .normal)
    barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.font:
        FontFamily.Muller.regular.font(size: 15)], for: .highlighted)
}

如果这是一种“错误的”转换方法,我会很感兴趣。

答案 3 :(得分:0)

可以从非并发单元格范围创建多维数组。我所做的是将上面的一些代码用于范围复制机制,我学到了两件事:使用该方法,您不仅可以引用实际单元格,还可以引用数据,还可以移动和保留顺序。在我的个人项目中,我们必须使用一些excel文件来填写校准数据。它运行计算并生成校准记录报告,以供我们的文件稍后参考。这些库存文件很无聊!我想把它整理一下,并根据校准是否通过对大多数文档上空的单元格上色。这些文件将各个检查步骤分开,因此我要查看的范围并不总是相邻的。我想到的是使用下面的复制功能创建一个新工作表,并将所有非并行范围粘贴到一组不错的并发范围中,然后让我的数组查看新工作表以绘制表格。我让它运行所需的查找,然后删除现在无用的工作表。

Public Sub ColorMeCrazy()


' First Declare your variables that you will need line notes will be added to all the ones for just the array problem
Dim chkarray As Variant

Dim i As Integer ' for the array lookup loop
Dim j As Integer ' also for the array lookup loop

Dim chk1 As Boolean
Dim chk2 As Boolean

Dim cpyrange As Range ' the non-concurrent range collector haha.
Dim cz As Range

chk2 = True

Set cz = Worksheets("AN_PRM-36").Range("A1:I1,C2:I2,I3:I35,A30:H32,D33:H35,C34:C35,A36:E36,A22:H23,D24:H24,A16:H16,A8:H9,D10:H10")

' the next item below sets the ranges i wish to use. see that they arent all just right next to eachother.
Set cpyrange = Worksheets("AN_PRM-36").Range("G7:H7,G15:H15,G21:H21,G28:H29")

' this is the new sheet i made to create the array with
Sheets.Add.Name = "AN_PRM-36tmp"

' the data gets coppied to the new sheet but now its all together
cpyrange.Copy Destination:=Worksheets("AN_PRM-36tmp").Range("A1")

' now i tell the array i want it to use the data on the new sheet
chkarray = Worksheets("AN_PRM-36tmp").Range("A1:B5")

'this was my look up for the nonsense that i wanted to do later
For i = LBound(chkarray, 1) To UBound(chkarray, 1)
        
    For j = LBound(chkarray, 2) To UBound(chkarray, 2)
        
        Debug.Print chkarray(i, j)
            If chkarray(i, j) = "Pass" Then
            chk1 = True
            Else
            chk2 = False
            End If
    Next
Next

If chk1 = True And chk2 = True Then
cz.Interior.ColorIndex = 4

Else
cz.Interior.ColorIndex = 3

End If

' this last bit will get rid of the new sheet and not ask you are you sure you want it gone.
Application.DisplayAlerts = False
Sheets("AN_PRM-36tmp").Delete
Application.DisplayAlerts = True


End Sub