i've faced an issues with incremental functions. My current excel macro can't generate incremental number by +1. I will explain in more details in the following.
Firstly, i have a macro files(file named: Still working on it.xlsm) there are one button inside this macro files with assign macro vba the function of this macro will actually convert a file (file named: File Header.xls) into certain formats in text files(PaymentFile01.txt).
These are the following output in text files
00|MAABBJQQ|AJ_20161206_001||||||||||||||||||||||||||
I need to make it incremental like this whenever it convert to the text files (PaymentFile01.txt). This paymentfile01.txt that contains AJ_20161206_001 will be removed as this AJ_20161206_002 will be generated into the same path
00|MAABBJQQ|AJ_20161206_002||||||||||||||||||||||||||
i do have a sample on how to do this, it's based on Date( Sample function that i need.xlsm),but i can't integrate it into my code in Still working on it.xlsm because it's different situation as it seems to be more complicated.
Still working on it.xlsm codes
Sample function that i need.xlsm
i've uploaded 4 files
1. Still working on it.xlsm
2. File Header.xls (still working on it.xlsm will process this file)
3. PaymentFile01.txt (The output)
4. Sample function that i need.xlsm
Firstly, i have a macro files(file named: Still working on it.xlsm) there are one button inside this macro files with assign macro vba the function of this macro will actually convert a file (file named: File Header.xls) into certain formats in text files(PaymentFile01.txt).
These are the following output in text files
00|MAABBJQQ|AJ_20161206_001||||||||||||||||||||||||||
I need to make it incremental like this whenever it convert to the text files (PaymentFile01.txt). This paymentfile01.txt that contains AJ_20161206_001 will be removed as this AJ_20161206_002 will be generated into the same path
00|MAABBJQQ|AJ_20161206_002||||||||||||||||||||||||||
i do have a sample on how to do this, it's based on Date( Sample function that i need.xlsm),but i can't integrate it into my code in Still working on it.xlsm because it's different situation as it seems to be more complicated.
Still working on it.xlsm codes
Code:
Sub CreatePFHeaderFooter()
Dim x As Long, y As Long
Dim data(1 To 29) As String
Dim myfile As String
'file location
myfile = "C:\Users\lye.yan.nian\Desktop\File Header.xls"
Application.Workbooks.Open Filename:=myfile
DatFile1Name = ThisWorkbook.Path + "\PaymentFile01.txt"
Open DatFile1Name For Output As #1 'create csv file
x = 2
While Cells(x, 1).Value <> ""
If Cells(x, 3) = "" Then Cells(x, 3) = getNewID(Cells(x - 1, 3))
For y = 1 To 28
data(y) = Cells(x, y)
Next
Print #1, Join(data, "|")
x = x + 1
Wend
Close #1
MsgBox ("File PaymentFile01.TXT created")
ActiveWorkbook.Close
End Sub
Function getNewID(OldID As String) As String
Dim arr() As String, strDate As String
Dim d As Date
arr = Split(OldID, "_")
strDate = arr(1)
d = DateSerial(Left(strDate, 4), Mid(strDate, 5, 2), Right(strDate, 2))
If d = Date Then
arr(2) = Format(CInt(arr(3)) + 1, "000")
Else
arr(1) = Format(Date, "yyyymmdd")
arr(2) = "001"
End If
getNewID = Join(arr, "_")
End Function
Sample function that i need.xlsm
Code:
Private Sub Workbook_Open()
If [B1] = "" Then
[B1] = Format(Now(), "dd/mm/yyyy")
[B2] = 1
Else
If Trim([B1]) <> Format(Now(), "dd/mm/yyyy") Then
[B1] = Format(Now(), "dd/mm/yyyy")
[B2] = 1
Else
[B1] = Format(Now(), "dd/mm/yyyy")
[B2] = [B2] + 1
End If
End If
End Sub
i've uploaded 4 files
1. Still working on it.xlsm
2. File Header.xls (still working on it.xlsm will process this file)
3. PaymentFile01.txt (The output)
4. Sample function that i need.xlsm
Attachments
Last edited: