Removing messy data

3thr3e

New Member
Joined
Mar 12, 2011
Messages
15
Hi,

I have created quite a long macro so I won't bore you with the details. I have just been testing it and found that due to a data discrepancy, about half way through the process cannot continue.

The sheet is the screenshot below. The stage it's getting stuck at is one where the macro goes down column E removing everything in each cell except the number in square brackets. It uses the following code:

Sub GetNums()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With Range("E" & i)
.Value = Val(Mid(.Value, InStr(.Value, "[") + 1, InStr(.Value, "]") - InStr(.Value, "[") - 1))
End With
Next i
End Sub

As you can see, when it gets to the cell containing "999 Ac" it errors. This is not a normal scenario and I don't know why our system exports the line like that. It's also a duplicate entry from the table (the one directly above it) so is completely unnecessary.

I guess the solution would be to create a macro which says:

"If any of the rows in column E don't begin with "Room #9999" then delete them."

How would I implement this?

helpaw.png
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
try this .NB not tested so do it on a copy of your data first!
Code:
Option compare text
Sub GetNums()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = LR to 1 step -1    '*****************note change here
If cells(1 , 5) like "*999 Ac" then
Rows(i).Delete
Else
With Range("E" & i)
.Value = Val(Mid(.Value, InStr(.Value, "[") + 1, InStr(.Value, "]") - InStr(.Value, "[") - 1))
End With
End if
Next i
End Sub
 
Upvote 0
Try this:

Code:
Sub GetNums()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row

For i = 1 To LR
    With Range("E" & i)
       
        If Left(.Value, 10) <> "Room# 9999" Then
            .Value = "Error " & .Value
        Else
            .Value = Val(Mid(.Value, InStr(.Value, "[") + 1, InStr(.Value, "]") - InStr(.Value, "[") - 1))
        End If
    End With
Next i

End Sub
 
Upvote 0
this is maybe better - misread your post
Code:
Option compare text
Sub GetNums()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = LR to 1 step -1    '*****************note change here
If Not cells(1 , 5) like "Room #9999*" then
Rows(i).Delete
Else
With Range("E" & i)
.Value = Val(Mid(.Value, InStr(.Value, "[") + 1, InStr(.Value, "]") - InStr(.Value, "[") - 1))
End With
End if
Next i
End Sub
 
Upvote 0
This seems to have worked. Please could you explain what has changed about the process so I know for the future?

Thanks!

EDIT: Had a proper look and makes sense now. Cheers

Try this:

Code:
Sub GetNums()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
 
For i = 1 To LR
    With Range("E" & i)
 
        If Left(.Value, 10) <> "Room# 9999" Then
            .Value = "Error " & .Value
        Else
            .Value = Val(Mid(.Value, InStr(.Value, "[") + 1, InStr(.Value, "]") - InStr(.Value, "[") - 1))
        End If
    End With
Next i
 
End Sub
 
Last edited:
Upvote 0
Glad to help. I'm sure you saw most of it was changuing from columns to rows and such.


One thing that always gets me, when counting rows from the bottom, the (xlUp) functionality is used but when counting columns from the right its (xltoleft). I can't tell you how often I bomb code by changing (xlup) for rows to (xlLeft) for columns when it should be (xlTOLeft).

That little different costs me about an hour a month of productivity. :-)
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top