dcsimg
Results 1 to 2 of 2

Thread: excel range selection in loop not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    1

    Post excel range selection in loop not working

    Code:
    Sub test1()Dim i As Integer
    Dim acount
    acount = Range("A" & Rows.Count).End(xlUp).Row
    
    
    For i = 1 To acount
    If Range("A:D" & i).Value = "" Then
    Range("A:D" & i).Select
    Selection.Delete shift:=xlUp
    End If
    Next i
    i = i + 1
    
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,760

    Re: excel range selection in loop not working

    what do you mean, not working?
    what does happen?
    error? wrong result?
    nothing at all?

    this appears to be intended to delete any cells in column A, within the data area if the worksheet, when they all contain no data
    as this only deletes limited columns, not the entire row, you should not have a problem with skipping rows, but if any cells are deleted, the code will continue past the data in those columns, for the number of rows with deleted cells

    try like
    Code:
    Dim acount
    acount = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To acount
    If WorksheetFunction.CountBlank(Range("A" & i).Resize(, 4)) = 4 Then   ' all cells A:D are empty
    Range("A" & i).Resize(, 4).Delete shift:=xlUp
    End If
    Next i
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width