1

I'm very new to VBA so this might be a basic question..

Every week i'm exporting a file with all kinds of data. Within this file I have to delete the same range of rows every time. I could easily automate this by defining a range based on cell positions, but this range starts sometimes not on the same row. However, the range starts with the same value and ends with the same value every time.

Is there any chance I can delete automatically all the rows within the range from begin to bottom? Without needing to specify a range based on cell positions?

10
  • 1
    Are those start and stop values repeated elsewhere within the same column? If not you could use Find to locate them and those cell references to set up your range for deletion. Or even, if they are the first match and last match respectively. Commented Mar 14, 2018 at 9:20
  • Only appear once. Is text though. Commented Mar 14, 2018 at 9:21
  • Can you show some data and any attempts you have made at coding so far? Text can be matched in lots of ways. Is it part of a larger bit of text in a cell? Commented Mar 14, 2018 at 9:22
  • Range starts with: "Artikelgroep: Promotional material" Within the range: "1234ART.NR PRODUCT NAME" Range ends with: "Artikelgroep: (totaal) " Within this range there are +- 100 rows with all different values and placed every week on another row. Commented Mar 14, 2018 at 9:29
  • is "Artikelgroep: Promotional material" within one cell by itself? Commented Mar 14, 2018 at 9:30

1 Answer 1

1

And this is what that might look like for you

Option Explicit
Sub TargetRows()

    Dim wb As Workbook
    Dim wsTarget As Worksheet
    Dim startCell As Range
    Dim endCell As Range
    Dim startMatch As String
    Dim endMatch As String

    startMatch = "Artikelgroep: Promotional material"
    endMatch = "Artikelgroep: (totaal)"

    Set wsTarget = ThisWorkbook.Worksheets("Sheet2") 'change as required
    Set startCell = wsTarget.Columns(1).EntireColumn.Find(what:=startMatch, LookIn:=xlValues, lookat:=xlPart)
    Set endCell = wsTarget.Columns(1).EntireColumn.Find(what:=endMatch, LookIn:=xlValues, lookat:=xlPart)

    Dim deleteRange As Range

    If Not startCell Is Nothing And Not endCell Is Nothing And startCell.Row <= endCell.Row Then
        Set deleteRange = wsTarget.Range("A" & startCell.Row & ":A" & endCell.Row)
    Else
        Debug.Print "1 or both values not found or end text found before start text."
    End If

    If Not deleteRange Is Nothing Then deleteRange.EntireRow.Delete
End Sub

Reference:

Excel VBA Find Method for specific column (@shai rado)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.