If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Find Copy Macro which contains "xyx"

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-23-10, 07:25
parmdeep parmdeep is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
Find Copy Macro which contains "xyx"

Hello all,

since my last post i can gladly say my macro knownledge is increasing, I am currently working on a spreadsheet that has 5000 rows and I have to copy rows on to a new worksheet if a row has one of 3 criteria, I have written a macro to do 2 of the criteria.

The Criteria is as follows;

AL col contains a "Y"
AN col contains a "Y"

J column contains "dec" or "de'd" or "dedd"

AL and AN only contain "Y" or "N" so the macro was fairly straight forward, im havin issues with Col J since it contains other things i.e. " xdsddxsds dec "

this is the macro i have written

Sub Find()
Set i = Sheets("Sheet1")
Set e = Sheets("Sheet2")
Dim d
Dim j
d = 1
j = 2

Do Until IsEmpty(i.Range("AL" & j))

If i.Range("AL" & j) = "Y" Then
d = d + 1
e.Rows(d).Value = i.Rows(j).Value

Else
If i.Range("AN" & j) = "Y" Then
d = d + 1
e.Rows(d).Value = i.Rows(j).Value

End If
End If

j = j + 1
Loop
End Sub

But im having major issues in trying to get one macro to to run through all the criteria, any help would be much appriecated.

Last edited by parmdeep; 03-23-10 at 08:00.
Reply With Quote
  #2 (permalink)  
Old 03-23-10, 08:49
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi parmdeep,
Quote:
since my last post i can gladly say my macro knownledge is increasing
It's great to hear that you're picking up VBA well.

I just want to clarify the issue you're having with column J:
Quote:
J column contains "dec" or "de'd" or "dedd"

...... im havin issues with Col J since it contains other things i.e. " xdsddxsds dec "
Am I right in thining that you want to find a string within a string? So, in your example, that row should be copied because "xdsddxsds dec" contains one of your 3 keywords ("dec" or "de'd" or "dedd")?

If you could just confirm or (if that interpretation is wrong) clarify what you need then we should be able to come up with something for you.

The looping approach you're employing is very logical and a good learning exercise. We might find that there's a better way to achieve your goal though - by using (automating) Excel's built-in Advanced Filter tool.

Hope that helps...
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #3 (permalink)  
Old 03-23-10, 09:09
parmdeep parmdeep is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
hi Colin,

Yes you are right to assume that it is a string within in a string, im working with a file that col J displays the acc holder's name but if hes deceased (i know gruesome task) it displays Dec, Dec'd, or Deceased I should mention that since its been inputted manually it can be a mix of upper case and lower case characters. i.e. some have written DEC and some Dec and some dec etc.

But i should also mention that some times the acc holder isnt deceased but can still fulfil one of the other criterias. and thus has to be copied.

if i use the filter way will it show for all 3 criteria? (i shall start googling now)

Last edited by parmdeep; 03-23-10 at 09:21.
Reply With Quote
  #4 (permalink)  
Old 03-23-10, 09:59
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi parmdeep,
Quote:
but if hes deceased (i know gruesome task) it displays Dec, Dec'd, or Deceased I should mention that since its been inputted manually it can be a mix of upper case and lower case characters. i.e. some have written DEC and some Dec and some dec etc.
Effectively then, using the advanced filter, the criteria for that column can just be "dec*", which will pick up any string beginning with the letters "dec" and is not case sensitive. Btw, you could implement some data validation to get the manual entries in that field consistent? A simple boolean flag would seem appropriate (is the person dead, TRUE/FALSE)?


Anyway, the criteria for the advanced filter are:
Column AL is Y
OR
Column AN is Y
OR
Column J is Dec*

This maps to a criteria range as follows:
Code:
Field 2 Field 3 Field 4
Y
         Y
                     dec*
where the field headers in bold are the column headers in your table. Once that's set up the advanced filter is quite simple to automate. Each critereon is on it's own row which signifies OR conditions.

I've attached an example to show you. The code in the example is simply:
Code:
    Range("B13:E1000").AdvancedFilter _
                Action:=xlFilterCopy, _
                CriteriaRange:=Range("Criteria"), _
                CopyToRange:=Range("Extract"), _
                Unique:=False
where Range("B13:E1000") is the source table. Of course, the dimensions (number of rows) of the source table can be determined dynamically at runtime. In the example I've added some conditional formatting just to indicate which rows meet the criteria to be shown in the results area.


For learning purposes, if you want me to go over the loop approach you were trying to employ then let me know.


Hope that helps...
Attached Files
File Type: zip Advanced Filter OR conditions example.zip (7.8 KB, 8 views)
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #5 (permalink)  
Old 03-23-10, 10:09
parmdeep parmdeep is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
Thank you ever so much, from my google searches the troubles i was running into was the i put the whole criteria on the same line i didnt realise for an OR statement they should be on seperate lines. as far incorporating the loop into my macro for this i guess i should stick to using the excel functions for the time being i do not wanna jump in the deep end quite yet enjoyin my armbands! ty again.
Reply With Quote
  #6 (permalink)  
Old 03-23-10, 10:35
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Debra Dalgleish has some good material on her site; here's some information on the advanced filter:
Excel Advanced Filter Introduction

You can see on there that your Dec* criterion could be simplified to Dec.

Good luck with the rest of your project!
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On