VB.net excel合并单元格范围

时间:2016-04-01 08:39:51

标签: vb.net excel

我有一个包含一些尺寸的Excel电子表格。我想在运行时搜索这个文件。

Excel sheet

此工作表包含一个表列1,定义搜索中必须发生的组。所以我将搜索A列,直到找到一个包含我喜欢的组的单元格。

此组是合并的单元格,其中包含属于该组的所有行。

我的问题:如何获取合并单元格消耗的行范围?需要此范围才能列出该组的所有可用评级。

1 个答案:

答案 0 :(得分:0)

感谢J. Chomel的评论。我可以自己回答这个问题。这Link向我展示了我需要的所有信息。

谢谢!

    Public Sub GetRatings()

    Dim oExcelLoc As String = "J:\EXCEL - Lijsten\NOZZLE SIZES.xlsx"

    ' Excel load data
    Dim oExcelApp As New Application
    Dim oWorkBook As Workbook
    Dim oWorkSheet As Worksheet

    oWorkBook = oExcelApp.Workbooks.Open(oExcelLoc, False, True)
    oWorkSheet = oWorkBook.Worksheets(1)


    For oRow As Integer = 1 To 100
        If oWorkSheet.Range("A" & oRow).Value = GroupString Then

            Dim oRange As Range
            oRange = oWorkSheet.Range("A" & oRow)

            ' Check if merge cells
            If oRange.MergeCells Then

                Debug.Print("Group start row = " & oRow & " |Group end row = " & oRow + oRange.MergeArea.Rows.Count - 1)

            Else
                MsgBox("Error")
            End If
            Exit For
        End If
    Next


    oWorkBook.Close()

    'Release object references.
    releaseObject(oWorkSheet)
    releaseObject(oWorkBook)
    releaseObject(oExcelApp)

End Sub

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