|
本帖最后由 xiangxilanren 于 2014-6-26 23:31 编辑
各位老师:工作中用到道路里程数据,例如:1234.56789千米、0.12345千米、123.04567(数据精确到小数点后五位,也就是厘米)。现在需要将数据按:1234Km567m、0Km123m、123Km045m的格式(数据无需四舍五入)输出为文本文件使用。我先用InStr函数确认小数点位置,再用Mid函数提取小数点前后的数据。实际使用中发现,不能正确提取小数点后三位的数据,特别是数据中含有“0”时,错得无法忍受。不知道错在哪里,请各位老师帮忙指教。万分感谢!
With ActiveSheet
iZ = .Range("B65536").End(xlUp).Row
wjm = .Name
Set MyFile = CreateObject("scripting.filesystemobject").createtextfile(ThisWorkbook.Path & "\" & wjm & ".txt", True)
For r = 8 To iZ
c = ""
ksKM = ""
ksM = ""
c = InStr(.Cells(r, 4), ".")
ksKM = Mid(.Cells(r, 4), 1, c - 1)
ksM = Mid(.Cells(r, 4), c + 1, c + 3)
MyStr = ""
MyStr = MyStr & ksKM & "Km" & ksM & "m" ;"
MyFile.writeline (MyStr)
Next r
MyFile.Close
End With
请问各位老师,有没有更好的方法实现这个格式呢?可不可以用Format函数呢?
- With ActiveSheet
- iZ = .Range("B65536").End(xlUp).Row
- wjm = .Name
- Set MyFile = CreateObject("scripting.filesystemobject").createtextfile(ThisWorkbook.Path & "" & wjm & ".txt", True)
- For r = 8 To iZ
- ksKM = ""
- ksm = 0
- ksm = Int(Replace(.Cells(r, 4), "千米", "") * 1000)
- MyStr = ""
- MyStr = Format(ksm, "0km000m")
- MyFile.writeline (MyStr)
- Next r
- MyFile.Close
- End With
复制代码试试。。。
|
|