简体   繁体   中英

Modifying VBA macro with a macro

  • there are quite a few different excel macro files with a link that has been updated. Is there a way to modify a macro with a macro?

  • This is what I have been able to find so far using CodeModule, find line where the link is at and then replace the whole line. I have this macro in one file, and I run it in a file that I want to change (later I may automate it to go through all files in a folder), but need to get it working first.

  • One part that is confusing me, at which point does SL (starting line) gets changed?

  • What are CodeModule columns? My link is say 35characters long, why is the column 35, should it not be 1?

  • Is there a better way? Thanks

    Sub findLink() Dim VBProj As VBIDE.VBProject Dim VBComp As VBIDE.VBComponent Dim CodeMod As VBIDE.CodeModule Dim FindWhat As String Dim SL As Long ' start line Dim EL As Long ' end line Dim SC As Long ' start column Dim EC As Long ' end column Dim Found As Boolean Set VBProj = ActiveWorkbook.VBProject Set VBComp = VBProj.VBComponents("Module1") Set CodeMod = VBComp.CodeModule 'find text to replace FindWhat = "https://www.abc.net.au/" With CodeMod SL = 1 EL = .CountOfLines SC = 1 EC = 255 Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _ EndLine:=EL, EndColumn:=EC, _ wholeword:=True, MatchCase:=False, patternsearch:=False) Do Until Found = False MsgBox CStr(SL) Debug.Print "Found at: Line: " & CStr(SL) & " Column: " & CStr(SC) EL = .CountOfLines SC = EC + 1 EC = 255 Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _ EndLine:=EL, EndColumn:=EC, _ wholeword:=True, MatchCase:=False, patternsearch:=False) Loop 'If Found Then Call .ReplaceLine(SL, "Link = ""https://www.abc2.net.au/"" ") End With MsgBox "Found at: Line: " & CStr(SL) & " Column: " & CStr(SC) End Sub
  1. " One part that is confusing me, at which point does SL (starting line) gets changed? "

When a match is found it is returned/changed like the line number on which the matching text is found . Independent of its initial value. So, when trying to search from the beginning, it is enough to only declare the variable .

And, in order to let the code searching the rest of the lines, you should increment this returned line number/variable :

So, you should replace:

SC = EC + 1

with:

SL = SL + 1 'to start searching from the next code line
  1. " What are CodeModule columns? My link is say 35characters long, why is the column 35, should it not be 1? "

Well, "Each character in a code line is in a separate column , beginning with zero on the left code line side. If a match is found, the value of the StartColumn argument is set to the column in which the beginning character of the matching string is found.

So, if your code returns 35 (as StartColumn , after the match) it means that there are other 34 characters before the first one of the link string. Space characters are also counted...

If the respective code line contains other words which should be kept, the code may be adapted to change only the necessary string.

And, if there are more occurrences, your code (without the above suggested modification) will return only the first one. After modification, the final MsgBox will return only the last occurrence ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM