-2

重複の可能性:
Excelマクロ:ByRef引数タイプの不一致

私は次のようにコードを書きます:

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
....

それらを新しい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 

それを変更する方法は?私は本当にいくつかのsuggestion.thxが必要です

4

1 に答える 1

1

サブの繰り返し部分を別の関数に分割することは、優れたプログラミング手法です。

私は個人的に次のルートに入ります:

Sub Subroutine()
    set tCell = UDF_FindValue("APM Output",">> State Scalars")
    set tCell = UDF_FindValue("APM Output",">> GPU LPML")
    set tCell = UDF_FindValue("APM Output",">> Limits and Equations")
End Sub

Function UDF_FindValue(WS_Name as String, SearchString as String) as Range
    Set RefWS = WorkSheets(WS_Name)
    for each sCell in intersect(RefWS.Rows("1:100"),RefWS.Columns("1:100"))
        if sCell.value = SearchString then
            Set UDF_FindValue = sCell
            End function
        end if
    next sCell
End Function

関数を十分に一般化すると、後で再利用することもできます...

また、たとえば、find関数がすでに必要なことを実行していることがわかった場合は、コードを簡単に変更できます...

于 2012-09-20T18:04:11.757 に答える