Formatting a line in a comment using VBA - excel

I'm struggling to find a way to format a line that in a comment that is required to be a currency value.
I've set the variable as currency, but it reverts back to basic when used in a comment.
Dim wage As Currency
wage = Range("b14")
Range("C14") = wage
Range("D14").AddComment
Range("D14").Comment.Visible = False
Range("D14").Comment.Text Text:=
Application.UserName & Chr(10) & wage & Chr(10) & "Joe Bloggs"
When i hover the wage variable in break mode is showing the value in b14 as plain, however i require it to appear as currency (£#.##)
The following syntax appears when i start writing
Text([Text], [Start], [Overwrite]) as string
I'm not sure, but could the overwrite section be the key???
Thanks in advance

If cell B14 has already been formatted to the proper currency format, then replace:
Dim wage As Currency
wage = Range("b14")
with:
Dim wage as String
wage = Range("B14").Text

Related

How to use user defined column names for calculating a field in excel vba?

I am using excel vba, to make a pivot table. After I build my pivot using the two columns in the value section of pivot "Value MAT19" and "Volume(Kg) MAT 19", I want to add a derived field i.e 'Value MAT19'/'Volume(Kg) MAT 19' to the pivot. However I don't want to fix the column names myself instead give user the control to select the columns in the pivot (it could be Value MAT18 or Value MAT17 and similarly for the Volume column) and hence I am using the InputBox feature of VBA. But I after I run my macro the calculated field does not show up. Here is the code I wrote for it. I have declared myValue and myVolume as String variables. The column Final API is getting displayed in the pivot but Final Val/Vol is not showing up.
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add "Final Val/Vol", _
"=myValue /myVolume", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("Final Val/Vol"). _
Orientation = xlDataField
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add "FinalAPI", _
"='Value MAT19' /'Volume(Kg) MAT 19'", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("FinalAPI").Orientation = _
xlDataField
EDIT: This is the code that I have added according to Cyril's suggestion, but it still doesn't add the "Final Val/Vol" column.
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add "Final Val/Vol", _
"=" & myValue & "/" & myVolume, True
ActiveSheet.PivotTables("PivotTable1").PivotFields("Final Val/Vol"). _
Orientation = xlDataField
This is where I add myValue and myVolume and ask use for input, also declaring default column values:
Dim myVolume As String
Dim myValue As String
myVolume = InputBox("Please enter volume column name", "Volume", "Volume(Kg) MAT 19")
myValue = InputBox("Please enter value column name", "Value", "Value MAT19")
Posting comments as answer to close-out this resolved issue:
Try:
"='" & myValue & "'/'" & myVolume & "'"
The inputbox values need captured as variables and then used with appropriate syntax, e.g., 'column_name'

How to insert a formula with VBA that contains a number varibleex

I have a formula that I'm trying to insert in cell D8. The value is in cell M15, so I'm trying to take that value and make the formula (value in M15 - D12). I have the following code, but I keep getting an error (400 error). Can someone help figure this out?
Dim chargeinput As Worksheet
Set chargeinput = model.Worksheets("Charge Input")
Dim i As Long
i = bdws.Range("M15").Value 'dbws is a worksheet set as a variable, it changes
but the value is always in M15
chargeinput.Range("D8").Formula = "=i-" & D12
Your formula construction is using i as something the worksheet recognizes; concatenate it into the formula. D12 should be D12 on the Charge Input worksheet.
chargeinput.Range("D8").Formula = "=" & i & "-D12"
'does i have to be there?
'is it bdws or dbws? Your sample is confusing
chargeinput.Range("D8").Formula = "='" & bdws.name & "'!M15-D12"

How would I extract the formatted text from a cell in VBA?

Lets say A1 is a date formatted to "YYMMDD" and I enter 7/7/2014 in the cell.
The cells string changes to "140707" but the function bar still shows up as "7/7/2014"
When I try to write this code:
Dim dateCell As String
dateCell = Cells(1, "A")
dateCell will equal to "7/7/2014" and not "140707"
I want the cell to be formatted that way so that when anyone puts in a date, it'll automatically change it to yymmdd. Now how would I get the text dateCell to equal 140707 and not 7/7/2014?
I would greatly appreciate it!!
Thanks
You can see the three different ways to get at the value in a cell here. Put a date in A1, and format it as you format it.
Public Sub test()
Dim r As Range
Set r = Range("a1")
MsgBox ("Value:[" & r.Value & "] Value2:[" & r.Value2 & "] Text:[" & r.Text & "]")
End Sub
In your case you want the Text.
In VBA, you can declare a variable as type Date instead of String which will solve that part of the problem.
In Excel, you need to specify a custom number format to "yyMMdd". Excel will understand that this cell is a date from the value that is in it and extract the proper year, month and day and display it as you specified.

Display percentages in file name

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

Excel macro - adding a word to a currency field

I've successfully copied a currency value to a field. Now I need to add a word, but this always makes me lose my currency format. Is there a way to solve this?
Example
Range("A2").Value = Range("A1") & "/link"
"Output"
A1 = € 2.800,00
A2 = 2800/link
"Wanted output"
A2 = € 2.800,00/link
I know I can record a macro and copy that, but that's only applicable for that certain case. The number is variable.
Thank you for your help.
Range("A2").Value = Range("A1").Text & "/link"

Resources