2012-09-20 141 views
0

我寫的代碼如下:Excel宏:ByRef參數類型不匹配

Call search(xx, yy, "APM Output", ">> State Scalars", label1) 

label1: 
     ........... 

Sub search(row As Variant, col As Variant, wkst As String, str As String, label_num As Name) 
For row = 1 To 100 
    For col = 1 To 100 
    strTemp = Worksheets(wkst).Cells(row, col).Value 
    If InStr(strTemp, str) <> 0 Then 
     GoTo label_num 
    End If 
    Next 
Next 
End Sub 

我想打電話給子搜索(..)子搜索的腳本首先,和然後轉到label1。 問題是說label_num的「ByRef參數類型不匹配」。什麼應該是sub_num搜索中label_num的正確類型(..,..,..,label_num)?

我加入一些原創劇本,這些都是我想要的東西轉換成子()

For xx = 1 To 100 
    For yy = 1 To 100 
     strTemp = Worksheets("APM Output").Cells(xx, yy).Value 
     If InStr(strTemp, ">> State Scalars") <> 0 Then 
      GoTo label1 
     End If 
    Next 
Next 
label1: 
    For uu = 1 To 100 
     For vv = 1 To 100 
      strTemp = Worksheets("APM Output").Cells(uu, vv).Value 
      If InStr(strTemp, ">> GPU LPML") <> 0 Then 
       GoTo label2 
      End If 
     Next 
    Next 
label2: 
    For mm = 1 To 100 
     For nn = 1 To 100 
      strTemp = Worksheets("APM Output").Cells(mm, nn).Value 
      If InStr(strTemp, ">> Limits and Equations") <> 0 Then 
       GoTo label3 
      End If 
     Next 
    Next 
+1

Label1不是'Name'。因此你的錯誤。但即使如此,你也不能以這種方式使用它來退出一個子,並直接進入第二個子的定義位置 – brettdj

+0

哦,我明白了。順便說一下,你能給我一些建議,如何寫這個子?我添加了我的腳本,並且我已經測試過了,它們可以毫無問題地運行。我想將它們總結爲呼叫子() – TJLD22

回答

1

作爲一個良好的做法,請不要使用標籤,不惜一切代價!

我會回答你剛纔修改你的代碼,我猜你要保存的 XX值,YY,UU,VV,MM,NN

下面的代碼是如何避免使用標籤

Dim found1 As Boolean 
Dim found2 As Boolean 
Dim found3 As Boolean 
    found1 = False 
    found2 = False 
    found3 = False 
    For i = 1 To 100 
    For j = 1 To 100 
     strTemp = Worksheets("APM Output").Cells(i, j).Value 
     If InStr(strTemp, ">> State Scalars") <> 0 And Not found1 Then 
      found1 = True 
      xx = i 
      yy = j 
     End If 

     If InStr(strTemp, ">> GPU LPML") <> 0 And Not found2 Then 
      found2 = True 
      uu = i 
      vv = j 
     End If 
     If InStr(strTemp, ">> Limits and Equations") <> 0 And Not found3 Then 
      found3 = True 
      mm = i 
      nn = j 
     End If 
    Next j 
Next i 

,使你的函數成子,根本就

Sub my_search(ByRef rowNum As Long, ByRef colNum As Long, ByVal searchString As String, ByVal height As Long, ByVal width As Long, ByRef ws As Worksheet) 
Dim i As Long 
Dim j As Long 
Dim found As Boolean 
found = False 
Dim strTemp 
With ws 
    For i = 1 To height 
     For j = 1 To width 
      strTemp = ws.Cells(i, j).Value 
      If InStr(strTemp, searchString) <> 0 And Not found1 Then 
       found = True 
       rowNum = i 'assigning rowNum 
       colNum = j 'assigning colNum 
       Exit For 
      End If 
     Next j 
     If found Then 
     Exit For 
     End If 
    Next i 

End With 
End Sub 

,並調用它的3倍, 例如:

my_search xx,yy,">>State Scalars", 100, 100, Worksheets("APM Output") 
3

你個子並沒有多大意義,我作爲

  1. 如果發現">> State Scalars"它存在For循環並且去label1
  2. 如果它沒有找到">> State Scalars"無論如何到達label1

你可以使用下面

  • 搜索代碼的100單杆100單元格區域
  • 要麼就會發現你的部分字符串匹配或不
  • 它可以很容易地修改,停止後一個成功的搜索(或失敗)的搜索

code

Sub ReCut() 
Dim rng1 As Range 
Dim rng2 As Range 
Set rng2 = Worksheets("APM Output").Range("A1:CV100") 
Set rng1 = rng2.Find(">> State Scalars", , xlValues, xlPart) 
If Not rng1 Is Nothing Then 
MsgBox "Found >> State Scalars value at " & rng1.Address(0, 0) & vbNewLine & "This is the equivalent of your exit on INSTR" 
Else 
End If 
Set rng1 = rng2.Find(">> GPU LPML", , xlValues, xlPart) 
If Not rng1 Is Nothing Then 
MsgBox "Found >> GPU LPML value at " & rng1.Address(0, 0) & vbNewLine & "This is the equivalent of your exit on INSTR" 
Else 
End If 
End Sub 
+0

+ 1。我同意。 '。找到'是更快:) –