如何使用合并的单元格识别表数据?

时间:2018-11-15 02:22:30

标签: excel vba

我正在尝试在表格中插值。但是,由于我的列单元格是合并在一起的,所以我的代码不会读取这些值。到目前为止,每当我取消合并那些列时,它就可以正常工作并为我提供所需的值。如何将合并的单元格整合为矩阵?

Sub brent()

Dim i As Integer, j As Integer
Dim P As Single, P1 As Single, P2 As Single
Dim M As Single, M1 As Single, M2 As Single
Dim inputmat()

nrow = 29
ncol = 2

P = Range("Axial").Value

ReDim inputmat(nrow, ncol)

For i = 1 To nrow
    For j = 1 To ncol
    inputmat(i, j) = Cells(5 + i, 6 + j)
    Next j
Next i
If (P > inputmat(1, 1)) Or (P < inputmat(nrow, 1)) Then Range("PM").Value = 
"NG"
Else
    For i = 1 To nrow - 1
       If (P <= inputmat(i, 1)) And (P >= inputmat(i + 1, 1)) Then
          P1 = inputmat(i, 1)
          P2 = inputmat(i + 1, 1)
          M1 = inputmat(i, 2)
          M2 = inputmat(i + 1, 2)
      End If
Next i

For i = 1 To nrow

M = M1 + (P - P1) * (M2 - M1) / (P2 - P1)

Next i

Range("PM").Value = M
End If

End Sub

我知道“输入(i,j)=单元格(5 + i,6 + j)”下存在问题 有什么方法可以读取合并的单元格之间的黑色列?

1 个答案:

答案 0 :(得分:0)

enter image description here

Sub FindMergedCells()

    Dim tbl As Range, cll As Range
    Dim i As Integer

    i = 1
    Set tbl = Range("A1:E4")

    For Each cll In tbl
        If cll.MergeCells Then
            Cells(i, 7) = "Cell " & cll.Address & " is merged"

            i = i + 1
        End If
    Next

End Sub