0

我写了如下代码:

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

我想先调用 sub search(..),然后转到 label1。问题是说 label_num 的“ByRef 参数类型不匹配”。Sub search(..,..,.., label_num ) 中 label_num 的正确类型应该是什么?

我添加了一些原始脚本,这些是我想要转换成 sub()

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
4

2 回答 2

3

你的潜艇对我来说没有多大意义

  1. 如果它发现">> State Scalars"它存在For循环并转到label1
  2. ">> State Scalars"如果无论如何label1都找不到

您可以使用下面的代码

  • 一次搜索您的 100 x 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
于 2012-09-20T05:41:05.217 回答
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")
于 2012-09-24T05:10:35.460 回答