向右排列值
Lining up values to the right
如果我有一些分散的值,我想排在右边
这可以用公式实现吗,或者VBA?
谢谢
对齐数据
之前
之后
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
如果我有一些分散的值,我想排在右边
这可以用公式实现吗,或者VBA?
谢谢
对齐数据
之前
之后
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