Dear members,
Sorry to disturb you again. I have two code snippets that are identical (except for one variable name):
Snippet 2
Snippet 1For i = 2 To Application.CountA(Range("A:A")) For j = 2 To Application.CountA(Range("A:A")) If TO_Sheet_OFA.Range("A" & i) = Filename_TO.Sheets("Extra_days").Range("A" & j) Then TO_Sheet_OFA.Range("F" & i) = Filename_TO.Sheets("Extra_days").Range("B" & j) End If Next j Next iSnippet 1 works, and copies the right information. Snippet 2 doesn't work, as column F remains empty after the code has been executed.For i = 2 To Application.CountA(Range("A:A")) For j = 2 To Application.CountA(Range("A:A")) If TO_Sheet_CTO.Range("A" & i) = Filename_TO.Sheets("Extra_days").Range("A" & j) Then TO_Sheet_CTO.Range("F" & i) = Filename_TO.Sheets("Extra_days").Range("B" & j) End If Next j Next i
My suspicion at first that the source file (Filename_From, not shown in the text above) might be corrupted. I recreated the file, but that did not help.
I have attached the two code files in their entirity below. What are your theories?
EDIT: added workbooks.
Last edited by nErD; 01-04-2012 at 07:46 AM. Reason: Adding files
Why would you have two identical pieces of code barring a variable name?
Why not use one piece of code, the piece that works, and pass the sheet name to it as a variable? One variable that contains the sheet name.
You're doubling the maintenance effort and, as you are finding out, the time to debug code that works in one place but not in another.
Regards, TMS
TMSucks: indeed, you have a good point. The issue is, the two codes pertain to two user forms. The respective user forms are different, and depend on a user choise executed in a seperate module (this code is part of a much larger module I am working with).
My working theory is still that one of the files must be corrupted somehow ... one or several cells barring the way. Right now I am confounded. I will let you know if I figure it out! I will leave this thread open in the mean time.
Why don't you attach the workbook?
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Probably this suffices, assuming 'TO_Sheet_OFA' refers to a worksheet
Basically if you use more than 1 workbook you should refer to those workbooks properly, the same applies to worksheets:For each cl in TO_Sheet_OFA.columns(1).specialcells(2) Filename_TO.Sheets("Extra_days").columns(1).find(cl.value,,xlvalues,xlwhole).offset(,5)=cl.offset(,1).value next
For each cl in workbooks("book1.xls").sheets("sheet12").columns(1).specialcells(2) workbooks("book2.xls").sheets("sheet4").columns(1).find(cl.value,,xlvalues, xlwhole).offset(,5)=cl.offset(,1).value next
Last edited by snb; 01-04-2012 at 06:40 AM.
I'm clearly missing something as there's no forms or code in either of the templates that you have uploaded.
Regards, TMS
Sorry if I have confused you TMShucks!
snb: indeed, I discovered my error and it was due to bad referncing from my part! My lesson of the day: lean how to reference properly.
Here is the solution:
Thanks! I hope this can help someone else; I will tag it with the word "referencing".Filename_TO.Sheets("Extra_days").Activate For i = 2 To Application.CountA(Range("A:A")) For j = 2 To Application.CountA(Range("A:A")) If TO_Sheet_OFA.Range("A" & i) = Filename_TO.Sheets("Extra_days").Range("A" & j) Then TO_Sheet_OFA.Range("F" & i) = Filename_TO.Sheets("Extra_days").Range("B" & j) End If Next j Next i
In VBA you'd better not use 'activate' nor 'select'.
You can refer to workbooks, worksheets directly as I showed in my example. As yuo can see you don't even need objectvariables to accomplish this.
You are correct snb; as I was not 100% sure of how you code worked, I used the activate choice to get to work. I will try to modify it according to your code when I understand more about how it works (never done a loop like the one you posted).
snb: I forgot to ask, what is cl in this example?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks