0

Have a few questions about the following code that compressed and reformats a list.

  1. I set variable endIndicator as a temporary marker for the end of the list. Would it be better to just continually check for the current end of the list via my ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row - 1?
  2. I tried a With ActiveCell.Offset(rowOffset) statement in the delete loop which gives me a

    run time error 424 Object Required

    on the second iteration of the loop. I gather this has to do with the destruction of the previous row. Is there an appropriate With statement to use for this loop?

  3. Similarly, I tried a With ActiveCell.EntireColumn for the last three statements and get the same result. I assume for the same reason. Is there an appropriate solution?

Here's the code

Option Explicit

Sub Condense1()    
'Purpose : Condense list by removing unwanted rows    
'Requires: Column B row verbiage    
'          Column A row blank for unwanted row    
'Returns : Single compressed column of values wanted    

    Dim endIndicator As String
    Dim rowOffset As Long

    Worksheets(1).Activate          'Select Sheet
    Range("A1").Select              'Set offset base

    endIndicator = "zzzendozx"      'Assign unique value unlikely to be duplicated
                                    'Find last used row
    rowOffset = ActiveSheet.UsedRange.Rows.Count _
        + ActiveSheet.UsedRange.Rows(1).Row - 1
                                    'Temporarily mark next row as loop terminator
    ActiveCell.Offset(rowOffset, 0).Value = endIndicator

    rowOffset = 0                   'Reset offset pointer

                                    'For each row from top to loop terminator
    Do While ActiveCell.Offset(rowOffset).Value <> endIndicator
                                    ' Delete rows whose column "A" is empty
        If Len(ActiveCell.Offset(rowOffset).Value) < 1 Then
            ActiveCell.Offset(rowOffset).EntireRow.Delete
        Else
            rowOffset = rowOffset + 1   'Otherwise prepare to look at next row
        End If
    Loop
    ActiveCell.Offset(rowOffset).EntireRow.Delete   'Remove loop terminator row
    ActiveCell.EntireColumn.Delete                  'Remove Column A
    ActiveCell.EntireColumn.Font.Size = 14          'Set Font
    ActiveCell.EntireColumn.AutoFit                 'Set optimum column width

End Sub
FreeMan
  • 5,660
  • 1
  • 27
  • 53
grNadpa
  • 79
  • 1
  • 8
  • 4
    You need to read up on how to avoid Select/ActiveCell https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba – SJR Feb 26 '19 at 16:25
  • 1
    Could you try to describe in 1-2 sentences what exactly you are trying to achieve with your code? Ideally provide some data (eg. picture of data) with input and expected result. Makes it a bit easier to comprehend – Samuel Hulla Feb 26 '19 at 16:36
  • Rawplus: I do not know how to add a picture. Cells A3 A5 A6 and A8 contain a letter "x". Cells B1 B2 and B4 contain literal "ignore", Cells B3 B5 and B6 contain literal "Show" Cell B8 contains "After skipped row". All other cells blank or unused. When done, I expect a single column with A1 A2 and A3 as "Show" and A4 as "After skipped row" – grNadpa Feb 27 '19 at 20:46
  • SJR Reading up on that now. Thank you. – grNadpa Feb 27 '19 at 20:49

1 Answers1

0

This may be of some help:

Sub Kompressor()
    Dim nLastRow As Long, nFirstRow As Long, i As Long
    Set r = ActiveSheet.UsedRange

    nLastRow = r.Rows.Count + r.Row - 1
    nFirstRow = r.Row

    For i = nLastRow To nFirstRow Step -1
        With Cells(i, 1)
            If .Value = "" Then .EntireRow.Delete
        End With
    Next i
End Sub

Note we run the loop backwards.
The code will run slightly faster if we delete only one time.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • Thank you. works nicely. Note that r is not defined in you code. I set it as Range and it worked. – grNadpa Feb 27 '19 at 20:34