2017-04-04 55 views
2

我的數據有10列以上,我想選擇三列,並且進一步格式化這三列,但是沒有。行不固定,所以我無法一次選擇所有這三列。 這裏是我想要做的格式化可變但行數相等的消毒列,並且列不相鄰

Dim lastrow As Long 
lastrow = Range("A" & Rows.Count).End(xlUp).Row 
Range("G2:H" & lastrow, "J2:J" & lastrow).Select 

但是,這是我選擇的列也是如此。 我想這也

Range("G2:H & lastrow, J2:J" &lastrow).select 

但是,這給了我的錯誤預期。

當使用

Range("J2:J" & lastrow).Select 
With Selection 
    .NumberFormat = "0" 
    .Value = .Value 
End With 

得到的數據格式正確的,但我想這樣做的所有三列未adjacnet Screenshot 1

但是,如果使用

Intersect(Range("G:H, J:J"), Rows("2:" & lastrow)).Select 
With Selection 
    .NumberFormat = "0" 
    .Value = .Value 
End With 

列G和H格式正確,但列J不是,它給了我#NA條目。 Screenshot 2

回答

3

,你必須通過每個連續的範圍內,您可以通過Areas()財產的手段得到循環,如下:

Dim lastrow As Long 
lastrow = Range("A" & Rows.Count).End(xlUp).Row 
Dim area As Range 

With Intersect(Range("G:H, J:J"), Rows("2:" & lastrow)) 
    .NumberFormat = "0" 
    For Each area In .Areas 
     area.Value = area.Value 
    Next 
End With 
+0

感謝!它完美:) – Digvijay

+1

...你可能想將答案標記爲已接受:-) – user3598756

+0

是的,我已經做到了。 – Digvijay

相關問題