TWO AMENDMENTS NEEDED IN MACRO - PLEASE HELP

K

K

Hi all, I have macro set on a button (see below)

***************************************************>
Sub OpenFile2()
Folder = "C:\Document\KK"
VRNumber = Range("A1").Value
fName = Dir(Folder & "\*" & VRNumber & "*.xlsx")
If fName = "" Or Range("J51").Value = "" Or Len(VRNumber) <> 6 Then
MsgBox "VIREMENT *" & VRNumber & "* DONT EXIST"
Else
Workbooks.Open Filename:=Folder & "\" & fName
End If
End Sub
<***************************************************

In Folder "C:\Document\KK" files names are like (see below)
Excel 553656.xlsm
Excel 556987 - Page 1.xlsm
Excel 556987 - Page 2.xlsm …… etc

I want two amendment in macro above. At the moment if I put 6 digits
number in cell "A1" which are in file name (mentioned above) and hit
the button then macro opens that file for me. The first amendment I
want is to specify that only 6 digits numbers should be put in cell
"A1" by adding code line in above macro "Mid(fName,7,6)" so if some
one put 6 digits like "Excel " macro should not open the file and it
should only open if the middle bit of file name which are 6 digit
numbers will be put in cell "A1". The second amendment i want in
above macro as the last two files mentioned above got same 6 digits
number in their names, the only difference is the last bit which is
"Page 1" and "Page 2" so if i put 6 digits number which is "556987" in
cell "A1" and when I hit the button then macro should open all the
file which got that 6 digits number in their names. I tried many
experiments but wasnt very successful. Can any friend amend my macro
above and put these two amendments. it will be much appricated.
Thanks in advance
 
J

Joel

Sub OpenFile2()
Folder = "C:\Document\KK"
VRNumber = Range("A1").Value
If IsNumeric(VRNumber) And (Range("J51").Value <> "") And _
Len(VRNumber) = 6 Then

fName = Dir(Folder & "\*" & VRNumber & "*.xlsx")
If fName = "" Then
MsgBox "VIREMENT *" & VRNumber & "* DONT EXIST"
Else
Do While fName <> ""
Workbooks.Open Filename:=Folder & "\" & fName

fName = Dir()
Loop
End If
Else
MsgBox "VIREMENT *" & VRNumber & "* DONT EXIST"
End If
End Sub
 
K

K

Sub OpenFile2()
Folder = "C:\Document\KK"
VRNumber = Range("A1").Value
If IsNumeric(VRNumber) And (Range("J51").Value <> "") And _
   Len(VRNumber) = 6 Then

   fName = Dir(Folder & "\*" & VRNumber & "*.xlsx")
   If fName = "" Then
      MsgBox "VIREMENT  *" & VRNumber & "*  DONT EXIST"
   Else
      Do While fName <> ""
         Workbooks.Open Filename:=Folder & "\" & fName

         fName = Dir()
      Loop
   End If
Else
   MsgBox "VIREMENT  *" & VRNumber & "*  DONT EXIST"
End If
End Sub








- Show quoted text -

Thanks lot Joel. That’s what I was looking for
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top