2016-07-06 83 views
1

我有两个子。第一个子(GetKeyWord)向用户询问关键字并将它们存储在一个字符串数组中。下一个子(AlertFinder)接收特定的字符串数组并在网页中搜索它。但是,我希望AlertFinder每运行几分钟,但我不希望用户询问每次运行AlertFinder时他/她要查找哪些关键字(只需在开始时询问用户一次并获得字符串之后数组保持不变)。这就是为什么我将GetKeyWord作为一个单独的子集,但现在我无法使AlertFinder运行,因为它要求GetKeyWord的字符串数组运行。VBA将变量传递给重复子

下面是代码:

Sub GetKeyWords() 


Dim numKey As Integer 
Dim strTemp() As Variant 

'Input Keywords 

numKey = InputBox("How many keywords would you like to search for? (Integer)", "Integer Value Please") 




For k = 1 To numKey 
    ReDim Preserve strTemp(numKey - 1) 
    strTemp(k - 1) = InputBox("Please enter keyword" & k) 


Next 


'Execute Alert Finder 
Call AlertFinder(strTemp) 
End Sub 


Sub AlertFinder(strTemp() As Variant) 


'Set Variables 
Dim boolFound As Boolean 
Dim txt As String 
Dim strOutput As String 
Dim tbl As HTMLTable, tables As IHTMLElementCollection 
Dim tr As HTMLTableRow, r As Integer, i As Integer 
Dim tRows As IHTMLElementCollection 
Dim ie As InternetExplorer 
Dim strCurrent As Variant 

~bunch of code~ 

    Set ieDoc = ie.Document 

    'Loop to refresh webpage every 25 minutes 
    Do While True 

     'Pause the script for x minutes 
     Application.Wait (Now + TimeValue("00:05:00")) 

     'AFter time is up, reload page and run Alert Finder Again 
     ieDoc.Location.Reload (True) 
     AlertFinder (strTemp) 

     If Err <> 0 Then 

      Wscript.Quit 
     End If 
    Loop 

    Set ie = Nothing 
    End Sub 

麻烦出现在那里我打电话内AlertFinder本身AlertFinder(strTemp),但strTemp来自GetKeyWord我要保持不变,并没有运行GetKeyWord每5分钟。任何帮助,将不胜感激!

+0

很难说如果没有看到“〜一堆代码〜”,但是你应该从'Sub AlertFinder'将循环提取到它自己的'Sub'中,而不是使用递归。 – Comintern

+0

谢谢!你介意阐述吗? –

+0

该循环保持脚本活着。没有理由再次调用AlertFinder(strTemp)。发生的情况是每5分钟创建一个AlertFinder副本。在5分钟内,你有2个AlertFinder正在运行,10分钟内有4个副本,15分钟内有16个副本....等等。 – 2016-07-06 18:35:26

回答

0

感谢那些评论。我最终做的是将strTemp声明为公共变量,以便它可以在GetKeyword中赋值,并传递到AlertFinder,而AlertFinder也可以调用它自己(然后删除在GetKeyword子集中声明的所有strTemp()的实例)。工作得很好。

公共strTemp()为Variant

之前的所有子过程的伎俩。希望这可以帮助有同样问题的其他人。