我有一个问题,因为我不熟悉如何编码格式,因为我传递的变量是其他模块中的数字。我发布了我的代码sub record
(这是我的excel vba中的模块9)。这个子模块是我需要将字段修改为某种格式的地方,并且我需要从其他模块传递值。我传递一个变量从模块2,将返回一个数字,因为我已经声明它为Public myVal As String
,因为我需要这个Myval
值,并以格式将其插入到此字段3中。从其他模块传递变量并将其置于格式
的字段3的值是000000000209644
(数字15位)我需要修改弄成这样020161201209644
0201612 = 0 + YYYYMM
01 =设为myVal从其它模块2值
209644 =是此文件中的当前编号数据。
下面是我的示例代码,以显示我在做什么
Sub record()
Dim myfile, mysfile, myxfile As String
myfile = ThisWorkbook.Path + "\PaymentFile02.xlsx"
Open DatFile1Name For Output As #1
Application.Workbooks.Open FileName:=myfile
vRow = 2
While Cells(vRow, 1).Value <> ""
Field1 = Cells(vRow, 1).Value
Field2 = Cells(vRow, 2).Value
Field3 = Cells(vRow, 3).Value ' this is the variable where i need to do formatting
Dim Str As String
str =""
str = field1 & "|" & field2 & "|" & Field3 & "|"
Print #1, str
vRow = vRow + 1 ' this is incremental as there are a lot of rows
Wend
Close #1
ActiveWorkbook.Close
MsgBox ("File PaymentFile02.TXT created")
End Sub
采样数据为场1场2场3:
00|HELLO|000000000209644|
看看下面为你的作品我的答案代码如预期 –