Display percentages in file name - excel

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!

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.
multiple by 100 (0.5 becomes 50)
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

Related

Concatenate date, text and cell value

I am trying to use VBA to create a description that includes the Month/Year, text, and the value of a cell. I have tried two methods and neither one is working for me.
First i tried this:
ActiveCell.FormulaR1C1 = _
"=CONCATENATE(Format(DateAdd("m", -1, now), "mmmyy"), " My Text ",RC[-19])"
Which gives a
run-time error 1004.
Next I tried
Range("X10").Value = Format(DateAdd("m",-1,now), "mmmyy") & " My Text " & E10
Which output the date and the text but not the value of the cell.
I'm not sure where I'm going wrong with this. Searching on here got me to this point, but I'm stuck.
Thanks for any help.
'Let: X be your Workbook index, Y be your sheet index, YY be your row index, XX be your column index
Sub macro2()
MsgBox Month(Date) & "/" & Year(Date) & " " & Workbooks(X).Sheets(Y).Cells(YY, XX).Value
End Sub
All of these may be stored into a string variable for portability across your project.
Good luck!.

How to keep a number format after I've converted multiple strings to a hyperlink

I want to write a hyperlink to a cell yet format it "0000". The code I have come up with so far is as the below.
Dim PictureNum As String
Dim FolderLoc As String
PictureNum = Format(inputwks.Range("picturenumcell").Value, "0000")
FolderLoc = inputwks.Range("imagefolderpath").Value
with recordwks
.Hyperlinks.Add anchor:=.Cells(nextRow, PictureCol2), Address:=(FolderLoc & "\" & PictureNum & ".png"), TextToDisplay:=Format(PictureNum, "0000")
End with
The .Cells(nextRow, PictureCol2) in the hyperlinks line puts the picture number as a hyperlink into a specific cell in the recordwks worksheet.
For example, if it's picture number 0002.png, the resulting hyperlink text to display ends up just putting it as "2" rather than "0002"
How do I modify it to make it say "0002"?
Many thanks for any help.
Prefixing cell content with ' will make Excel treat it as text, so you can do this:
TextToDisplay:= "'" & Format(PictureNum, "0000")

Word VBA format number in a Table cell - cell text unrecognized

I'm constructing a Word doc from VBA (Excel) with Excel table data.
The data was placed in the table OK.
The last column has numbers (e.g. 456789) That I want to number-format (e.g. $456,789).
Word adds chr(13) and chr(7) at the end of each table cell value (so that the length of the visible value 456789 is not 6 but 8).
Without the Replace statement below to eliminate these two chars I could not treat the value as a string and further manipulate.
Is this the best way vba has to offer for accessing table cells values as a standard type such as String? Why do I need to eliminate the ending chars in the process? what am I missing?
For Each cell In .Columns(3).Cells
strCellValue = CStr(cell.Range.Text)
strCellValue = Replace(strCellValue, Chr(13) & Chr(7), "")
If IsNumeric(strCellValue) Then
cell.Range.Text = Format(strCellValue, "$#,##0")
End If
Next
Thanks!
There are numerous ways of doing this. Perhaps the simplest is:
For Each Cell In .Columns(3).Cells
strCellValue = Split(Cell.Range.Text, vbCr)(0)
If IsNumeric(strCellValue) Then Cell.Range.Text = Format(strCellValue, "$#,##0")
Next

Excel VLOOKUP Increment

I am creating a excel file that will pull a daily report from a folder on a different drive everyday. I was wondering if there is a way to increment a file name in the formula by 1 easily?
The formula I am currently using is:
=VLOOKUP("Oil Production(Sm³)",'G:\DPNA\Non_Op_Assets\Hib\Pe\Production\Reports\Daily Report\2019\01 Jan19\[190113 Daily Report.xls]Daily Report'!$A$1:$P$500,3,FALSE)*1
I was wondering If it is possible to be able to get the 190113 to increment by one for the next 365 cells easily.
Not easily. It looks to me that not only do you need to increment the 190113, but it will need to flip over to 190201 for the 1st February, and also the file path has the year and the month spelled out.
This can be constructed with formulas, though. You can create the formulas that generate the path and file name bit automatically.
="Daily Report\"&YEAR(A2)&"\"&TEXT(A2,"dd MMMyy")&"\["&TEXT(A2,"yymmdd")&" Daily Report.xls]Daily Report'!$A$1:$P$500,3,FALSE)*1"
The next issue is how you use that. You could plug that into an Indirect function, but if Indirect refers to an external file, that file must be open for Indirect to work. Which defeats the purpose.
So, the other option is that you build the complete formula as a text string,
="=VLOOKUP(""Oil Production(Sm³)"",'G:\DPNA\Non_Op_Assets\Hib\Pe\Production\Daily Report\"&YEAR(A2)&"\"&TEXT(A2,"dd MMMyy")&"\["&TEXT(A2,"yymmdd")&" Daily Report.xls]Daily Report'!$A$1:$P$500,3,FALSE)*1"
then copy and paste the formula as values, then edit each pasted cell and confirm the formula it now contains.
Edit: VBA is the way to go to bulk edit, but you won't be able to record this, since you will need a loop.
Will the files be open? If so you can use indirect I just put a basic example together for you:
=INDIRECT("'C:\Reporting Archive\[Daily Sales and Orders Report - 2018121" & ROW(A1)+5 & ".xlsx]Summary - Volume'!$C$41")
I can drag this down and the 2018121 in the filename will start with a 6 then increment to a 7 then 8.
If you don't have the files open then you will need to use VBA. When reading from a closed workbook without opening it, I have always used the ExecuteExcel4Macro method like so:
strPath = "C:\Test\"
strFile = "Book1.xls"
strSheet = "Sheet1"
strRng = Range("A1")
strRef = "'" & strPath & "[" & strFile & "]" & strSheet & "'!" & strRng
Result = ExecuteExcel4Macro(strRef)

mesaage box displaying values from multiple columns in excel vba

There are three columns. one has some names of tables and second column has its free space and third has percentage used.
help required is:
Msgbox should display the value of PERCENTAGE USED column in the cell it finds more than 90.
Also it should display the name of the corresponding table and the free space of the table for which it is displaying the value in step one.
msgbox output should be:
"ABCD table is 96.67% used and space left is: 7431MB "
Sample Code which displays only table name and percentage used:
Dim rngValues As Range
Dim strTableName As String
Dim cell As Range
' Adjust ranges to suit
Set rngValues = [JI3:JI90]
For Each cell In rngValues
If cell.Value > 94.45 Then
cell.Select
MsgBox Range(cell.Address).Offset(0, -268).Value _
& " table has current size: " & cell.Value
cell.Interior.Color = RGB(255, 0, 0)
End If
Sample output of this code : ABCD table has current size : 97.87%
One way to solve this problem is to use a second Range(cell.Address).Offset().Value statement to append the additional information to the text being displayed by the message box. The exact code is going to depend on how many columns away from column 'JI' the column showing the free space left is.
Assuming that it is the column before the one containing the % space used you could do the following:
MsgBox Range(cell.Address).Offset(0, -268).Value _
& " table has current size: " & cell.Value _
& " and the space left is: " & Range(cell.Address).Offset(0, -1).Value
If it isn't the column before the one containing the % space used you simply need to adjust the column offset in Range(cell.Address).Offset(0,-1).Value properly. For example if the column containing free space occurrs 9 columns after column JI change the Offset to Offset(0,9)
The first argument between the parentheses for Offset is the number of rows and the second one is the number of columns. negative offsets specify rows or columns appearing before the selected cell, postivive specify rows or columns appearing after the selected cell. '0' specifies the current row or column.

Resources