Solved

VBA Code Needs Revision

Posted on 2013-06-07
4
251 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
Comment Utility
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
Comment Utility
@ 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
Comment Utility
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
Comment Utility
Not a problem! :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now