Solved

VBA Code Needs Revision

Posted on 2013-06-07
4
256 Views
Last Modified: 2013-06-07
Hello Experts,

I am currently using this code,...

Sub BuildFileNameAndSave()
    
    Dim SaveStr As Variant
    Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("Input") 'change as needed
    
    SaveStr = False
        
    Do Until SaveStr <> False
        SaveStr = Application.GetSaveAsFilename(CurDir & "\LOT # " & ws.[LotNumber] & " - CUST # " & ws.[CustomerNumber] & " - " & ws.[CustomerName] & ".xlsm", "Excel Workbooks (*.xlsm), *.xlsm", , _
            "Select file name and folder:")
        If SaveStr = False Then
            MsgBox "Please make a selection!", vbCritical
        End If
    Loop
    
    ThisWorkbook.SaveAs SaveStr
    
End Sub

Open in new window


But I ran into two issues, and I don't know my way out of it.

Issue 1 - "CustomerName", needs to be converted to UPPER CASE.  I know in Excel you just use something like this "=UPPER(A1)" - but with this code, I'm unsure of how to do it.

Issue 2 - When you run the code, and you are at the save dialog window - if the user then decides they don't want to save, they would click on "Cancel" - but doing so shows the error message "Please make a selection!", then it goes back to the save dialog window.

When I ran into this issue, I actually couldn't figure out how to stop it - so I had to force close excel.

Any ideas?

Thank you in advance for your help!

~ Geekamo
0
Comment
Question by:Geekamo
  • 2
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 39231026
Try this

Sub BuildFileNameAndSave()
    
    Dim SaveStr As Variant
    Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("Input") 'change as needed
    
    SaveStr = False
        
    Do Until SaveStr <> False
        SaveStr = Application.GetSaveAsFilename '(CurDir & "\LOT # " & ws.[LotNumber] & " - CUST # " & ws.[CustomerNumber] & " - " & UCase(ws.[CustomerName]) & ".xlsm", "Excel Workbooks (*.xlsm), *.xlsm", , _
            "Select file name and folder:")
        If SaveStr = False Then
            SaveStr = MsgBox("Please make a selection!", vbRetryCancel)
        End If
        If SaveStr = 4 Then SaveStr = False
    Loop
    If SaveStr <> 2 Then
        ThisWorkbook.SaveAs SaveStr
    End If
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Geekamo
ID: 39231034
@ ssaqibh

This is great!  The only thing I noticed is there was an " ' " in the line of code, which turned it into a comment.  I just removed that and it worked correctly.

Thank you very much!

~ Geekamo
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39231038
Yes, sorry I had to comment that out to be able to run the command on my computer which did not have your directory structure. In the end I forgot to remove it.

Thanks for the points.
0
 
LVL 1

Author Comment

by:Geekamo
ID: 39231042
Not a problem! :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question