我写了如下代码:
Call search(xx, yy, "APM Output", ">> State Scalars", label1)
label1:
...........
这是Sub搜索的脚本
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搜索中,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
答案 0 :(得分:3)
你的潜艇对我来说没有多大意义
">> State Scalars"
它存在For
循环并转到label1
">> State Scalars"
到达label1
您可以使用以下代码
<强> 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
答案 1 :(得分:1)
作为一种良好的做法,请不惜一切代价避免使用标签!
我将回答你只是修改你的代码,我想你想要保存的值 XX,YY,UU,VV,毫米,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")