简体   繁体   English

Excel VBA宏数组限制

[英]Excel VBA Macro Array Limit

I'm relatively new to Macro's in Excel (more of a java programmer) so I've written something to simply delete all the rows in a sheet for a colleague of mine. 我对Excel中的Macro相对较新(更多是Java程序员),所以我写了一些东西来简单地删除我同事的工作表中的所有行。

The code is as follows: 代码如下:

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

The reason I split it up over three different macro's is because when I try to fill myArray with the complete set of numbers, it says the statement is incorrect. 我将其拆分为三个不同宏的原因是,当我尝试用完整的数字集填充myArray时,它说该语句不正确。 I assume this has to do with the maximum amount of numbers I can add in the Array? 我认为这与我可以在Array中添加的最大数量有关吗? Is this correct and does anyone know a workaround? 这是正确的吗,有人知道解决方法吗?

PS: I know this might not be the most efficient way to do it but I figured if I start from the bottom, I will delete the correct rows whereas if I start from the beginning, I would delete rows in a faulty way as 3 becomes 2 after you delete 2. PS:我知道这可能不是最有效的方法,但是我认为如果我从底部开始,则会删除正确的行,而如果我从头开始,则会以错误的方式删除行,因为3变为2删除后2。

I don't know if there is a maximum number of input arguments for the Array function but even if there was you haven't reached it yet. 我不知道Array函数是否有最大数量的输入参数,但是即使您尚未达到此数量也是如此。 Your problem might be the maximum line length (up to 1024). 您的问题可能是最大线长(最多1024)。 Try to split up the numbers using _ : 尝试使用_分割数字:

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

edit: I could fit 2037 numbers inside an Array(...) command before getting a compile error (Too many local, nonstatic varaibles) 编辑:在出现编译错误之前,我可以在Array(...)命令中放入2037个数字(太多本地,非静态变量)

I would not accomplish your task as you have (very inefficient). 我将无法完成您的任务(效率很低)。 However, to answer your question, I have read there is a limit of 1,024 characters in a physical line. 但是,为回答您的问题,我读到物理行中的字符数限制为1,024个。 This can be overcome by using the line continuation character. 这可以通过使用行连续字符来克服。

EDIT : 编辑

Here is a method using your row numbers, but combining them into a single range object: 这是一种使用行号的方法,但是将它们组合成一个范围对象:

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

Here is a screenshot of a message box containing the range addresses in MyRange : 这是一个包含MyRange范围地址的消息框的屏幕截图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM