Page 1 of 1

ปรับโค้ดอย่างไรครับ ถึงจะตรวจสอบเฉพาะอักขระ 8 ตัวแรกเท่านั้นไม่ให้ซ้ำ ถ้าไม่ซ้ำถึงให้ทำตามเงื่อนไข

Posted: Mon Jun 20, 2022 12:34 pm
by 10idlnw
2022-06-20_10-05-52.jpg
ผมต้องการวิธีไม่ให้ชื่อไฟล์ที่เป็นตัวเลข(เลขอ้างอิง)ซ้ำกันเด็ดขาด จากภาพจะเห็นว่า 65060003 ซ้ำกันถึง 3 ไฟล์ เพราะพอเปลี่ยนชื่อบริษัทประกัน หรือป้ายทะเบียน มันก็ทำการสร้างได้อยู่ดี จะทำยังไงครับ ให้เฉพาะตัวเลข 8 ตัวแรกเท่านั้นที่ห้ามซ้ำกันเด็ดขาด โค้ดที่ผมใช้อยู่ด้านล่าง ต้องปรับโค้ดอย่างไรครับ

Code: Select all

Private Sub CommandButton1_Click()

Dim str1 As String
Dim nameSvFile As String
Dim mypath As String
Dim strFileExists As String
Dim filenames As String
 
    Call PadRunNumber
    
    mypath = ThisWorkbook.Path & "\"
    str1 = mypath & "00000000-insname-carid.xlsm"
    nameSvFile = mypath & [ShowFName4Save] & ".xlsm"
    strFileExists = Dir(mypath & [ShowFName4Save] & ".xlsm")

    If strFileExists <> "" Then
            
            MsgBox Split(strFileExists, "-")(0) & " ไฟล์นี้น่าจะมีอยู่แล้วกรุณาตรวจสอบใหม่อีกครั้ง"
            
    Else
    
            Range("RunNumber") = Range("RunNumber") + 1
            Call PadRunNumber
                FileCopy str1, nameSvFile
                MsgBox "สร้างไฟล์สำเร็จ " & [RunCaseNumber] - 1
            Workbooks.Open nameSvFile
            
    End If
    
End Sub

Re: ปรับโค้ดอย่างไรครับ ถึงจะตรวจสอบเฉพาะอักขระ 8 ตัวแรกเท่านั้นไม่ให้ซ้ำ ถ้าไม่ซ้ำถึงให้ทำตามเงื่อนไข

Posted: Mon Jun 20, 2022 12:51 pm
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

'Other code
strFileExists = mypath & Left([ShowFName4Save], InStr([ShowFName4Save], "-")) & "*"

If Dir(strFileExists) <> "" Then
    MsgBox Split(strFileExists, "-")(0) & " ไฟล์นี้น่าจะมีอยู่แล้วกรุณาตรวจสอบใหม่อีกครั้ง"
Else
'Other code

Re: ปรับโค้ดอย่างไรครับ ถึงจะตรวจสอบเฉพาะอักขระ 8 ตัวแรกเท่านั้นไม่ให้ซ้ำ ถ้าไม่ซ้ำถึงให้ทำตามเงื่อนไข

Posted: Mon Jun 20, 2022 1:10 pm
by 10idlnw
ใช้งานได้ตรงตามต้องการเลยครับ ขอบคุณอาจารย์มากๆครับ