Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to look for certain data in a column

Status
Not open for further replies.

wafs

Technical User
Jan 17, 2006
112
US
Here is a code that I requested to look for an input and compare to a column. It looks like it is working except the output is coping over all TestRange3 and not just ones that match the header. I have several different numbers in TestRange 2 and I need this to be able to go through the column and just pull out what I have requested. All help would be greatly appreciated.

Dim cellstart As Integer 'starting point of informaiton (row)
Dim p As Integer
Dim n As String 'counting variable
Dim TestRange As String 'Test String
Dim TestRange2 As String 'Test String
Dim TestRange3 As String 'Test String
Dim Output(1 To 10) As String
Dim m As String
Dim sh As String
Dim destsh As String
Dim reportlocation As String
Dim header As String 'header you want to run Holds header

cellstart = 3 'Starting point of source sheet
p = 0
header = InputBox("Which header do you want to run?", "header") 'input the requested header

sh = "Daily" 'Work sheet where the header information is kept
destsh = header ' desination sheet for header

n = cellstart
TestRange2 = "B" + n 'column where to match header request
TestRange3 = "A" + n

Do While header <> Sheets(sh).Range(TestRange2)
p = p + 1
m = p + 4 'Tells where to start importing information on dest sheet
reportlocation = "A" + m 'starting point of output file
Sheets(sh).Range(TestRange3).Copy Sheets(destsh).Range(reportlocation)
TestRange2 = "B" + n
TestRange3 = "A" + n
n = n + 1
Loop
End Sub
 


It sure would be helpful to post some sample data for which this code is demonstrating as described.

Skip,

[glasses] [red]Be Advised![/red] A chicken, who would drag a wagon across the road for 2 cents, is…
POULTRY in motion to PULLET for a PALTRY amount! [tongue]
 

your copy occurs in the loop that says the header <> [testrange2]???

change your logic!


Skip,

[glasses] [red]Be Advised![/red] A chicken, who would drag a wagon across the road for 2 cents, is…
POULTRY in motion to PULLET for a PALTRY amount! [tongue]
 
I changed my logic and now it goes from the do while to the end sub. It doesn't read the rest of the macro. I just need the macro to go down a column and copy of information if it matches the header that I keyed in.
 


Then you don't want to stop processing then column if there is no match. You only want to copy if there is a match.

Skip,

[glasses] [red]Be Advised![/red] A chicken, who would drag a wagon across the road for 2 cents, is…
POULTRY in motion to PULLET for a PALTRY amount! [tongue]
 
Here is an updated code, and it won't run if the first header number is different then what I'm asking for. it goes right from the if statement to the end sub. If the first number is what i'm asking for it runs that, but stops at the next row if it is different then what I'm asking for. How can I get the code to keep running even if the next row does not match. do I need to move my copy statement after the else??

Dim cellstart As Integer 'starting point of informaiton (row)
Dim p As Integer
Dim x As Integer
Dim n As String 'counting variable
Dim TestRange As String 'Test String
Dim TestRange2 As String 'Test String
Dim TestRange3 As String 'Test String
Dim Output(1 To 10) As String
Dim m As String
Dim sh As String
Dim destsh As String
Dim reportlocation As String
Dim header As String 'header you want to run Holds header

cellstart = 3 'Starting point of source sheet
p = 0
header = InputBox("Which header do you want to run?", "header") 'input the requested header
TestRange = header
sh = "Daily" 'Work sheet where the header information is kept
destsh = header ' desination sheet for header

n = cellstart
TestRange2 = "B" + n 'column where to match header request
TestRange3 = "A" + n
If TestRange = Sheets(sh).Range(TestRange2) Then
p = p + 1
m = p + 3 'Tells where to start importing information on dest sheet
reportlocation = "A" + m 'starting point of output file
Sheets(sh).Range(TestRange3).Copy Sheets(destsh).Range(reportlocation)
n = n + 1
TestRange2 = "B" + n
TestRange3 = "A" + n
Else
End If

End Sub
 


Well what values are in testrange and Sheets(sh).Range(TestRange2) at that point?

Put a BREAK in your code and use the Watch Window to observe the key values.

Skip,

[glasses] [red]Be Advised![/red] A chicken, who would drag a wagon across the road for 2 cents, is…
POULTRY in motion to PULLET for a PALTRY amount! [tongue]
 


I'd probably change the code...
Code:
 If UCase(Trim(TestRange)) = UCase(Trim(Sheets(sh).Range(TestRange2))) Then


Skip,

[glasses] [red]Be Advised![/red] A chicken, who would drag a wagon across the road for 2 cents, is…
POULTRY in motion to PULLET for a PALTRY amount! [tongue]
 
I will give it a try. Thanks for you help.
 
Thanks guys, it works. I appreciate all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top