-
[RESOLVED] Compare Data Between worksheets
Thanks in adv for help.
I have 2 worksheets. The primary worksheet (Sheet1) only has data in the first 50 Rows.
The 2nd worksheet has a lot of Data down to 200 at least.
The Macro has to look for my Name in (Sheet2) Column F (F3:F200), and then the Project Number in Column B (B3:B200) (I guess I need an Offset command on this.)
It has to compare the Proj Number in Culumn B (Sheet2) B3:B200) (Only if it has my name in Column F) with the projects in Column E (E6:E50) of (Sheet1), and if there is no match before it reaches the end of the Data it places it in the next (empty) Row of Sheet1.
Thanks for the help.
-
Re: Compare Data Between worksheets
where did u stuck in ur code?
-
Re: Compare Data Between worksheets
an idea:
vb Code:
Sub a()
Dim endRow As Integer
Dim newRow As Integer
Dim i As Integer
Dim MyName As String
Dim Rng As Range
MyName = "Luis"
endRow = Sheets("Sheet2").Range("F1").End(xlDown).Row
newRow = Sheets("Sheet1").Range("F1").End(xlDown).Row + 1
For i = 1 To endRow
If Sheets("Sheet2").Cells(i, 6) = MyName Then
Sheets("Sheet1").Columns("B:B").Select
Set Rng = Selection.Find(What:=Sheets("Sheet2").Cells(i, 2).Value)
If Rng Is Nothing Then
Sheets("Sheet2").Rows(i).Copy
Sheets("Sheet1").Cells(newRow, 1).Select
ActiveSheet.Paste
newRow = newRow + 1
End If
End If
Next i
End Sub
-
Re: Compare Data Between worksheets
Thanks for the help Kaliman.
for some reason it's erroring on End Row=
I don't know if I have all the Starting and ending rows correct, and if it finds a match it should move to the next row on the Data sheet (sheet10).
Code:
Sub Vldt()
Dim endRow As Integer
Dim newRow As Integer
Dim i As Integer
Dim MyName As String
Dim Rng As Range
MyName = "Thomas"
endRow = Sheets("Sheet10").Range("E1").End(xlDown).Row 'Errors Subscript out of Range
newRow = Sheets("Sheet1").Range("E1").End(xlDown).Row + 1
For i = 3 To endRow
If Sheets("Sheet10").Cells(i, 6) = MyName Then
Sheets("Sheet10").Columns("B:B").Select
Set Rng = Selection.Find(What:=Sheets("Sheet1").Cells(i, 2).Value)
If Rng Is Nothing Then
Sheets("Sheet10").Rows(i).Copy
Sheets("Sheet1").Cells(newRow, 1).Select
ActiveSheet.Paste
newRow = newRow + 1
End If
End If
Next
End Sub
-
Re: Compare Data Between worksheets
Subscript out of range indicates that the Sheet is wrong. make sure your sheet name is "Sheet10". Even better, change the name of the sheet to something more representative and use that.
In that example, endRow is just counting the rows of the second sheet, the one with the many rows (it is expecting column E to be full, if there is a gap or empty cell in E it wont work correctly).
newRow is looking for the next row that will be filled in the first sheet. Again, needs column E to have all consecutive values.
-
Re: Compare Data Between worksheets
Well, it's doing stuff, but it's not the right stuff. I think it's just taking the Row with my name and pasting it on top of the forst Row of the Main sheet.
I need it to:
1)Check my name in the Row Sheets("Porter").Range("F3:200")
2)Hold the Project Number in a String on same row Sheets("Porter").Range("B3:B200")
3)Loop through the Projects in Sheets("LD").Range("E6") to Used Range
4)If it finds the Project in the LD "E" Column, it should move to the next row in the Porter sheet.
5)If it doesn't find the Project in the LD "E" Column it should place the string in the blank "E" Cell
6)5. will eventually be expanded to move Porter "D" info to LD "F", and Porter "E" info to LD "B"
Problems:
1)I'm seeing with select and paste is that I have a Selectionchange by Val on LD, and it's skipping to that Macro unnecessarily.
2)Why is it selecting the entire "E:E" column?
Code:
Sub Vldt()
Dim endRow As Integer
Dim newRow As Integer
Dim i As Integer
Dim MyName As String
Dim Rng As Range
MyName = "Thomas"
endRow = Sheets("Porter").Range("E1").End(xlDown).Row
newRow = Sheets("LD").Range("E1").End(xlDown).Row + 1
For i = 3 To endRow
If Sheets("Porter").Cells(i, 6) = MyName Then
Sheets("LD").Columns("E:E").Select
Set Rng = Selection.Find(What:=Sheets("LD").Cells(i, 2).Value)
If Rng Is Nothing Then
Sheets("Porter").Rows(i).Copy
Sheets("LD").Cells(newRow, 1).Select
ActiveSheet.Paste
newRow = newRow + 1
End If
End If
Next
End Sub
-
Re: Compare Data Between worksheets
to comply with your list do the following:
1) change the line to:
endRow = Sheets("Porter").Range("F1").End(xlDown).Row
this will not be limited to 200 rows as your example suggests, if that were the case then endRow = 200
2) That part is handled by the comparisson made in
If Sheets("Porter").Cells(i, 6) = MyName Then
But here we are considering that the name column is column "F, if it is "E than change the previous line and also here to .Cells(i, 5)
3) This line you changed wrong
Set Rng = Selection.Find(What:=Sheets("Porter").Cells(i, 2).Value)
The number two is expecting the program to be in column "B", if it is in another column adapt the number to it.
4 & 5) Set Rng Is Nothing takes care of that
your problems:
1) I did not understand what you mean.
2) Its selecting entire column E to search for the program, in my original example it was on column B because that is what you said in your original post. If the actual column that holds the programs is "E" you need to change
What:=Sheets("Porter").Cells(i, 5)
-
Re: Compare Data Between worksheets
It's Pasting the entire row from the Porter Sheet into the 1st Row of the LD sheet
I just need the project number Porter "B" to LD "E" in the next blank Row (if the project number isn't already in the LD "E" Column.
When I cycle through the Macro with the F8 Key, everytime it selects something it triggers the Selectionchange Macro, and it jumps from Macro to Macro. I guess it won't hurt anything, but I'd rather the Macro not select anything directly.
Can you work this into the Loop?
'Sheets("LD").Cells(i, 5) = Sheets("Porter").Cells(i, 2).Value
Code:
Sub Vldt()
Dim endRow As Integer
Dim newRow As Integer
Dim i As Integer
Dim MyName As String
Dim Rng As Range
MyName = "Thomas"
endRow = Sheets("Porter").Range("F1").End(xlDown).Row
newRow = Sheets("LD").Range("E:E").End(xlDown).Row + 1
For i = 3 To 200
If Sheets("Porter").Cells(i, 6) = MyName Then
Sheets("LD").Columns("E:E").Select
Set Rng = Selection.Find(What:=Sheets("Porter").Cells(i, 2).Value)
If Rng Is Nothing Then
Sheets("Porter").Rows(i).Copy
Sheets("LD").Cells(newRow, 1).Select
ActiveSheet.Paste
'Sheets("LD").Cells(i, 5) = Sheets("Porter").Cells(i, 2).Value '(Can you make this work?
newRow = newRow + 1
End If
End If
Next
End Sub
-
Re: Compare Data Between worksheets
I will try to do it without selecting but I think you should not use a macro with that event, it can put you in more trouble.
So you do not need the whole row, just the project number? it is originally in porter sheet in column B, and you need it in LD sheet in E column?
-
Re: Compare Data Between worksheets
try it like this:
vb.net Code:
Sub Vldt()
Dim endRow As Integer
Dim newRow As Integer
Dim i As Integer
Dim MyName As String
Dim Rng As Range
MyName = "Thomas"
endRow = Sheets("Porter").Range("F3").End(xlDown).Row
newRow = Sheets("LD").Range("E1").End(xlDown).Row + 1
For i = 3 To endRow
If Sheets("Porter").Cells(i, 6) = MyName Then
Set Rng = Sheets("LD").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value)
If Rng Is Nothing Then
Sheets("Porter").Cells(i, 2).Copy
ActiveSheet.Paste Destination:=Worksheets("LD").Cells(newRow, 5)
newRow = newRow + 1
End If
End If
Next i
End Sub
Note that in order for this to work, there must be at least two data in column F in Porter sheet with no black cells, the same for column E in LD sheet,
-
Re: Compare Data Between worksheets
yes that is correct. but of course only of my project designated by my name in porter column "F".
I was always told to not select and manipulate what you can do with code directly. Good Bad, I don't know, but I know it wouldn't trigger the Existing Selectionchange Macro I have in both sheets.
-
Re: Compare Data Between worksheets
I think you just missed my last post for less than a minute
-
Re: Compare Data Between worksheets
I would go with the code in my last post because it is clearer. But this is a compact version of the exact same algorithm.
vb.net Code:
Sub Vldt()
MyName = "Thomas"
newRow = Sheets("LD").Range("E1").End(xlDown).Row + 1
For i = 3 To Sheets("Porter").Range("F3").End(xlDown).Row
If Sheets("Porter").Cells(i, 6) = MyName And Sheets("LD").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value) Is Nothing Then
Sheets("Porter").Cells(i, 2).Copy
ActiveSheet.Paste Destination:=Worksheets("LD").Cells(newRow, 5)
newRow = newRow + 1
End If
Next i
End Sub
i
-
Re: Compare Data Between worksheets
It's starting on the LD "E6" and Pasting all my projects in order from the Porter sheet.
It's not Comparing the Porter Sheet project Numbers with the LD Project numbers that are already there, it's just pasting them all in order from Porter. It needs to put the project number from porter and put it in a String and compare as it loops down the LD sheet. I need a loop within a loop, and that's too hard for me.
-
Re: Compare Data Between worksheets
The short code isn't working.
New Row is always 6, and i loops from 3-6 then starts over. I can't tell it's doing anything to the LD sheet
-
Re: Compare Data Between worksheets
you do not need a loop. It only may be that we are not understanding each other right.
let me tell you exactly what this is doing so you can see where did I understand it wrong.
* You have two sheets: "LD" and "Porter"
* In the Porter sheet you have some data, where in column "F" there are some names and in column "B" project numbers.
* In the LD sheet you have the project numbers on column "E"
* The code first searches in order column "F" until it finds a match for your name ("Thomas")
* Then it looks in LD sheet, column E to see if the corresponding Program Number is already there, if not it adds it at the bottom of the list.
Is this what you want?
Things that may mess it up:
If Porter sheet, column F has less than 2 data starting from F3
If column F in porter sheet has blank cells
If column E in LD sheet has less than 2 data starting from E1
If column E in LD sheet has blank cells
If there are spaces (not blank) on cells in column E
-
Re: Compare Data Between worksheets
Your understanding Correctly, and the short code looks like it isn't duplicating the Projects, but it is still Pasting the Data starting with LD Line 6. Somehow it needs a command to where it knows the first open row and pastes there.
I don't like the Paste, because it messes with the Formatting of the LD sheet.
I may not need a Loop, but I'd rather have a Loop, String, Compare, Insert. But that's just me ;-)
Also, the reason it wasn't doing anything earlier, is I suppose because I deleted my name is Row 2 just to make sure it could handle it. I can understand why a Blank on the LD sheet would mess it up, but why does a Blank in the Porter sheet mess it up?
-
Re: Compare Data Between worksheets
OK. If you dont want the .paste lets just fill the data in the cell
don't use the short code. the other one is a bit more reliable.
vb.net Code:
Sub Vldt()
Dim endRow As Integer
Dim newRow As Integer
Dim i As Integer
Dim MyName As String
Dim Rng As Range
MyName = "Thomas"
endRow = Sheets("Porter").Range("F3").End(xlDown).Row
newRow = Sheets("LD").Range("E1").End(xlDown).Row + 1
For i = 3 To endRow
If Sheets("Porter").Cells(i, 6) = MyName Then
Set Rng = Sheets("LD").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value)
If Rng Is Nothing Then
Sheets("LD").Cells(newRow, 5).Formula = Sheets("Porter").Cells(i, 2).Value
newRow = newRow + 1
End If
End If
Next i
End Sub
The blanks would hurt because the list is looking for consecutive values. If there is a blank in the names column it will stop when it reaches it.
-
Re: Compare Data Between worksheets
It's still adding the projects in order from LD "E6" on down. I deleted Everything out of LD "E:E" and put AAABBBCCC in Porter "B5". I put the same AAABBBCCC in LD "E12". It detected it in the LD column, because it did not paste the value when it got to "E6", but later on it pasted something on top of "E12" anyways.
-
Re: Compare Data Between worksheets
That sounds to me like either there is nothing on some cells above E6. Is it possible that you post your sheet as an attachment?
-
Re: Compare Data Between worksheets
E1:E5 is my header field, I have buttons to run macros, and the Column Titles.
-
Re: Compare Data Between worksheets
Then make a this change:
newRow = Sheets("LD").Range("E6").End(xlDown).Row + 1
Now, data must be on at least 2 cells of "LD" sheet (E6 & E7), if there is not then we would need to add a few lines to the code. Also in "Porter" sheet, cells F3 and F4
If you are shure that E5 contains the header then you could put .Range("E5") in the above line and would only need one extra data on E6.
I must insist. Put a copy of your sheet, change some data and delete other code if you are concerned about your intelectual property or confidential data. But it would benefit me (us) to have a broad feeling of the actual layout.
-
1 Attachment(s)
Re: Compare Data Between worksheets
-
Re: Compare Data Between worksheets
It appears to work OK if you follow the steps in my last post. set the newRow according to "E5" and make sure there's at least one data in "E6"
-
Re: Compare Data Between worksheets
Just figured out a simple way of coding it without the need of any data in LD column E, or Porter column F
change rows 10 and 11 to
vb Code:
endRow = Sheets("Porter").Range("F10000").End(xlUp).Row
newRow = Sheets("LD").Range("E10000").End(xlUp).Row + 1
-
Re: Compare Data Between worksheets
Alright, this works. Now I need something else.. I need it to disregard my name, and look at the project numbers on both sheets, to take the Due Date from Porter "E" and update LD "B". That should help out a bunch.
-
Re: Compare Data Between worksheets
Quote:
Originally Posted by
tome10
Alright, this works. Now I need something else.. I need it to disregard my name, and look at the project numbers on both sheets, to take the Due Date from Porter "E" and update LD "B". That should help out a bunch.
lets see if I understand. after you do what you did with the previous code you need to search for the project numbers that you have in LD. and for each one update column B according on what that same project number has on column E in Porter Sheet.
Right?
-
Re: Compare Data Between worksheets
That is correct. Thanks for the help. ;-)
-
Re: Compare Data Between worksheets
I also have a problem with the original code. I have a History sheet with completed projects. It's the exact same layout as LD, but the Name is "Hist". Problem is the "Porter" Sheet seems to not get updated with Completions in a timely manner, and the code is just importing completed projects back into "LD".
So, I need it to look in "Hist" used range first, and then look in "LD" and if it can't find the Project, then import the data into "LD".
-
Re: Compare Data Between worksheets
I put this together, I used <> for if <is not> "this" I thought that was Legit, but it comes back with an error.
This is trying to cycle through the History sheet first and make sure the projects aren't there and then cycle through the LD sheet and if the project is not there then to transfer it to LD.
Code:
Sub PrLead()
Dim endRow As Integer
Dim newRow As Integer
Dim i As Integer
Dim MyName As String
Dim Rng As Range
Sheets("LD").ToggleButton1.Value = "True"
MyName = "Thomas"
endRow = Sheets("Porter").Range("F3").End(xlDown).Row
newRow = Sheets("LD").Range("E6").End(xlDown).Row + 1
For i = 3 To endRow
If Sheets("Porter").Cells(i, 6) = MyName Then
Set Rng = Sheets("Hist").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value)
If Rng <> Sheets("Porter").Cells(i, 2).Value Then ' it hangs up here Object Variable with Block Variable not set
Set Rng = Sheets("LD").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value)
If Rng <> Sheets("Porter").Cells(i, 2).Value Then
Sheets("LD").Cells(newRow, 5).Formula = Sheets("Porter").Cells(i, 2).Value
Sheets("LD").Cells(newRow, 2).Formula = Sheets("Porter").Cells(i, 5).Value
Sheets("LD").Cells(newRow, 6).Formula = Sheets("Porter").Cells(i, 4).Value
newRow = newRow + 1
End If
End If
End If
Next i
Sheets("LD").ToggleButton1.Value = "False"
End Sub
-
Re: Compare Data Between worksheets
You are in the right track, except that in the line in question you are trying to assing the value of a cell to a range, just use a variable of the type of the contents of the cells instead of Rng which is declared as a Range
-
Re: Compare Data Between worksheets
I'm not really sure what your saying, so, can you help me out? ;-)
-
Re: Compare Data Between worksheets
Quote:
Originally Posted by
tome10
I'm not really sure what your saying, so, can you help me out? ;-)
When you declare Rng it is defined as range
And you assign it the range resulting of a find
Code:
Set Rng = Sheets("Hist").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value)
But then you compare it to a cell's value:
Code:
If Rng <> Sheets("Porter").Cells(i, 2).Value Then
Rng holds a range, like A1:C200 or the like, whilest Sheets("Porter").Cells(i, 2).Value holds a value, like 10 or 134.4 or "John". These are two different types of object and can't compare them. What you need is a way to extract the value of the first cell of Rng, a range can be any size, in this case it is just one cell but VBA has no way of knowing this until it executes. And even then, you are not asking for its value but the range. Is this somewhat clear?
-
Re: Compare Data Between worksheets
Alright, so this is doing 2 things. Setting the range to look in and finding the project number.
Quote:
Set Rng = Sheets("Hist").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value)
So I need something to say; if it doesn't find the project number to change the range to the LD sheet and look there.
How do I do that?
-
Re: Compare Data Between worksheets
Quote:
Originally Posted by
tome10
Alright, so this is doing 2 things. Setting the range to look in and finding the project number.
So I need something to say; if it doesn't find the project number to change the range to the LD sheet and look there.
How do I do that?
Yes, this part is ok because you are assigning a range to a variable of type range, but then you compare the range to a value.
What you need to do in the other statement is to look for the value of a cell inside the range and compare it to the other value. You cannot compare the characteristics of a basket to the contents of another one, makes no sense.
what you are saying with If Rng <> Sheets("Porter").Cells(i, 2).Value Then is like saying If a big basket is the same as 12 oranges Then, when what you should be saying is If the contents of the basket is the same as 12 oranges Then, am I making my self clear? sorry for all the confussion but I don't know how to explain it. Anyway you should code that like somethink like this:
Code:
If Rng(1.1).Value <> Sheets("Porter").Cells(i, 2).Value Then
-
Re: Compare Data Between worksheets
I have to ask.. What does the (1.1) represent.
-
Re: Compare Data Between worksheets
Sorry, it should be (1,1). it is the first cell in the range (row 1, cell 1), in this case its the only one but still, you need to reference the value of a cell in a range to compare it to a value.
It does not represent "A1" since it is confined to Rng
-
Re: Compare Data Between worksheets
Alright, there's something wrong.. It errors saying Rng=Nothing.
Code:
If Rng(1, 1).Value <> Sheets("Porter").Cells(i, 2).Value Then 'Errors here
Code:
Sub PrLead()
Dim endRow As Integer
Dim newRow As Integer
Dim i As Integer
Dim MyName As String
Dim Rng As Range
Sheets("LD").ToggleButton1.Value = "True"
MyName = "Thomas"
endRow = Sheets("Porter").Range("F3").End(xlDown).Row
newRow = Sheets("LD").Range("E6").End(xlDown).Row + 1
For i = 3 To endRow
If Sheets("Porter").Cells(i, 6) = MyName Then
Set Rng = Sheets("Hist").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value)
If Rng(1, 1).Value <> Sheets("Porter").Cells(i, 2).Value Then 'Errors here Says Range = Nothing
Set Rng = Sheets("LD").Columns("E:E").Find(What:=Sheets("Porter").Cells(i, 2).Value)
If Rng(1, 1).Value <> Sheets("Porter").Cells(i, 2).Value Then
Sheets("LD").Cells(newRow, 5).Formula = Sheets("Porter").Cells(i, 2).Value
Sheets("LD").Cells(newRow, 2).Formula = Sheets("Porter").Cells(i, 5).Value
Sheets("LD").Cells(newRow, 6).Formula = Sheets("Porter").Cells(i, 4).Value
newRow = newRow + 1
End If
End If
End If
Next i
Sheets("LD").ToggleButton1.Value = "False"
End Sub
-
Re: Compare Data Between worksheets
that would happen if the previous statement, which is a "Find" does not find anything. Then the range would be nothing. Going back to the basket of oranges, Rng would have no basket, so you can't look inside the basket if there is none.
-
Re: Compare Data Between worksheets
I have the first/main part working, thanks for the help..
This part is the one that has to look up the proj numbers from LD Col "E" and find them in the porter sheet Col "B" and bring the Due Dates Porter Col "E" over to LD Col "B".
It's kinda working. Seems Random at best, but it is bringing every date over from Porter which is a start, but it is not comparing the Project numbers and it needs something to say ignore blank cells,and only go after the dates for project numbers existing in the LD sheet.
Code:
For i = 6 To endRow
Set Rng = Sheets("Porter").Columns("B:B").Find(What:=Sheets("LD").Cells(i, 5).Value)
Sheets("LD").Cells(i, 2).Formula = Sheets("Porter").Cells(i, 5).Value
Next i
-
Re: Compare Data Between worksheets
before you compare check for nothingness. As the Find method may not find what you are looking for and thus be "nothing", in that case you should not compare it to anything (it appears that you took out that line, but you need it so it will only copy what you want). Before you do that, you must make sure Rng has something, meaning it found a value in the range.
If Rng Is Nothing Then
-
Re: Compare Data Between worksheets
I put the code in a Module by itself so I can see what it's doing.. The Rng is coming back Null I think.
It should be cycling through the LD Col "E" and finding the matching numbers in Porter Col "B". So, what's wrong?
Code:
Sub Dates()
Dim i As Integer
Dim endRow As Integer
Dim Rng As Range
For i = 6 To endRow
Set Rng = Sheets("Porter").Columns("B:B").Find(What:=Sheets("LD").Cells(i, 5).Value)
Sheets("LD").Cells(i, 2).Formula = Sheets("Porter").Cells(i, 5).Value
Next i
End Sub
-
Re: Compare Data Between worksheets
In your recent code you use Rng to set the range of the result of the find. But on the next line you simply assign the value of a cell in "Porter" to another cell in "LD" without taking into consideration the value of Rng. In fact, you never use that value making the line pointless.
In earlier versions of your program you were checking to see if the searched value was found and then do the copy. The problem there is that when it did not find the searched item it was assigned "nothing" thus making the comparisson fail.
What you want to do is only copy when it finds it, so it would be something like:
vb Code:
For i = 6 To endRow
Set Rng = Sheets("Porter").Columns("B:B").Find(What:=Sheets("LD").Cells(i, 5).Value)
If Not(Rng is nothing) then
Sheets("LD").Cells(i, 2).Formula = Sheets("Porter").Cells(i, 5).Value
End If
Next i
-
Re: Compare Data Between worksheets
It isn't doing anything. When I cycle through with f8, it jumps from For i to End Sub. ideas?
Code:
Sub Dates()
Dim i As Integer
Dim endRow As Integer
Dim Rng As Range
For i = 6 To endRow
Set Rng = Sheets("Porter").Columns("B:B").Find(What:=Sheets("LD").Cells(i, 5).Value)
If Not (Rng Is Nothing) Then
Sheets("LD").Cells(i, 2).Formula = Sheets("Porter").Cells(i, 5).Value
End If
Next i
End Sub
-
Re: Compare Data Between worksheets
I couldn't figure out what was wrong with the other code, so I started working with a piece of code I have from another project and finally got it to working.. Almost.. Only thing this is doing wrong is that it's out of sync.. LD sheet starts at Row 6, and Porter sheet starts on Row 3. So it's 3 behind. I think i'm supposed to dim another letter "c", and say c=3, but I can't figure out how I'm supposed to make it count. it's just stuck filling in all the LD cells with the first porter Row Date..
Code:
Sub Dates()
Dim i As Integer, c As Integer
'Dim endRow As Integer
'Dim Rng As Range
Dim ws As Worksheet, aCell As Range
Set ws = Sheets("Porter")
c = 3
For i = 6 To 200 'endRow
If Sheets("LD").Cells(i, 5).Value <> "" Then
Set aCell = ws.Range("B3:B200").Find(What:=Sheets("LD").Cells(i, 5).Value, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Sheets("LD").Cells(i, 2).Formula = Sheets("Porter").Cells(c, 5).Value
End If
End If
Next
End Sub
-
Re: Compare Data Between worksheets
Yes, in this little example you left out the calculation of endRow, so the loop can't go from 6 to 0 the way its setup. the calculation of endRow is important so you do not need to set the loop to 200.
you do not need another variable, just when referencing the Porter Sheet deduct 3 from i
Code:
Sheets("LD").Cells(i, 2).Formula = Sheets("Porter").Cells(i-3, 5).Value
-
Re: Compare Data Between worksheets
1) I can't make the Endrow work at all, it just refuses to work. 2) it's not finding the Project number and bringing over the corresponding date. It seems to be just going down the line and filling in the dates in order from the porter sheet. So I'm missing something.
How do I make this work?
How do I make it bring back the row it found the project number in? i.row being the Row in Porter where it found the project number. It's not activerow, because that always brings back the endrow.
Code:
MsgBox Sheets("LD").Cells(i, 5).Value & " Found. " & i.Row
Code:
Sub Dates()
Dim i As Integer
'Dim endRow As Integer
'Dim Rng As Range
Dim ws As Worksheet, aCell As Range
Set ws = Sheets("Porter")
For i = 6 To 200 'endRow
If Sheets("LD").Cells(i, 5).Value <> "" Then
Set aCell = ws.Range("B3:B200").Find(What:=Sheets("LD").Cells(i, 5).Value, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Sheets("LD").Cells(i, 2).Formula = Sheets("Porter").Cells(i - 3, 5).Value
MsgBox Sheets("LD").Cells(i, 5).Value & " Found. " & i.Row
End If
End If
Next
End Sub
-
Re: Compare Data Between worksheets
OK, why dont you post as attachment the project as it is now so I can take a look at it with latest additions. I have been trying to direct you in the direction I think is best. Let me now try to solve the issue as for some reason it is eluding us.
you are assigning the result of the .Find to aCell, so the found row must be aCell.Row, i.Row makes no sense as i is just an integer counter.
-
Re: Compare Data Between worksheets
Once you told me that it was the aCell I should be looking for, I got it to work. with aCell.offset(0,3).value
So the only thing not working now is the End Row Loop. Why did it stop working? Shouldn't it have an xlUp command or something?
Code:
Sub Dates()
Dim i As Integer, c As Integer
'Dim endRow As Integer
'Dim Rng As Range
Dim ws As Worksheet, aCell As Range
Set ws = Sheets("Porter")
For i = 6 To 200 'endRow
If Sheets("LD").Cells(i, 5).Value <> "" Then
Set aCell = ws.Range("B3:B200").Find(What:=Sheets("LD").Cells(i, 5).Value, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Sheets("LD").Cells(i, 2).Formula = aCell.Offset(0, 3).Value
'MsgBox Sheets("LD").Cells(i, 5).Value & " Found. " & aCell.Row & aCell.Offset(0, 3).Value
End If
End If
Next
End Sub
-
Re: Compare Data Between worksheets
The problem is that you took out the calculation of endRow and in your code endRow is always zero. before the start of the loop you must determine the endRow (which is the last row that has data)
vb Code:
endRow = Sheets("Porter").Range("E1").End(xlDown).Row
-
Re: Compare Data Between worksheets
your code would currently be something like
vb Code:
Sub Dates()
Dim i As Integer, c As Integer
Dim endRow As Integer
Dim ws As Worksheet, aCell As Range
Set ws = Sheets("Porter")
endRow = ws.Range("E10000").End(xlUp).Row
For i = 6 To endRow
If Sheets("LD").Cells(i, 5).Value <> "" Then
Set aCell = ws.Range("B3:B200").Find(What:=Sheets("LD").Cells(i, 5).Value, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Sheets("LD").Cells(i, 2).Formula = aCell.Offset(0, 3).Value
End If
End If
Next
End Sub
I still think you should send me the workbook so I can do a thorough annalysis of it.