0

I have written the following code which is supposed to run through a data set and delete all rows that do not match the value in call C1. In my original code I deleted line by line and the code was very slow, so now I am trying to add all values to a variant and delete all cells at the end. Is this possible?

Sub FixData()

Dim wbFeeReport As Workbook
Dim wsData As Worksheet
Dim wsData2 As Worksheet
Dim FrRngCount As Range
Dim x As Long
Dim y As Long
Dim varRows As Variant

Set wbFeeReport = ThisWorkbook
Set wsData = wbFeeReport.Worksheets("Data")
Set wsData2 = wbFeeReport.Worksheets("Data2")

Set FrRngCount = wsData.Range("D:D")
y = Application.WorksheetFunction.CountA(FrRngCount)

For x = y To 2 Step -1
If wsData.Range("J" & x).Value <> wsData2.Range("C1").Value Then
varRows = x
Else
wsData.Range("AF" & x).Value = wsData.Range("J" & x).Value
End If
Next x

wsData.Rows(varRows).EntireRow.Delete

End Sub

Right now the code only deletes the last row as the variant is overwritten each time as it runs through the loop. Any suggestions on how I can store all values in the variant and delete the rows I don't need at the end?

Thanks for you help!

1
  • 4
    Use the Union method to collect all of the cells into a single Range object then use rng.entirerow.delete. Commented Oct 5, 2016 at 16:59

2 Answers 2

1

The fastest way is to

  • Load the data into an array
  • Copy the valid data into a second array
  • Clear the contents of the range
  • Write the second array back to the worksheet

Sub FixData()
    Dim Source As Range
    Dim Data, Data1, TargetValue
    Dim x As Long, x1 As Long, y As Long

    Set Source = Worksheets("Data").Range("A1").CurrentRegion
    TargetValue = Worksheets("Data2").Range("C1")

    Data = Source.Value
    ReDim Data1(1 To UBound(Data, 1), 1 To UBound(Data, 2))

    For x = 1 To UBound(Data, 1)
        If x = 1 Or Data(x, 10) = TargetValue Then
            x1 = x1 + 1
            For y = 1 To UBound(Data, 2)
                Data1(x1, y) = Data(x, y)
            Next
        End If
    Next

    Source.ClearContents
    Source.Resize(x1).Value = Data1

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

3 Comments

Wow this is so quick! Thanks so much for your help Thomas.
Quick question: How come Dim Data, Data1 & TargetValue do not have do be defined?
If you don't specify a declaration type, the variable type defaults to Variant.
1

As you need a range holding all rows, you can collect it in one "on the run" like this:

Sub FixData()

  Dim wsData As Worksheet
  wsData = ThisWorkbook.Worksheets("Data")

  Dim val As Variant
  val = ThisWorkbook.Worksheets("Data2").Range("C1").Value

  Dim DelRows As Range, x As Long

  For x = 2 To wsData.Cells(wsData.Rows.Count, 4).End(xlUp).Row
    If wsData.Range("J" & x).Value <> val Then
      If DelRows Is Nothing Then
        Set DelRows = wsData.Rows(x)
      Else
        Set DelRows = Union(wsData.Rows(x), DelRows)
      End If
    Else
      wsData.Range("AF" & x).Value = wsData.Range("J" & x).Value
    End If
  Next x

  DelRows.EntireRow.Delete

End Sub

1 Comment

This is also a nice piece of code. Thanks for your help Dirk!

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.