导出的CSV格式问题

我有一个macros人为我创build一个Excel范围表导出到CSV文件的macros。 当我运行macros时,csv的格式不是我正在寻找的。 例如,电子表格中的最后一列是一个货币值(100.25),但是当我转换它时,我得到一个像100.2542的值。 有没有在macros中的任何地方我可以插入一个命令,让我纠正或更改格式正确的格式? 提前感谢您的帮助。

Sub MacroMan() ChDrive "P:" '// <~~ change current drive to P:\ Dim copyRng As Excel.Range Dim ThisWB As Excel.Workbook Dim OtherWB As Excel.Workbook Dim sName As String '// set reference to the 'Master' workbook Set ThisWB = ActiveWorkbook '// assign selected range to 'copyRng' Set copyRng = Application.InputBox(Prompt:="Select range to convert to CSV", Type:=8) '// If the user selected a range, then proceed with rest of code: If Not copyRng Is Nothing Then '// Create a new workbook with 1 sheet. Set OtherWB = Workbooks.Add(1) '// Get A1, then expand this 'selection' to the same size as copyRng. '// Then assign the value of copyRng to this area (similar to copy/paste) OtherWB.Sheets(1).Range("A1").Resize(copyRng.Rows.Count, copyRng.Columns.Count).Value = copyRng.Value '// Get save name for CSV file. sName = Application.GetSaveAsFilename(FileFilter:="CSV files (*.csv), *.csv") '// If the user entered a save name then proceed: If Not LCase(sName) = "false" Then '// Turn off alerts Application.DisplayAlerts = False '// Save the 'copy' workbook as a CSV file OtherWB.SaveAs sName, xlCSV '// Close the 'copy' workbook OtherWB.Close '// Turn alerts back on Application.DisplayAlerts = True End If '// Make the 'Master' workbook the active workbook again ThisWB.Activate MsgBox "Conversion complete", vbInformation 

万一

结束小组

我个人不推荐这个动作。 货币通常存储在小数点后两位,并计算到小数点后四位。 这是为了确保税务计算的准确性,用混合数字表示的数量的乘法和复杂公式中的长列数字的总和。 将值截断到小数点后两位将不可避免地让你“丢一分钱”。

这就是说,这就是你想要做的,这是你如何做到的。

 '// If the user entered a save name then proceed: If Not LCase(sName) = "false" Then '// Turn off alerts Application.DisplayAlerts = False ' set Precision As Displayed' - permanently truncates off numbers to the displayed decimal places OtherWB.PrecisionAsDisplayed = True '// Save the 'copy' workbook as a CSV file OtherWB.SaveAs sName, xlCSV ' turn off Precision As Displayed' - numbers have been permanently changed; you don't get back the lost decimal places OtherWB.PrecisionAsDisplayed = False '// Close the 'copy' workbook OtherWB.Close '// Turn alerts back on Application.DisplayAlerts = True End If