2017-06-07 277 views
0

有n張已被選中,並且我嘗試將「百分比」樣式應用於所有單元格選擇範圍 但是隻有第一張紙張被格式化。我使用的樣式完全適用於所有工作表,只有「百分比」似乎只能在第一張工作表中使用!是否有特定原因?請粘貼我的代碼片段:提前致謝您的幫助在Excel-VBA中「百分比」樣式格式化

Range(Cells(143, 2), Cells(146, MarketRangeColumn + 10)).Select 
Selection.Style = "Percent" 
Range(Cells(100, 2), Cells(142, MarketRangeColumn + 10)).Select 
Selection.Style = "Comma" 
Selection.numberformat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""-""??_);_(@_)" 
Selection.numberformat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)" 
+0

你創建多個帳戶? https://stackoverflow.com/questions/44413047/formatting-macro-doesnt-work-on-all-worksheets – Absinthe

回答

0

如果您選擇了多個工作表,應用樣式只會影響活動工作表上的單元格。應用NumberFormat將影響所有工作表,因此您應該使用該工作表而不是樣式,或者在每個工作表中循環使用

0

所有紙張選擇的方法

Sub test() 
    Dim vName() 
    Dim Ws As Worksheet, n As Integer 
    For Each Ws In Worksheets 
     n = n + 1 
     ReDim Preserve vName(1 To n) 
     vName(n) = Ws.Name 
    Next Ws 
    Sheets(vName).Select 
    Range(Cells(143, 2), Cells(146, MarketRangeColumn + 10)).Select 
    Selection.Style = "Percent" 
    Range(Cells(100, 2), Cells(142, MarketRangeColumn + 10)).Select 
    Selection.Style = "Comma" 
    Selection.NumberFormat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""-""??_);_(@_)" 
    Selection.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)" 
End Sub