简体   繁体   English

行选择限制(?)VBA Excel

[英]Row selection limit (?) VBA excel

I am new in VBA. 我是VBA新手。

I run into a problem and I could not find any answer using google. 我遇到问题,无法使用Google找到任何答案。 The goal is: selecting multiple rows, then deleting them. 目标是:选择多行,然后将其删除。 Here is the code: 这是代码:

Dim i As Long
Dim SelectedCells As String

i = 1
SelectedCells = ""

While IsEmpty(Range("A" & i)) = False

If Range("A" & i) < 5 Then

        SelectedCells = SelectedCells & i & ":" & i & ","
        i = i + 1
Else:
        i = i + 1
End If

Wend

SelectedCells = Left(SelectedCells, Len(SelectedCells) - 1)
'That is how I inspected the value of the "SelectedCells" variable:
'Range("AA1").Value = Right(SelectedCells, 25)
Range(SelectedCells).Select
Selection.Delete Shift:=xlUp

It works fine up to 45 selections. 它最多可以处理45个选择。 If it needs to select more than that, it returns an error message at the "Range(SelectedCells).Select": "Run-time error '1004': Method 'Range' of object '_Global' failed. 如果需要选择更多选项,它将在“ Range(SelectedCells).Select”处返回错误消息:“运行时错误'1004':对象'_Global'的方法'Range'失败。

However, I inspected the value of SelectedCells variable, and it looks as it should... 但是,我检查了SelectedCells变量的值,它看起来应该是...

Does anybody have an idea what is wrong? 有人知道出什么问题了吗? Do you have any better idea to solve this issue? 您有解决这个问题的更好主意吗? I need to run this kind of loop on several thousands (or hundred-thousands) of records. 我需要在数千个(或十万个)记录上运行这种循环。

Any input will be appriciated. 任何输入都会被应用。

Thank you. 谢谢。

You do not need to select the range before deleting it. 删除之前不需要选择范围。 You do not have to use cell addresses. 您不必使用单元地址。

' Get a reference to sheet
Dim s As Worksheet
Set s = ThisWorkbook.Sheets("Sheet1")
' Get reference of range including row n to m
Dim n As Integer
Dim m As Integer
n = 10
m = 29
Dim r As Range
Set r = s.Range (s.Rows (n), s.Rows (m))
r.Delete

You can simply do this: 您可以简单地做到这一点:

Range("10:100").Delete

or 要么

Range(n & ":" & m).Delete

SelectedCells is a string (max 256 in length), so it just happens to be 45 selections. SelectedCells是一个字符串(最大长度为256),因此恰好是45个选择。 instead, use a loop like : 相反,使用如下循环:

ok , this will boost the speed of your stuff, even if its a bit more complicated (PS: dont use select, unless you want to select as a final goal) (PS2: Code not tested, adjust to your needs) 好的,这将提高您东西的速度,即使它有点复杂(PS:不要使用select,除非您希望将其作为最终目标)(PS2:未测试代码,请根据需要进行调整)

Dim ColA() as variant 'declares variable of VBA Array (wich is faster than Excel Array)
Dim i as long , MaxData as Long
Dim Sh as Worksheet

With Application
    .screenupdating=false
    .enableevents=false
    .calculation=XlManual
End with

Set Sh = Activesheet 'for multiple sheets just add sh in an argument when calling the macro
with sh
    MaxData = .cells( .rows.count , "A").End(Xlup).row 'count the lines in Column A
    redim ColA (1 to maxData , 1 to 1)
    ColA = .range( .cells(1,1) , .cells( MaxData,1) ).value 'ok, you can write this .range("A1:A" & maxData).value , also

    Dim Temp as Variant

    For i=MaxData to  step -1 'if you go the other way, some lines will be forgotten
        temp = ColA(i,1)
        if isempty(temp) then 
            exit for
        Elseif Temp<5 then .Cells(i,1).EntireRow.delete 'can be written many other ways, dont use select ; might need a if isnumber(Temp) or if isnumeric(Temp) to avoid text, but i used temp as variant to settle this
        'a simple .rows(i).delete , might just do the trick :)
        'also an other way : instead of adding the line number (i) to a string , you can increment a variable array ToDelete(1)=line1, Todelelte(2)=line28 ...
        'in my example of code i delete right away...
        End if
    Next i

erase ColA
set sh=nothing

With Application
    .screenupdating=true
    .enableevents=true
    .calculation=XlAutomatic
End with

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

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