View Full Version : Excel VBA help me pls.


ghostling
12-04-2009, 01:35 AM
Currently I have 2 spreadsheets of data. Among the data, there is some similarity to the information. I want the system to match the data from the 2 sheets and extract the other data that is on the same row as them. Is there anyone who can help me in this?

wiklendt
12-06-2009, 03:13 PM
you don't need VBA to do what you describe - i think =LOOKUP() or =VLOOKUP() will help you, if i understand your problem correctly. i'm pretty sure excel help has good information (to at least get you started) on how to use these formulas.

ghostling
12-06-2009, 08:58 PM
i've tried using vlookup as stated. however, as the data of my first sheet have redundant empty spacing after the value, the vlookup is unable to work. is there a way to use vlookup to search for cell's containing my search value?

wiklendt
12-07-2009, 12:43 AM
it would help if you gave us an example of your data. even better - post your file to the forum as an attachment.

John Big Booty
12-07-2009, 01:02 AM
i've tried using vlookup as stated. however, as the data of my first sheet have redundant empty spacing after the value, the vlookup is unable to work. is there a way to use vlookup to search for cell's containing my search value?

Try using the Trim() (http://www.techonthenet.com/excel/formulas/trim.php) function before using the Vlookup()

wiklendt
12-07-2009, 11:49 AM
Try using the Trim() (http://www.techonthenet.com/excel/formulas/trim.php) function before using the Vlookup()

ah, yes. that should work on what the OP described, if it's now an accurate description. forgot about that one, nice work john. :)

ghostling
12-08-2009, 06:58 PM
Thank you for your help. With the Trim function I am able to do the value matching for the first portion. The amount of value matching for the first portion is 296 rows of data.

Do
For row1 = 1 To FinalRow
If Trim(Sheets("GradRegList").Cells(row1, 3)) = Trim(Sheets("GradRegList").Cells(row2, 15)) Then
award = Sheets("GradRegList").Cells(row2, "M").Value
Sheets("GradRegList").Cells(row1, "I").Value = award
row2 = row2 + 1
End If
Next row1
Loop Until Sheets("GradRegList").Cells(row2, 15) = ""

Below is the coding for the second portion.

Do
For row3 = 1 To FinalRow
If Trim(Sheets("GradRegList").Cells(row3, 3)) = Trim(Sheets("GradRegList").Cells(row4, 13)) Then
qual = Sheets("GradRegList").Cells(row4, 16).Value
Sheets("GradRegList").Cells(row3, 10).Value = qual
row4 = row4 + 1
End If
Next row3

Loop Until Sheets("GradRegList").Cells(row4, 16) = ""

I am using the exact same logic for the codings. The only difference between the first and second portion is that the total rows of data to be matched for the second portion is 4985.

I tried running both codings. However, only the first portion works. The second portion appeared to be as a indefinite loop, hanging my computer and crashing excel.

Is there anyone that can help me in this?

Thanks

ghostling
12-15-2009, 12:57 AM
bumpz~

Can someone kindly help me?

Is there any ways for the entire loop to bypass the loop should 1 of the data is incorrect?

wiklendt
12-15-2009, 01:10 AM
i don't know much about loops or other methods you've used in your code, but i do see some differences that may be why you're having difficulty. that is, i can see in the first code (you working code), that some of the values have double quotation marks, whereas in your second code, values in the same method arguments appear without quotation marks. might this be it?

e.g.,
code 1

award = Sheets("GradRegList").Cells(row2, "M").Value
Sheets("GradRegList").Cells(row1, "I").Value = award
code 2

qual = Sheets("GradRegList").Cells(row4, 16).Value
Sheets("GradRegList").Cells(row3, 10).Value = qual
i know the values in code 2 LOOK like numbers, but try the quotes, you never know! (hey, it worked in code 1! ;))

chergh
12-15-2009, 03:57 AM
whats the point of the do while loop?

The infinite loop is due to the "row4 = row4 + 1" being within the if statement. At some point there are no more true values to the if statement so row4 is not being incremented and therefore the loop until criteria is never fulfilled. the same could also happen to your first code portion. remember just because something works doesn't mean the logic is sound.

ghostling
12-15-2009, 06:33 AM
whats the point of the do while loop?

The infinite loop is due to the "row4 = row4 + 1" being within the if statement. At some point there are no more true values to the if statement so row4 is not being incremented and therefore the loop until criteria is never fulfilled. the same could also happen to your first code portion. remember just because something works doesn't mean the logic is sound.

hello, if thats the case would there be any ways to bypass the false value should the loop encounter it?

chergh
12-15-2009, 06:59 AM
Try removing the do while loop and see if it works.

ghostling
12-15-2009, 07:03 AM
Try removing the do while loop and see if it works.

I had tried that way before. It is able to run smoothly. However, because the For loop condition is from row3 to final row (that is pre-defined previously). Therefore, if my first row3 loop runs up till the 2001th row, the second loop will start from the 2002th loop to the final loop, resulting in the data from 1-2000th rows not able to fulfil the matching process.