VBA删除部分代码,并将其放入单独的过程中
我绝对不知道如何创build单独的子/ functions
来缩短code
。 我指的是那些子(如整数等)
下面我们有这个code
驻留在我的核心module
Set els = IE.Document.getelementsbytagname("a") For Each el In els If Trim(el.innertext) = "Documents" Then colDocLinks.Add el.href End If Next el For Each XML_link In colDocLinks LoadPage IE, CStr(XML_link) For Each el In IE.Document.getelementsbytagname("a") If el.href Like "*[0-9].xml" Then With Worksheets("CONTROL_ROOM").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) .NumberFormat = "@" .Value = Ticker .Offset(0, 1).Value = el.href End With Debug.Print el.innertext, el.href colXMLPaths.Add el.href End If Next el Next XML_link
我真的需要缩短我的code
。 我怎么能创build一个单独的sub
或function
而不是有这块code
到我的主要module
?
书籍提供了过于简单化的例子,并没有像这样的实际情况对我有任何帮助 。 我是否需要在单独的Sub
或Function
声明Dim els
? 感谢您的耐心提前。
而最重要的是,无论我看这些例子多less次,我都无法弄清楚我把这些variables放在这里:
(Private) Sub / (Private) Function ( variables ?)
+++任何好的示例/链接都将有所帮助。
无论何时您想调用一段代码来执行某些操作,都无需向调用它的代码返回任何types的值,就可以创build一个子例程:
Sub MainCode()
Dim myString as String ...'all your main code Call OtherSub(myString) ...'all your main code
结束小组
Sub OtherSub(theString as String)
'Do Something with the string theString
结束小组
创build一个函数,当你想返回的东西:
Sub MainCode()
Dim myString as String, newString as String ...'all your main code NewString = OtherSub(myString) ...'all your main code
结束小组
函数ManipulateString(theString as String)
'Do Something with the string theString ManipulateString = theString & ...
结束function
在函数结束时,要返回新的值,只需将函数名称设置为等于传回的任何值即可。
希望有所帮助。