PDA

View Full Version : [SOLVED] Vba Custom Format "0000" in File Name



icemail
12-25-2019, 07:03 AM
Hello i try to open picture in userform2.imagebox .All picture names like "0001" format in folder. Expectation is "0001" output is "1"




randno = 0
randno = Int((1000 - 2 + 2) * Rnd + 2)


If Len(randno) = 1 Then
fname = CStr("\" & "000" & randno & "-" & Lg & ".jpg")
ElseIf Len(randno) = 2 Then
fname = CStr("\" & "00" & randno & "-" & Lg & ".jpg")
ElseIf Len(randno) = 3 Then
fname = CStr("\" & "0" & randno & "-" & Lg & ".jpg")
ElseIf Len(randno) = 4 Then
fname = CStr("\" & "" & randno & "-" & Lg & ".jpg")
End If

With Img
Path1 = "C:\Downloads\Lg\"
myfile = Path1 & Lg & fname
.Picture = LoadPicture(myfile)
End With

Paul_Hossler
12-25-2019, 09:24 AM
Not exactly sure about what you want to do, but this should return "0001" to "9999"




Option Explicit


Sub test()
Dim i As Long
Dim s As String

i = 1 + Int(9999# * Rnd)

s = Format(i, "0000")

MsgBox s


End Sub

icemail
12-27-2019, 02:57 AM
s = Format(i, "0000")

Thanks Paul format is working.