xancalagonx
Board Regular
- Joined
- Oct 31, 2011
- Messages
- 57
What I am trying to do is tidy up an Excel report by deleting all the unwanted rows of data.
The Rng is dimmed as String.
What I tried to do is first .find the column and define the range of the selection I want to work with.
Then I run a For...Next loop to identify all the cells with a value other than "ASPF", and add the address of those cells into a string called Rng so all the addresses are listed out as $G$3, $G$4, $G$7,$G$11 and so on.
All of this seems to work.
However, when I attempt to .Select all the cells defined in the Rng, I get an error message.
What I wanted to do was .select all the cells with the unwanted data, then simply expand the selection by using .EntireRow and then .Delete all of the rows leaving me with only the data I need to work with.
The error message I receive is: "Run-time error '1004': Method 'Range' of object '_Global' failed"
Unfortunately I'm not too sharp on Excel code (yet) but I have a suspicion that the Rng which is dimmed as a String gets overflowed with data (since the report has a total of 8700 rows and likely around 5000 of those should be deleted) or that the Rng somehow can't handle the amount of addresses being fed into it from the For...Next.
Anyone know what trips up my code, or how/what to use instead to get it working?
Code:
Range("1:1").Find(what:="MCCR*resp*", lookat:=xlWhole).Activate
Range(ActiveCell.Offset(1, 0).Address, ActiveCell.End(xlDown).Address).Select
For Each cell In Selection
If cell <> "ASPF" Then
firstAddress = cell.Address
Rng = Rng & cell.Address & ","
End If
Next cell
If Rng <> "" Then
Rng = Left(Rng, Len(Rng) - 1)
Range(Rng).Select
Selection.EntireRow.Delete
End If
The Rng is dimmed as String.
What I tried to do is first .find the column and define the range of the selection I want to work with.
Then I run a For...Next loop to identify all the cells with a value other than "ASPF", and add the address of those cells into a string called Rng so all the addresses are listed out as $G$3, $G$4, $G$7,$G$11 and so on.
All of this seems to work.
However, when I attempt to .Select all the cells defined in the Rng, I get an error message.
What I wanted to do was .select all the cells with the unwanted data, then simply expand the selection by using .EntireRow and then .Delete all of the rows leaving me with only the data I need to work with.
The error message I receive is: "Run-time error '1004': Method 'Range' of object '_Global' failed"
Unfortunately I'm not too sharp on Excel code (yet) but I have a suspicion that the Rng which is dimmed as a String gets overflowed with data (since the report has a total of 8700 rows and likely around 5000 of those should be deleted) or that the Rng somehow can't handle the amount of addresses being fed into it from the For...Next.
Anyone know what trips up my code, or how/what to use instead to get it working?