• 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.

Question About Working Macro

I have this macro that works but it asks for me to choose the file fName1 three or four times. Why?

Code:
Sub GetFile()

Dim fName1 As Variant, wbo As Workbook
Dim fName2 As Variant, wba As Workbook
Dim WBName As Worksheet


fName1 = Application.GetOpenFilename(filefilter:="Excel Files (*.XLS), *.XLS", Title:="Select the prior spreadsheet:")

  If fName1 = False Then Exit Sub

  Set wbo = Workbooks.Open(fName1)
  wbo.Activate
 
  
  
fName2 = Application.GetOpenFilename(filefilter:="Excel Files (*.XLS), *.XLS", Title:="Select the current spreadsheet:")

  If fName2 = False Then Exit Sub

  Set wba = Workbooks.Open(fName2)
  
  wba.Activate
  
  Range("M2").Value = "1"
  Range("M2").Select
  Selection.AutoFill Destination:=Range("m2:m" & Cells(Rows.Count, "A").End(xlUp).Row)
  
  Range("N2").FormulaR1C1 = "=RC[-13]&RC[-10]"
  Range("N2").Select
  Selection.AutoFill Destination:=Range("N2:N" & Cells(Rows.Count, "A").End(xlUp).Row)
  
 
  Range("O2").FormulaR1C1 = "=VLOOKUP(RC[-1],' & fName1 & '!R2C14:R7C14,1,0)"
  Range("O2").Select
  Selection.AutoFill Destination:=Range("O2:O" & Cells(Rows.Count, "A").End(xlUp).Row)
  
 wbo.Close savechanges:=True
 wba.Close savechanges:=True

End Sub
 
This code alone only asks to enter sheet once. Each for previous and current.

Without seeing what other process/code are present in your workbook. It would be hard to tell where the issue is.

Upload sample demonstrating where your issue occurs.
 
OK, here are the two files. SalesLimit1 will be the previous file or fName1 and SalesLimit2 will be the current file or fName2.

When the macro runs it asks: Update Values: & fName1 &

What the macro does is opens fName1 (SalesLimit1) and puts a VLOOKUP in column O of fName2 (SalesLimit2). The VLOOKUP looks funny but it w
 

Attachments

  • SalesLimit1-01262017-AM.xls
    25 KB · Views: 1
  • SalesLimit2-01272017-PM.xls
    25 KB · Views: 1
Oh I see. It's because of this line.
Code:
 Range("O2").FormulaR1C1 = "=VLOOKUP(RC[-1],' & fName1 & '!R2C14:R7C14,1,0)"

Try something like...
Code:
Range("O2").FormulaR1C1 = "=VLOOKUP(RC[-1],'" & wbo.Name & "'!R2C14:R7C14,1,0)"
 
You need to choose fName1 at least once. Since code uses GetOpenFilename to open the workbook.

If the file is fixed, you can just use hardcoded workbook name in it's place.
 
So the code, requires you to pick a file through dialog for it to open and assign it to a variable.

Code:
fName1 = Application.GetOpenFilename(filefilter:="Excel Files (*.XLS), *.XLS", Title:="Select the prior spreadsheet:")

If fName1 is going to be consistently same file every time you run the code. Instead of using GetOpenFilename... Dim fName1 as string and hard code it. Replacing "C:\YourPath" with actual location of your file. Otherwise, use code as is, with modification indicated in post#4.
Code:
Dim fName1 as String

fName1 = "C:\YourPath\SalesLimit1-01262017-AM.xls"
 
That's the issue, the file name and the sheet name will always be different names. That is why I chose GetOpenFileName.

Any suggestions?
 
Not sure I get you.

If you apply the modification in post #4. You just need to select each file once.
Code:
Sub GetFile()

Dim fName1 As Variant, wbo As Workbook
Dim fName2 As Variant, wba As Workbook
Dim WBName As Worksheet


fName1 = Application.GetOpenFilename(filefilter:="Excel Files (*.XLS), *.XLS", Title:="Select the prior spreadsheet:")

  If fName1 = False Then Exit Sub

  Set wbo = Workbooks.Open(fName1)
  wbo.Activate
 
 
fName2 = Application.GetOpenFilename(filefilter:="Excel Files (*.XLS), *.XLS", Title:="Select the current spreadsheet:")

  If fName2 = False Then Exit Sub

  Set wba = Workbooks.Open(fName2)
 
  wba.Activate
 
  Range("M2").Value = "1"
  Range("M2").Select
  Selection.AutoFill Destination:=Range("m2:m" & Cells(Rows.Count, "A").End(xlUp).Row)
 
  Range("N2").FormulaR1C1 = "=RC[-13]&RC[-10]"
  Range("N2").Select
  Selection.AutoFill Destination:=Range("N2:N" & Cells(Rows.Count, "A").End(xlUp).Row)
 
  Range("O2").FormulaR1C1 = "=VLOOKUP(RC[-1],'" & wbo.Name & "'!R2C14:R7C14,1,0)"
  Range("O2").Select
  Selection.AutoFill Destination:=Range("O2:O" & Cells(Rows.Count, "A").End(xlUp).Row)
 
wbo.Close savechanges:=True
wba.Close savechanges:=True

End Sub

What's the issue?
 
The issue is, that once I have selected the files, I get a dialog box asking to Update Values from fName1.

upload_2017-3-23_10-46-52.png

How do I avoid that?
 
I get it right after I select the second file. I tried adding Application.DisplayAlerts but that messes up the formula.

I'm running Excel 2010.

It has me puzzled especially since you do not get that message.
 
OK, there must have been something corrupt in the original macro. I copied the code from your post and put it in a new workbook and it works fine.
 
Back
Top