クリスしかし、あなたのコードは、私が思いついたこの解決策の出発点でした。結果は下の画像にこのソリューションの固有の欠点とともに示されています。このようにフォーマットされた後の列の数値で数式を実行すると、数値形式が奇妙になります。
私はそれを思い付くことができる唯一の解決策は、このソリューションは問題はありません。インデントを推定し、それを適用することに依存します。その解決策は、列幅が調整されていない限り、機能します。調整されている場合は、マクロを再実行する必要があります。さらに、インデントを1だけインクリメントするだけでインデントを増やすことができるため、インデントを適用したマクロは、通常、列の中の最大の数が正確に中央にないことになります。大した問題ではありませんが、現在のソリューションにはこれらの問題はなく、私の使用例では、スプレッドシートをフォーマットするプロセスの最後のステップとしてこれらのフォーマットが適用されているため、マクロは必要に応じて簡単に再実行できます。
'Select your data range, and run formatCells_Accounting(). The number formatting in the selected cells will widen to the cell with the longest value. Note, the macro does not work on values greater than 10^14 (not sure why.)
Sub formatCells_Accounting()
Dim rg, thisColRange, rCell As Range
Dim maxVal, minVal, valueLen, longest_, lenLongest As Long
Set rg = Selection
'Center aligns all selected cells
rg.HorizontalAlignment = xlCenter
'Loops through each column in the selected range so that each column can have it's own max value
For Each thisColRange In rg.Columns
maxVal = Application.WorksheetFunction.Max(thisColRange)
minVal = Application.WorksheetFunction.Min(thisColRange)
'The longest number in the range may be the most negative
'This if section accounts for this scenario
If Abs(minVal) > maxVal Then
longest_ = minVal
Else
longest_ = maxVal
End If
'Gets the length of the longest value rounded to the ones place (aka length not including decimals)
lenLongest = Len(CStr(Round(Abs(longest_), 0)))
'Creates a number format for every cell
For Each rCell In thisColRange.Cells
'Gets the length of the value in the current cell
valueLen = Len(CStr(Round(Abs(rCell.Value), 0)))
rCell.NumberFormat = "_(" & addCommasDollarsToFormat(lenLongest, valueLen, rCell.Value) & "_);" & _
"_(" & addCommasDollarsToFormat(lenLongest, valueLen, rCell.Value) & ")_);" & _
"_(" & Left(addCommasDollarsToFormat(lenLongest, 1, rCell.Value), Len(addCommasDollarsToFormat(lenLongest, 1, rCell.Value)) - 1) & "0_);" & _
"_(@_)"
Next
Next
End Sub
Function addCommasDollarsToFormat(ByVal lenLongest, ByVal valueLen, ByVal cellVal) As String
Dim new_str_ As String
Dim i, j As Long
'Initializes empty strings
new_str_ = ""
nearlyFinishedString = ""
'Adds ? and , through the length of the value currently being formatted
For i = 1 To valueLen
If i Mod 3 = 1 And i <> 1 Then
new_str_ = new_str_ & ",?"
Else
new_str_ = new_str_ & "?"
End If
Next
If cellVal < 0 Then
new_str_ = new_str_ & "$("
Else
new_str_ = new_str_ & "$"
End If
For j = i To lenLongest
If j Mod 3 = 1 Then
new_str_ = new_str_ & ",?"
Else
new_str_ = new_str_ & "?"
End If
Next
addCommasDollarsToFormat = StrReverse(new_str_)
End Function
ソリューションは、以下に示すソリューションの欠点を視覚化しています。

入れて十分に ''あなたの最大の数のアカウントに:? '$ ???、?? 0 ' –
こんにちはスコット - それで問題が$である数に隣接していませんすべての場合、私はそのルートに行く(2番目の例) – user1583016