循环从单元格开始并删除列

时间:2016-01-08 14:38:35

标签: excel vba excel-vba while-loop

我的所有数据都在" D"柱。

我想启动一个特定的单元格(即D249),然后选择下一个600并删除它们(即D250-D850)。然后我想跳过下一个(即D851)并删除下面的下一个600。我想这样做直到它结束,就像D19000。我想我需要使用while循环来检查我正在查看的单元格中是否存在数据。

这是我到目前为止的代码:

Sub delete600rows()
'delete600rows Macro
'Keyboard Shortcut: Ctrl+a

ActiveSheet.Range("D249").Select
While LEN()>0

Range("D249:D848").Select
Selection.Delete Shift:=xlUp

End Sub

如何为while循环编写条件,如何使范围选择按照我想要的方式工作?

5 个答案:

答案 0 :(得分:0)

(对不起,我还没有评论。)

我有点困惑我不得不说:如果你可以从D249向下删除所有内容,为什么你只想要删除600行?您是否在本专栏中找到了什么?有什么用呢?

否则有可能检查最后一行:

Dim LastRow as Long
LastRow = ActiveWorkbook.ActiveSheet.Cells(ActiveWorkbook.ActiveSheet.Rows.Count, "D").End(xlUp).Row

然后你可以删除从D249到工作簿中最后一行的所有内容。

如果我错了,那么肯定会有更多的澄清。

答案 1 :(得分:0)

你计算使用for循环并添加501的实例,cyntax关闭,但我在php中长大

start = 249
len = 500 

for (i=0;i=100;i++) {
    from = (i*(len+1)+start
    to = from+len
    range.select("D" & from & ":D" & to
    if (range.select("D" & from).value = ""){
       break
    }
}

答案 2 :(得分:0)

以下一项应该有效。我没有广泛测试过它。我选择了一个选项,您可以在其中选择要从中开始的单元格以及要删除的行数。如果这些是标准的,只需在代码中硬编码:)

Sub deleteNumOfrows()
Dim myBook As Workbook
Dim mySheet As Worksheet
Dim startCell As Range
Dim numOfRowsToDelete As Long
Dim lastRow As Long
Dim i As Long

'Set your workbook - in this version I select the active workbook
Set myBook = ActiveWorkbook

'Set up your worksheet - In this version I select the active worksheet
Set mySheet = myBook.ActiveSheet

'Select the cell you want to start
Set startCell = Application.InputBox(prompt:="Select a cell", Type:=8)

'Input how many rows you want to delete - If it is always 600 just set it to that
numOfRowsToDelete = Application.InputBox(prompt:="Number of rows to delete each time")

'Find the last row data exists in the column of the startCell
lastRow = mySheet.Cells(mySheet.Rows.Count, startCell.Column).End(xlUp).Row

'Delete the rows you do not want
i = startCell.Row 'Start from the row of the startCell

While i <= lastRow 'Repeat while you haven't reached the last row
    'mySheet.Rows((i + 1) & ":" & (i + numOfRowsToDelete)).EntireRow.Delete 'Delete the rows
    mySheet.Range(Cells((i + 1), startCell.Column), Cells((i + numOfRowsToDelete), startCell.Column)).Delete Shift:=xlUp  'Delete the range you dont want
    i = i + 1 'Next row
    lastRow = lastRow - numOfRowsToDelete 'Adjust lastRow
Wend

End Sub

答案 3 :(得分:0)

试试这段代码:

Sub test()

 Dim LR As Long
 Dim Rng As Range

 LR = Range("D" & Rows.Count).End(xlUp).Row
 Debug.Print LR
 If LR < 851 Then
  Set Rng = Range("D250:D850")
 Else
   For i = 249 To LR Step 601
    j = j + 1
    If Rng Is Nothing Then
     Set Rng = Range(Cells(i + j, "D"), Cells(i + j + 600, "D"))
    Else
     Set Rng = Union(Rng, Range(Cells(i + j, "D"), Cells(i + j + 600, "D")))
    End If
   Next
 End If

 Rng.Delete xlUp
End Sub

如果要删除整行,请将Rng.Delete xlUp替换为Rng.EntireRow.Delete

答案 4 :(得分:0)

这是一个让你适应的想法,而不是正确的代码:

<div class="stepwizard">
    <ul class="stepwizard-row">
        <li class="stepwizard-step">
            <span class="step-border"><a href="#step1" class="data-toggle="tab">1</a></span>
         //If user  click here <span class="step1"> should be change
        </li>
        <li class="stepwizard-step">
            <span class=""><a href="#step2" data-toggle="tab" >2</a></span>
          //If user  click here <span class="step2"> should be change
        </li>
        <li class="stepwizard-step">
            <span class=""><a href="#step3" class="p-n-t step-tab" aria-controls="documents" role="tab" data-toggle="tab">3</a></span>
       //If user  click here <span class="step3"> should be change
        </li>
    </ul>
</div>
<div class="m-t-sm step-title">
    <span class="step1">BASIC INFO</span> //here i want to add class
    <span class="step2">REGISTERED NUMBERS</span>
    <span class="step3">CLIENT SERVICES</span>
</div>

这样的东西可以工作,因为你总是一次删除600行,然后你想保留下一行。因此,在删除范围250-850之后,行250现在包含在行851中删除之前的内容。