2016-07-06 55 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

之前的所有子過程的伎倆。希望這可以幫助有同樣問題的其他人。