1

vb で作成した関数に問題があります。ツールは Excel シートを開き、そのシートで 2 つの値を検索します。

Excel シートは次のように構成されています。

テーブルの例の画像

私が書いた関数は、同じ行の列「M」と列「N」の値がクレテリウム1および2と同じかどうかを調べます。そうであれば、列「O」の値を返します"

私のコードは次のようになります。

    Function twoStrSearch(ByVal criteria1 As String, ByVal criteria2 As String, ByVal strPrimarySearchColumn As String, _
                      ByVal Offset_Krit2 As Integer, ByVal Offset_result As Integer, _
                      ByVal objWorksheet As Microsoft.Office.Interop.Excel.Worksheet) As VariantType
    '********************************************************************************************
    'Function for Searching an Excel Sheet.
    'If the Sheet Contains the two Criterias in the same row it will return the search Value
    '********************************************************************************************
    'Parameter:                 Explanation:         
    'criteria1                  The first comparison value
    'criteria2                  The second comparison value
    'strPrimarySearchColumn     The Name of the Row where the first comparsion value is
    'Offset_Krit2               The Offset Value where the second comparison value is
    'Offset_Ergebnis            The Offset Value where the Search result is what will be returned
    'objWorksheet               The object of the Excel Sheet that should be searched in
    '********************************************************************************************

    Dim strAddress As String
    Dim area As Microsoft.Office.Interop.Excel.Range
    Dim range As Microsoft.Office.Interop.Excel.Range
    'Get's the letter of the Column
    strAddress = objWorksheet.Cells.Find(What:=strPrimarySearchColumn).Address
    strAddress = Mid(strAddress, 2, 1)
    area = objWorksheet.Columns(strAddress & ":" & strAddress) 'Range over the Column
    For Each range In area
        'If both criteria in the same Row are True then get the result
        If range.Value2.ToString = criteria1 And range.Offset(0, Offset_Krit2).Value = criteria2 Then
            twoStrSearch = range.Offset(0, Offset_result).Value
            Exit Function
        End If
    Next
    twoStrSearch = "--" 'if nothing found result is "--"
End Function

セルの値を Criteria1 および 2 と比較すると、For Each ループでエラーが発生します。

私はしばらく立ち往生していましたが、おそらくあなたの何人かがアイデアを持っていると思いました!

4

2 に答える 2

2

ポイント対象外(説明のみ)

概要。

  1. 検索テキストがColにある場合、アドレスを抽出する方法でエラーが発生しますAA1
  2. 範囲を構築するためにアドレスは必要ありません。列番号を使用できます。
  3. すべてのセルをループするポイントはありません (xl2007+ の場合は 1048576) 検索列の最後の行を見つけて、関連する範囲を構築します

あなたのデータがこのように見えるとしましょう

ここに画像の説明を入力

コード:(VS 2010 Ultimate + Office 2010 Prof.で試行およびテスト済み)

これを試して。コードにコメントしたので、意味をなさないものがある場合はお知らせください。

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    '~~> Define your Excel Objects
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim objWorksheet As Excel.Worksheet

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        '~~> Open relevant file
        xlWorkBook = xlApp.Workbooks.Open("C:\MyFile.xlsx")

        '~~> Display Excel
        xlApp.Visible = True

        '~~> Set your first worksheet
        objWorksheet = xlWorkBook.Sheets(1)

        Dim Ret = twoStrSearch("1", "text-x", "TextNumber", -1, 1)

        MsgBox (Ret)

        '~~> Close the File
        xlWorkBook.Close (False)

        '~~> Quit the Excel Application
        xlApp.Quit()

        '~~> Clean Up
        releaseObject (objWorksheet)
        releaseObject (xlWorkBook)
        releaseObject (xlApp)
    End Sub

    Function twoStrSearch(ByVal criteria1 As String, ByVal criteria2 As String, ByVal strPrimarySearchColumn As String,
    ByVal Offset_Krit2 As Integer, ByVal Offset_result As Integer) As String
        Dim area As Excel.Range = Nothing
        Dim range As Excel.Range = Nothing
        Dim aCell As Excel.Range = Nothing
        Dim ColNo As Integer, lRow As Integer

        '~~> Find which column as the search text
        aCell = objWorksheet.Cells.Find(What:=strPrimarySearchColumn)

        '~~> Set it to "--" in case nothing is found
        twoStrSearch = "--"

        '~~> if found
        If aCell IsNot Nothing Then
            '~~> Get the column number
            ColNo = aCell.Column

            '~~> Get last row of that column
            lRow = objWorksheet.Cells(objWorksheet.Rows.Count, ColNo).End(Excel.XlDirection.xlUp).Row

            '~~> Construct your range from row 2 onwards. Row1 has headers
            area = objWorksheet.range(objWorksheet.Cells(2, ColNo), objWorksheet.Cells(lRow, ColNo))

            For Each range In area
                'If both criteria in the same Row are True then get the result
                If range.Value2.ToString = criteria1 And range.Offset(, Offset_Krit2).Value = criteria2 Then
                    twoStrSearch = range.Offset(, Offset_result).Value
                    Exit For
                End If
            Next
        End If

        releaseObject (area)
        releaseObject (range)
        releaseObject (aCell)

        Return twoStrSearch
    End Function

    '~~> Release the objects
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject (obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

出力:

ここに画像の説明を入力

于 2013-10-16T09:49:37.413 に答える