Question

I am trying to save the name of my file(s) partially based on cell values in one of my sheets. I have the following code:

Sheets("Input").Range("F18").Value & " - " & Sheets("Input").Range("M13").Value

The problem is I want the title to display as a percentage and not a value, or some cases as a dollar amount and not a value. Can someone explain how do this?

Thanks!

Was it helpful?

Solution

The following will save a file with a name such as "Filename-50%-$23.xlsx"

For now i am assuming the following (please comment and inform me if I am wrong)

  • You want to save a file you have open (word doc or excel sheet?)
  • You want to name that file based on the values in F18 and M13
  • The values can be either percentages or a cost in dollars
  • F18 will always be a percentage and M13 will always be in dollars
  • Values entered into cells are just the numerical value and the cells have been formatted to % or currency.

(It will help if in the future you lay your problem out a bit more obviously and include what you have tried so far)

    Sub testSave()

Dim SaveToDirectory As String

 'just checking string looks good comment or delete when happy
MsgBox ("other text you want " & Cells(18, 6).Value * 100 & "%-$" & Format(Cells(13, 13).Value, "#,###.##"))

    'set save directory
    SaveToDirectory = "C:\Users\DMASON2\Documents\"

    'set file name
    ActiveWorkbook.SaveAs Filename:=SaveToDirectory & "FileName-" & Cells(18, 6).Value * 100 & "%-$" & Format(Cells(13, 13).Value, "#,#*.##"), FileFormat:=xlWorkbookDefault


    End Sub

Now to explain what i did.

Firstly when you format a cell to percentage value and enter in 0.5 excel will change it to 50% automatically however the cell value is still 0.5 so when you access it you just need to do the same process excel does to display a basic number as a percentage.

  1. multiple by 100 (0.5 becomes 50)
  2. Add in the percentage char after the number

    Sheets("Input").Cells(Row, Column).value * 100 & "%"
    

Now the currency is simpler as the value that you see doesn not change, all you need to do is add in the currency Char

   "$" & Sheets("Input").Cells(row, column).value

Now for saving the document. As i has no idea what sort of document you wished to save i just used saving the active one as an example. The filename is a combination of the location (saveToDirectory) and the actual filename we constructed above. Note i have just called it "filename" (you can change this to a variable string or anything you want). Finaly for excel docs you have a number of different options whens saving a workboot, i chose to use the users default type. See file types for saving excel docs here

  ActiveWorkbook.SaveAs Filename:=SaveToDirectory & "FileName-" & Cells(18, 6).Value * 100 & "%-$" & Cells(13, 13).Value, FileFormat:=xlWorkbookDefault

UPDATE

I found a very simple way to include commas in the currency numbers using Format

      Format(value, #,###.##)

or

      Format(value, "Standard")

or (you can omit the $ sign from the string if you use this version)

      Format(value, "Currency") 

This method can also be used for the Percent values (again you can omit the % sign in string with this method)

      Format(Value, "Percent")

Information on Format function See

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top