excelmacros:ByRef参数types不匹配
我写一个代码如下:
Call search(xx, yy, "APM Output", ">> State Scalars", label1) label1: ...........
这是Subsearch的脚本
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
我想先调用子search(..),然后去标签1。 问题是说label_num的“ByRef参数types不匹配”。在Sub search(..,..,.., label_num )中应该是label_num的正确types吗?
我添加一些原始脚本,这些是我想要转换成一个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
作为一个良好的做法,请避免使用标签不惜一切代价!
我要回答你只是修改你的代码,我想你想保存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
把你的function变成一个子,简单的做
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")
你的潜力对我来说没有多大意义
- 如果它find
">> State Scalars"
它存在For
循环,并去label1
- 如果它没有find
">> State Scalars"
无论如何到达label1
你可以使用下面的代码
- 一次search100个100单元格
- 要么find你的部分string匹配或不
- 它可以很容易地修改,以停止search成功(或失败)后search
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