简体   繁体   中英

Excel VBA Get values of merged cells

I've found simillary question but it hasn't helped me. I have a column with merged and non-merged cells. I'd like to get their values. I don't know how to use it:

Set ma = myWorkbook.Range("C2").MergeArea
sValue = ma.Cells(c.Row, 1).Value

Look at my code:

Private Function GetValueOfMergedCells(sName, iColumn)
    Set myWorkbook = Workbooks().Worksheets()

    Set c = myWorkbook.Cells.Find(What:=sName, After:=ActiveCell, LookAt _
                        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

    If Not c Is Nothing Then
        If (iColumn <> 2) Then
            '...
        Else
            Set ma = NEWDevicesWS.Range("C2").MergeArea
            MsgBox ma.Cells(c.Row, 1).Value
            GetValueOfMergedCells = ma.Cells(c.Row, 1).Value
        End If
    Else
        '...
    End If
End Function


Sub main()
    Set firstWorkbook = Workbooks().Worksheets()

    ' Get array of strings
    Dim aNames() As String
        aNames = ImportSamplesNames() 

    For i = 1 To UBound(aNames())
        firstWorkbook.Cells(3 + i, 2) = GetValueOfMergedCells(aNames(i), 2)
    Next i
End Sub

What do I do wrong?

can't track down your code

so hope that:

ma.Resize(1, 1).Value

would do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM