VBA:双变量中缺少前导零

问题描述 投票:0回答:1

我已经编写了VBA脚本,以特定结构将表中的数据导出到CSV文件,这是输入其他软件所必需的。该脚本有效,除了如果<1(例如,十进制值)缺少其前导零,则1.234写入预期,但是0.123写入.123。

我不想像我尝试过的每种方法一样将其写为字符串,例如Format(Day(myDate), "00"),在CSV文件中的值周围创建引号,该文件与此文件所需的软件不兼容。是否有一种方法可以强制变量保留前导零,或将其写为不包含引号的字符串?

下面的代码摘录,其中cellValue1是保存该值的变量。]​​>

Sub test_export()

Dim txtFile As String, rng1 As Range, rng2 As Range, cellValue1 As Double, i As Integer
Dim myDate As Date, yyyy As Integer, mm As Integer, dd As Integer, hh As Integer, min As Integer, ss As Integer

txtFile = Application.DefaultFilePath & "\test.csv"

Set rng1 = Worksheets("OUTPUT").Range("B8:B103")
Set rng2 = Worksheets("OUTPUT").Range("G8:G103")

Open txtFile For Output As #1

For i = 1 To rng1.Rows.Count

myDate = rng1.Cells(i, 1).Value

yyyy = year(myDate)
mm = month(myDate)
dd = day(myDate)
hh = hour(myDate)
min = minute(myDate)
ss = Second(myDate)

cellValue1 = rng2.Cells(i, 1).Value

Write #1, yyyy, mm, dd, hh, min, ss, cellValue1

Next i

Close #1

End Sub

我已经编写了VBA脚本,以特定结构将表中的数据导出到CSV文件,这是输入其他软件所必需的。该脚本有效,除了十进制值缺少前导零...

excel vba double decimal
1个回答
0
投票

尝试

© www.soinside.com 2019 - 2024. All rights reserved.