excel - VBA Remove a part of the code and put him into a separate procedure -


i have absolutely no idea how create separate subs/functions shorten code. referring subs(something integer, etc)

below have code resides in core module

set els = ie.document.getelementsbytagname("a")     each el in els         if trim(el.innertext) = "documents"             coldoclinks.add el.href         end if     next el      each xml_link in coldoclinks         loadpage ie, cstr(xml_link)         each el in ie.document.getelementsbytagname("a")             if el.href "*[0-9].xml"                 worksheets("control_room").cells(rows.count, 1).end(xlup).offset(1, 0)                     .numberformat = "@"                     .value = ticker                     .offset(0, 1).value = el.href                 end                 debug.print el.innertext, el.href                 colxmlpaths.add el.href             end if         next el     next xml_link 

i need shorten code. how create separate sub or function instead of having chunk of code main module?

books offer over-simplistic examples , have not been me in real situations one. need make declarations such dim els inside separate sub or function? thank patience in advance.

and importantly no-matter how time these examples cannot figure out variables put in here:

(private) sub / (private) function ( variables ?) 

+++any examples/links help.

create subroutine anytime want able call block of code something, without returning kind of value code called it:

sub maincode()

dim mystring string  ...'all main code  call othersub(mystring)  ...'all main code 

end sub

sub othersub(thestring string)

'do string thestring 

end sub

create function when want return something:

sub maincode()

 dim mystring string, newstring string  ...'all main code  newstring = othersub(mystring)  ...'all main code 

end sub

function manipulatestring(thestring string)

'do string thestring   manipulatestring = thestring & ... 

end function

at end of function, return new value, set function name equal whatever passing back.

hope helps.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -