从其他模块传递variables并将其置于格式中

我有一个问题,因为我不熟悉如何编码的格式,因为我传递一个variables是从其他模块的数字。 我已经发布了我的代码sub record (这是我的Excel VBA模块9)。 这个子模块是我需要将某个字段修改为某种格式的地方,并且我需要从其他模块传递值。 我传递一个variables从模块2,这将返回一个数字,因为我已经声明为Public myVal As String因为我需要此Myval值,并将其插入到此字段3格式。

字段3的值是000000000209644 (15位数字)我需要修改成这样的东西020161201209644

  1. 0201612 = 0 + YYYYMM

  2. 01 =来自其他模块2的myval值

  3. 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| 

下面的代码在您的文章中像你想要的合并。

注意 :考虑到右边的前6个数字(15个数字之外) – 这是下面代码中的一个常量格式(可以修改为适合其他场景)。

 Sub record(myVal As String) Dim myfile As String, mysfile As String, myxfile As String myfile = ThisWorkbook.Path & "\PaymentFile02.xlsx" Open DatFile1Name For Output As #1 Application.Workbooks.Open (myfile) vRow = 2 While Cells(vRow, 1).Value <> "" field1 = Cells(vRow, 1).Value field2 = Cells(vRow, 2).Value ' this assumes your value in Column C will allways take 6 digits from the right Field3 = 0 & Format(Date, "YYYYMM") & myVal & Format(Cells(vRow, 3).Value, "000000") ' This is the variable where i need to do formatting Dim Str As String Str = "" Str = field1 & "|" & field2 & "|" & Field3 & "|" Debug.Print Str 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 

使用格式build立你的str。

格式函数(Visual Basic for Applications) https://msdn.microsoft.com/en-us/library/office/gg251755.aspx

Str =“0”&格式(field1,“YYYYMM”)等

不知道,如果你的3场3应该是场1,场2和场3 …