Link to home
Start Free TrialLog in
Avatar of MikeYoungMoon
MikeYoungMoon

asked on

how to find the bug in my VBA code. everything is ok, but the code still do not work.

I had this question after viewing VBA Challenge how to code to loop through all files in folder and revise some part of the codes.

i have used the F8 key to debug line by line the code in the attached workbook named "template.xlsb" everything looks perfect but still it does not replace the code.  

basically,  the VBA is to find the text match in D2 and replace it with E2. the code replacement to be done on test file workbook Split-Data-Based-on-Column-Value.xlsm

i would appreciate if someone could help me find the bug why the code does not work.
Split-Data-Based-on-Column-Value.xlsm
template.xlsb
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MikeYoungMoon
MikeYoungMoon

ASKER

thanks Bill.

i am relatively new with the whole VBA thing. the code above was helped by MacroShadow and i have no idea how to fix it, it looks to me quite complex.

thank you for pointing the difference on the vbCr & vbCrLf   as i could have never found this problem. becuase when i used the len(string) function both of text in D2 and inside module returned the same length of characters.  i have no idea how to fix this. is this something too difficult?

I'm also not sure you want WholeWord mode, did you do any testing with that?  It might be okay, just another thing to watch out for.
 i am not sure either whether i want wholeword mode or not. i did not know how to test that so i did not do any testing.

i agree, this code also looks puzzling to me, as i could not decipher its logic and how it works. i could not come up with an alternative way to have this fixed
Found = .Find(target:=strFind, StartLine:=SL, StartColumn:=SC, EndLine:=EL, EndColumn:=EC, wholeword:=True, MatchCase:=False, patternsearch:=False)
        Do Until Found = False
            Debug.Print "Found at: Line: " & CStr(SL) & " Column: " & CStr(SC)
            EL = .CountOfLines
            SC = EC + 1
            EC = 255
            Found = .Find(target:=strFind, StartLine:=SL, StartColumn:=SC, EndLine:=EL, EndColumn:=EC, wholeword:=True, MatchCase:=False, patternsearch:=False)
        Loop

i spent a lot of time trying to fix this code and in addition searched for hours  googling to see if i could find anything, it is just too difficult to solve this mystery.
Yes, code like this can be delicate and hard to get right.  I did some quick testing here, it doesn't look to me like the .Find method being used supports multiple lines of code.  That could be worked around by extracting the module code to a string variable, and then using Instr() on it to see if the strFind code from the worksheet cell is contained but I can't spend the amount of time ot would take to get this right and working and tested for a question.

If you really need this then you could post it as Gig and pay for the work, and I or someone might be able to spend the hours this will take to get it right.  Sorry I can't be of more help on this question at the moment, please understand what you are asking for is a large bit of work, and not just the answer to a straight forward question.  I hope what I have been able to share so far has been helpful.

~bp
I think this prior answer might be a better starting point for some of the search and replacement logic, and could be simpler.

https://www.experts-exchange.com/questions/27835744/Bulk-changing-of-VBA-code.html?anchorAnswerId=38332592#a38332592

~bp
thanks Bill,

i tried searching in EE, i found the thread  by Chris Bottomley  which uses part of the Found = .Find(target:=strFind, StartLine:=SL, StartColumn:=SC, EndLine:=EL, EndColumn:=EC  i tried playing with it, could not make it work.
i will post  a new question. thanks Bill
You might also find this interesting, or want to reach out to the member to see if there is an opportunity to collaborate, sounds like they are doing something similar to you...

https://www.experts-exchange.com/questions/29005864/Changing-a-line-of-VBA-using-VBA.html

~bp
thanks Bill