標題:
transfer string to number(小問題請教)
[打印本頁]
作者:
jj369963
時間:
2013-10-20 19:27
標題:
transfer string to number(小問題請教)
本帖最後由 jj369963 於 2013-10-20 19:29 編輯
Dear 各位大大與前輩們:
如附檔,想將D欄到CO欄位轉成數字格式,目前用以下代碼試過成功,but 是要一個一個寫到CO欄嗎?
請問是否有其他方式?發問是進步的原動力,再煩請各位先輩指教,感謝
Sub transfer()
Dim RngHead As Range, DataCunt As Long, i As Long
Set RngHead = Range("A2")
DataCunt = Range("A65536").End(xlUp).Row
Application.ScreenUpdating = False
For i = RngHead.Row To DataCunt
If Val(Range("A" & i).Value) <> 0 Then
Range("A" & i).NumberFormatLocal = "G/通用格式"
Range("A" & i).Value = Val(Range("A" & i).Value)
End If
If Val(Range("F" & i).Value) <> 0 Then
Range("F" & i).NumberFormatLocal = "G/通用格式"
Range("F" & i).Value = Val(Range("F" & i).Value)
End If
If Val(Range("G" & i).Value) <> 0 Then
Range("G" & i).NumberFormatLocal = "G/通用格式"
Range("G" & i).Value = Val(Range("G" & i).Value)
End If
If Val(Range("H" & i).Value) <> 0 Then
Range("H" & i).NumberFormatLocal = "G/通用格式"
Range("H" & i).Value = Val(Range("G" & i).Value)
End If
If Val(Range("I" & i).Value) <> 0 Then
Range("I" & i).NumberFormatLocal = "G/通用格式"
Range("I" & i).Value = Val(Range("G" & i).Value)
End If
If Val(Range("J" & i).Value) <> 0 Then
Range("J" & i).NumberFormatLocal = "G/通用格式"
Range("J" & i).Value = Val(Range("G" & i).Value)
End If
Next i
End Sub
複製代碼
[attach]16395[/attach]
作者:
luhpro
時間:
2013-10-20 20:51
Dear 各位大大與前輩們:
如附檔,想將D欄到CO欄位轉成數字格式,目前用以下代碼試過成功,but 是要一個 ...
jj369963 發表於 2013-10-20 19:27
關鍵是善用 Union 與 For Each 函數 :
Sub tf()
Dim rTar As Range, RngHead As Range
Dim DataCunt As Long
Dim vTar
Set RngHead = Range("A2")
DataCunt = Range("A65536").End(xlUp).Row
Set rTar = Union(Range(RngHead.Address & ":A" & DataCunt), Range("F" & RngHead.Row & ":J" & DataCunt))
For Each vTar In rTar
With vTar
If .Value <> 0 Then
.NumberFormatLocal = "G/通用格式"
.Value = Val(.Value)
End If
End With
Next vTar
End Sub
複製代碼
[attach]16397[/attach]
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)