excel - 向右排列值

标签 excel vba excel-formula

如果我有一些分散的值,我想向右排列enter image description here
这可以使用公式或 VBA 来实现吗?
谢谢

最佳答案

对齐数据
之前
enter image description here
之后
enter image description here

Option Explicit

Sub AlignData()
        
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    With ws.UsedRange
        
        Dim rCount As Long: rCount = .Rows.Count
        Dim cCount As Long: cCount = .Columns.Count
        If rCount + cCount = 2 Then Exit Sub ' one cell only
        
        Dim Data As Variant: Data = .Value
        
        Dim cValue As Variant
        Dim r As Long
        Dim sc As Long
        Dim dc As Long
        
        For r = 1 To rCount
            dc = cCount
            For sc = cCount To 1 Step -1
                cValue = Data(r, sc)
                If Len(CStr(cValue)) > 0 Then
                    Data(r, sc) = Empty
                    Data(r, dc) = cValue
                    dc = dc - 1
                End If
            Next sc
        Next r
        
        .Value = Data
        
    End With
    
    MsgBox "Data aligned.", vbInformation
        
End Sub

关于excel - 向右排列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72088706/

相关文章:

vba - 循环遍历工作表中的区域/区域?

excel - 有没有办法在Excel中获取截断数字的存储值

Excel VBA 启用/禁用图片上的功能

excel - 从文本框VBA中删除前导零

excel - 你如何使用 Left/right/substitute 函数来查找逗号之间的值

增加数字的Excel公式

vba - 使用 VBA 将图表系列放在辅助 X 轴上

excel - 使用自定义元动态定价规则导入 WooCommerce 产品

vba - 从 VBA 末尾删除回车符(alt+enter)

string - Excel:使用长度超过 255 个字符的公式