VBA Code Needed

prashanthdivya

Board Regular
Joined
Aug 23, 2011
Messages
75
Hi All,</SPAN>


I have a dump which contains huge data and under source field I need to retain source A, Source B, and Source C and the rest of the data I need to delete. Please help me with a VBA code for this.</SPAN>
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Whoa...that's a pretty general question !!
Can you give us more ( a lot more) information. ??
 
Upvote 0
Hi ,</SPAN>

I receive a huge dump on a daily basis. There is a column named Source in this dump and I have been told to retain only the data from three different sources i.e. Source A, Source B and Source C . There are data from other sources as well which I need to delete. But I believe using Auto filter I can have only two criteria’s. So I am need of VBA Code which would do this for me automatically</SPAN>

Ex:</SPAN>

Product</SPAN></SPAN>
Type</SPAN></SPAN>
Position</SPAN></SPAN>
Status</SPAN></SPAN>
Source</SPAN></SPAN>
A</SPAN></SPAN>
Bill </SPAN></SPAN>
Duplicate</SPAN></SPAN>
Taken</SPAN></SPAN>
SOURCE A</SPAN></SPAN>
B</SPAN></SPAN>
CS</SPAN></SPAN>
Delayed </SPAN></SPAN>
Action</SPAN></SPAN>
SOURCE D</SPAN></SPAN>
V</SPAN></SPAN>
HT</SPAN></SPAN>
fsda</SPAN></SPAN>
Request</SPAN></SPAN>
SOURCE B</SPAN></SPAN>
A</SPAN></SPAN>
HT</SPAN></SPAN>
gps</SPAN></SPAN>
Granted</SPAN></SPAN>
SOURCE R</SPAN></SPAN>
C</SPAN></SPAN>
NET</SPAN></SPAN>
andr</SPAN></SPAN>
QUERY</SPAN></SPAN>
SOURCE A</SPAN></SPAN>
F</SPAN></SPAN>
CCNA</SPAN></SPAN>
hwcse</SPAN></SPAN>
GFDDD</SPAN></SPAN>
SOURCE D</SPAN></SPAN>

<TBODY>
</TBODY>
 
Upvote 0
Hi Michael,</SPAN>

I receive a huge dump on a daily basis. There is a column named Source in this dump and I have been told to retain only the data from three different sources i.e. Source A, Source B and Source C . There are data from other sources as well which I need to delete. But I believe using Auto filter I can have only two criteria’s. So I am need of VBA Code which would do this for me automatically</SPAN>

Ex:</SPAN>

Product</SPAN></SPAN>
Type</SPAN></SPAN>
Position</SPAN></SPAN>
Status</SPAN></SPAN>
Source</SPAN></SPAN>
A</SPAN></SPAN>
Bill </SPAN></SPAN>
Duplicate</SPAN></SPAN>
Taken</SPAN></SPAN>
SOURCE A</SPAN></SPAN>
B</SPAN></SPAN>
CS</SPAN></SPAN>
Delayed </SPAN></SPAN>
Action</SPAN></SPAN>
SOURCE D</SPAN></SPAN>
V</SPAN></SPAN>
HT</SPAN></SPAN>
fsda</SPAN></SPAN>
Request</SPAN></SPAN>
SOURCE B</SPAN></SPAN>
A</SPAN></SPAN>
HT</SPAN></SPAN>
gps</SPAN></SPAN>
Granted</SPAN></SPAN>
SOURCE R</SPAN></SPAN>
C</SPAN></SPAN>
NET</SPAN></SPAN>
andr</SPAN></SPAN>
QUERY</SPAN></SPAN>
SOURCE A</SPAN></SPAN>
F</SPAN></SPAN>
CCNA</SPAN></SPAN>
hwcse</SPAN></SPAN>
GFDDD</SPAN></SPAN>
SOURCE D</SPAN></SPAN>

<TBODY>
</TBODY>
 
Upvote 0
A couple of assumptions here
Columns are "A" to "E"
The text in column "E" is, actually "SOURCE ", otherwise you will have to change to suit
Code:
Sub tester2()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "E").End(xlUp).Row
For r = lr To 2 Step -1
    If Range("E" & r).Value <> "SOURCE A" And Range("E" & r).Value <> "SOURCE B" _
    And Range("E" & r).Value <> "SOURCE C" Then
    Rows(r).Delete
    End If
Next r
End Sub
 
Upvote 0
A couple of assumptions here
Columns are "A" to "E"
The text in column "E" is, actually "SOURCE ", otherwise you will have to change to suit
Code:
Sub tester2()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "E").End(xlUp).Row
For r = lr To 2 Step -1
    If Range("E" & r).Value <> "SOURCE A" And Range("E" & r).Value <> "SOURCE B" _
    And Range("E" & r).Value <> "SOURCE C" Then
    Rows(r).Delete
    End If
Next r
End Sub

Hi Mike, I have a small question regarding the Step - 1 , is this to go back up one row once another row is deleted?

Example: If row 3 is deleted, then the activecell move up one row to repeat the loop? As once Row 3 is deleted, row 4 becomes row 3, and if we dont put the step then one row is omitted?
 
Upvote 0
hi

are you sure you want to delete the rows which dose not contains (Source A, Source B and Source C )

Give me some details

in which column these details will be available (Source A, Source B and Source C )


Column

A
B
CDE
ProductTypePositionStatusSource
ABillDuplicateTakenSOURCE A
BCSDelayedActionSOURCE D
VHTfsdaRequestSOURCE B
AHTgpsGrantedSOURCE R
CNETandrQUERYSOURCE A
FCCNAhwcseGFDDDSOURCE D

<tbody>
</tbody>
 
Upvote 0
Hi Michael,

Thankyou for the reply but this code is taking a lot of time since the data that I have is Huge almost 3 lakhs. It would be helpful if it is faster.
 
Upvote 0
@Mindpsyche
Yeah, basically.
If you F8 through the code and then run your cursor over the "r" in the code you'll see that the "r" value decreases after each loop, which means it is working it's way back to line 2, one line at a time.
If you change the step-1 to step -2 it will only do every second line.
 
Upvote 0
Try
Code:
Sub tester2()
Dim lr As Long, r As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "E").End(xlUp).Row
For r = lr To 2 Step -1
    If Range("E" & r).Value <> "SOURCE A" And Range("E" & r).Value <> "SOURCE B" _
    And Range("E" & r).Value <> "SOURCE C" Then
    Rows(r).Delete
    End If
Next r
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,483
Messages
6,055,672
Members
444,806
Latest member
tofanexcel

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