Excel VBA宏数组限制

时间:2016-07-07 10:57:10

标签: arrays excel vba excel-vba

我对Excel中的Macro相对较新(更多的是java程序员),所以我写了一些内容,只为我的同事删除工作表中的所有行。

代码如下:

Sub DeleteFirstPart()
Dim MyArray
MyArray = Array(455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 578, 593, 628, 638, 643, 679, 683, 754, 755, 759, 817, 826, 830, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 881, 885, 891, 892, 899, 900, 905, 911, 914, 915, 916, 923, 928)
For i = UBound(MyArray) To LBound(MyArray) Step -1
Rows(MyArray(i)).EntireRow.Delete
Next i
End Sub


Sub DeleteSecondPart()
Dim MyArray
MyArray = Array(100, 101, 102, 103, 105, 106, 107, 109, 112, 113, 114, 116, 120, 127, 139, 141, 148, 151, 165, 172, 185, 187, 189, 191, 196, 198, 201, 237, 240, 242, 243, 244, 246, 249, 253, 256, 271, 276, 282, 322, 323, 325, 330, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454)
For i = UBound(MyArray) To LBound(MyArray) Step -1
Rows(MyArray(i)).EntireRow.Delete
Next i
End Sub

Sub DeleteThirdPart()
Dim MyArray
MyArray = Array(2, 6, 24, 30, 31, 34, 35, 40, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 62, 64, 65, 66, 67, 68, 69, 70, 72, 73, 74, 76, 77, 78, 79, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 94, 96, 97, 98, 99)
For i = UBound(MyArray) To LBound(MyArray) Step -1
Rows(MyArray(i)).EntireRow.Delete
Next i
End Sub

我将它分成三个不同的宏的原因是因为当我尝试使用完整的数字填充myArray时,它说该语句是错误的。我假设这与我可以在数组中添加的最大数量有关吗?这是正确的,有没有人知道解决方法?

PS:我知道这可能不是最有效的方法,但我想如果从底部开始,我会删除正确的行,而如果我从头开始,我会以错误的方式删除行删除2后,3变为2。

2 个答案:

答案 0 :(得分:0)

我不知道Array函数是否有最大数量的输入参数,但即使您还没有达到它。您的问题可能是最大行长(最多1024)。尝试使用_分割数字:

myArray  = Array(2, 6, 24, 30, 31, 34, 35, _
    ....., _
    911, 914, 915, 916, 923, 928)

编辑:在收到编译错误(太多本地,非静态变量)之前,我可以在Array(...)命令中放入2037个数字

答案 1 :(得分:0)

我不会像你一样完成你的任务(非常低效)。但是,为了回答你的问题,我已经读过,物理行中有1,024个字符的限制。这可以通过使用行继续符来克服。

修改

这是一种使用行号的方法,但将它们组合成一个范围对象:

Option Explicit
Sub dural()
    Dim myArray As Variant
    Dim myRange As Range
    Dim I As Long

myArray = Array(2, 6, 24, 30, 31, 34, 35, 40, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 62, 64, 65, 66, 67, 68, 69, 70, 72, 73, 74, 76, 77, 78, 79, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 94, 96, 97, 98, 99, 100, 101, 102, 103, 105, 106, 107, 109, 112, 113, 114, 116, 120, 127, 139, 141, 148, 151, 165, 172, 185, 187, 189, 191, 196, 198, 201, 237, 240, 242, 243, 244, 246, 249, 253, 256, 271, 276, 282, 322, 323, 325, 330, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447 _
455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 578, 593, 628, 638, 643, 679, 683, 754, 755, 759, 817, 826, 830, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 881, 885, 891, 892, 899, 900, 905, 911, 914, 915, 916, 923, 928)

Set myRange = Rows(myArray(0))
For I = 1 To UBound(myArray)
    Set myRange = Union(myRange, Rows(myArray(I)))
Next I

myRange.Clear

End Sub

以下是包含MyRange中的范围地址的消息框的屏幕截图:

enter image description here