在源范围内调用的单元数是否有限制?

时间:2017-09-06 21:19:48

标签: vba

我拼凑了一个可视化的基本代码,它从多个工作簿中的单元格中提取数据,并创建一个主要的摘要工作簿。我想在每个工作簿中包含大量单元格,但如果在源范围中包含太多单元格,则Sub停止工作。下面的第一部分代码工作正常。如果我添加更多单元格(第二部分代码),Sub仍会运行,但会生成一个空白工作簿。在这两种情况下,其余代码都是相同的。我可以在命令中包含最大数量的单元格/字符吗?即便如此,我认为这不会超过这样的限制。

这个代码很精细

 With mybook.Worksheets(1)
                Set sourceRange = .Range("J2, C2, D7, F7, K7, G10, J10, G11, J11, G12, J12," _
                & "G14, J14, G15, J15, G16, J16, G17, J17, J21," _
                & "J2, D24:E24, G24, I24, J24, O24:S24," _
                & "D25:E25, G25, I25, J25, O25:S25," _
                & "D30:E30, G30, I30, J30, O30:S30")

            End With

这个代码返回一个空白的工作表

With mybook.Worksheets(1)
                Set sourceRange = .Range("J2, C2, D7, F7, K7, G10, J10, G11, J11, G12, J12," _
                & "G14, J14, G15, J15, G16, J16, G17, J17, J21," _
                & "J2, D24:E24, G24, I24, J24, O24:S24," _
                & "D25:E25, G25, I25, J25, O25:S25," _
                & "D26:E26, G26, I26, J26, O26:S26," _
                & "D27:E27, G27, I27, J27, O27:S27," _
                & "D28:E28, G28, I28, J28, O28:S28," _
                & "D29:E29, G29, I29, J29, O29:S29," _
                & "D30:E30, G30, I30, J30, O30:S30")

            End With

我尝试过使用不同的细胞并遇到同样的问题;它似乎是我选择的单元格数量,而不是原始工作簿中的单元格数量,这就是问题所在。

2 个答案:

答案 0 :(得分:3)

"A1, B1, C1"语法只是Range.Union的简写 - 使用显式Range.Union来电,天空就是极限!

答案 1 :(得分:1)

不是一个答案......只是一个提示

简化您的范围名称

With mybook.Worksheets(1)
    Set SourceRange = .Range("C2, J2," _
                           & "D7, F7, K7," _
                           & "G10:G12, J10:J12," _
                           & "G14:G17, J14:J17," _
                           & "J21," _
                           & "D24:E30, G24:G30, I24:J30, O24:S30")
End With
SourceRange.Select
Stop                 ' look at your worksheet here to see if the range is correct  ... comment out two lines after verification

如果您要在其他任何地方引用mybook.worksheets(1),那么定义一个对象变量并使用它,就像这样

Dim ws1 As Worksheet
Set ws1 = mybook.Worksheets(1)

Set SourceRange = ws1.Range("C2, J2," _
                          & "D7, F7, K7," _
                          & "G10:G12, J10:J12," _
                          & "G14:G17, J14:J17," _
                          & "J21," _
                          & "D24:E30, G24:G30, I24:J30, O24:S30")
SourceRange.Select
Stop                 ' look at your worksheet here to see if the range is correct  ... comment out two lines after verification