excel 宏:ByRef 参数类型不匹配
Posted
技术标签:
【中文标题】excel 宏:ByRef 参数类型不匹配【英文标题】:excel macro: ByRef argument type mismatch 【发布时间】:2012-09-20 04:54:44 【问题描述】:我写了如下代码:
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
【问题讨论】:
Label1 不是Name
。因此你的错误。但即便如此,你也不能用这种方式退出一个子,直接进入第二个子的定义位置
哦,我明白了。顺便说一句,你能给我一些建议如何写这个子吗?我添加了我的脚本,并且我已经测试过了,它们可以毫无问题地运行。我想把它们总结成一个调用 sub()
【参考方案1】:
你的潜艇对我来说没有多大意义
-
如果它找到
">> State Scalars"
它存在For
循环并转到label1
如果它没有找到">> 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
【讨论】:
+ 1 。我同意。.Find
更快:)【参考方案2】:
作为一个好习惯,请不惜一切代价避免使用标签!
我会回答你只是修改你的代码,我猜你想保存的值 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")
【讨论】:
以上是关于excel 宏:ByRef 参数类型不匹配的主要内容,如果未能解决你的问题,请参考以下文章
从被调用的函数调用函数时 VBA byref 参数类型不匹配
optional [byval byref] [paramarray] 变量名() as 数据类型