首页 > 解决方案 > VBA将字母表中的列值转换为数字

问题描述

如何将 excel 地址“$C$2”的 alpha 部分转换为 3 和 2,以便可以在单元格对象中重新使用它。

如果它是“$E$4”,那么我需要两个单独的值,比如 5(对于字母 E)和 4,这样我就可以使用对象来引用它 - Cells(4,5)

基本上,我正在尝试使用下面的代码取消合并单元格,这就是需要获取 excel 单元格数字的地方。

Sub UnMerge()
    Dim i As Integer
    Dim fromRange() As String
    Dim toRange() As String
    Dim temp() As String

    ActiveSheet.UsedRange.MergeCells = False
    fromRange() = Split(ActiveCell.Address, "$")
    temp() = Split(Selection.Address, ":")
    toRange() = Split(temp(1), "$")


    For i = fromRange(2) To toRange(2)
        If Cells(i, Range(temp(0)).Column) = "" Then
            Cells(i, Range(temp(0)).Column) = Cells(i - 1, Range(temp(0)).Column).Value
        End If
    Next i
End Sub

标签: excelvba

解决方案


更改和拆分字符串以获取数字很慢。只需使用 selection.rows 和 selection.column:

Sub UnMerge()

    Selection.MergeCells = False
    With ActiveSheet
        Dim i As Long
        For i = Selection.Row To Selection.Rows.Count + Selection.Row - 1
            If .Cells(i, Selection.Column) = "" Then
                .Cells(i, Selection.Column) = .Cells(i - 1, Selection.Column).Value
            End If
        Next i
    End With
End Sub

推荐阅读