• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Replacing numbers in last two digits

yan nian

New Member
hi,
i have an enquiry to ask about how to replace the last two numbers when the inputbox prompt out, currently my code is whenever the inputbox prompt out, key in the numbers then it will do the + num( num is a variable which user can key in the numbers). But now i need to replace the last two numbers instead of doing addition function( in my code) for the following output
Examples for current code

AJ_20170203_01 -> prompt out then type 15 then ->AJ_20170203_16 ( my current code is doing addition)

Examples for the output i need

AJ_20170203_01 -> input box prompt out then type 15 then -> AJ_20170203_15

Code:
Sub test()
    Dim fn As String, txt As String, num As Integer, temp
    fn = ThisWorkbook.Path + "\PaymentFile01.txt"
    If fn = "False" Then Exit Sub
    txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "_(\d+)(?=\|)"
        num = InputBox("Give me some input")
        myVal = Format$(.Execute(txt)(0).submatches(0) + num, "_00") 
        txt = .Replace(txt, myVal)
        .Pattern = "(\r\n)+$"
        Open Replace(fn, ".txt", ".txt") For Output As #1
            Print #1, txt;
            MsgBox "This is bathch No" & myVal
        Close #1
    End With
End Sub
 
Hi,

I think changing "+ num" to "& num" in the below code should do the job.

Code:
        myVal = Format$(.Execute(txt)(0).submatches(0) + num, "_00")
 
yes it will do the job, but only for first time when i clicked my button, when the second time it will be like
1st time, AJ_20170203_01 -> input box prompt out then type 15 then -> AJ_20170203_15
2nd time i trigger my button, and i input in just 1, it will become AJ_20170203_151 instead of just AJ_20170203_1
 
Back
Top