Inserting TextBox Values into a Formula - excel

I was hoping you could help me find the solution to my macro coding.
The textbox values are not being entered into the formula when pasted into the active cell.
Here is my coding:
ActiveCell.Formula = “=“”BOX “”&IF(OR(M3=1,M3=3),N3+TextBox1,N3-TextBox1)”
I want the cells end result to look something like this: BOX 20
Thanks

Do not use those fancy quotes, as VBA does not understand them.
Variables cannot be inside of quotes, so use this instead:
ActiveCell.Formula = "=""BOX ""&IF(OR(M3=1,M3=3),N3+" & TextBox1 & ",N3-" & TextBox1 & ")"
Keep in mind that if your textbox has something other than a number in it, or it is blank, you will get an error.
If IsNumeric(TextBox1) Then
ActiveCell.Formula = "=""BOX ""&IF(OR(M3=1,M3=3),N3+" & TextBox1 & ",N3-" & TextBox1 & ")"
Else
MsgBox "'" & TextBox1 & "' is not a numeric value."
End If
If you compare my answer to your question, do you see how your formula is all red? That means it's all part of a literal string, so there are no variables in it, and since your textbox is a variable, it needs to be outside of the quotes.
In my code, see how the TextBox1 variable is not red?

Named Ranges and Control Settings Setup
Named Ranges
• Box
• SelectedFruit
• Fruit
Settings
• Textbox:
- LinkedCell = Box
• Combobox
LinkedCell
ListFillRange
The trick is to use Named Ranges and Control settings. No VBA required!

Related

Extract current cell address

I need to extract the current cell in VBA without passing it in parameter of the function.
I mean, for example, if the formula is in cell B35, I want to extract the address (line = 35, column = 2), in the VBA code of the formula.
I thought the active cell would do it, but no, it extracts the address of the cell where the cursor is.
Do you know how I could do it?
Thanks in advance for help.
I think you mean Application.Caller which is the object that calls VBA. It can be used inside a function to return the cell calling the UDF, not the ActiveCell
Application.Caller property (Excel)
An easy example would be this:
Public Function ThisCell() As String
ThisCell = "ROW:" & Application.Caller.Row & " - COLUMN:" & Application.Caller.Column
End Function
If you type now in any cell =ThisCell() it will return its row and column:
Notice the output is different in all of them, even if they use the same UDF with no arguments.
A similar one to Foxfire And Burns And Burns' answer, but no vba needed.
Apply this formula, then copy it and paste as values.
="line = "&ROW()& " column= "&COLUMN()
Try below codes
Sub ExAddress()
MsgBox "Row: " & ActiveCell.Row & vbNewLine & _
"Column: " & ActiveCell.Column
End Sub

how to get msgbox text from reference cell, which includes newline characters

edit: all responses have missed my point, so i've removed any mention of special characters and just want to focus on the line break vbCrLf
i have a combobox populated from a range of cells, and i want a msgbox to pop up based on the user's selection in said combobox, such that the msgbox text is taken from a cell which corresponds to the user's selected cell (i.e. the cell linked to the user's cbox selection). for example:
user selection
msgbox text
scarf
It's 40 degrees. Are you sure you want a scarf?
swimshorts
Don't forget your sunscreen!
so that's the reason for trying to fill the cbox from a cell, rather than from VB.
my problem is how to write the newline and special character in the reference cell and have msgbox format it correctly.
i.e. MsgBox "It's 40 degrees." & vbCrLf & "Are you sure you want a scarf?" does what i want, but if i place that text in a cell and then try to reference that cell with a msgbox, e.g. MsgBox Sheet1.Range("B2"), then the msgbox just prints the text without formatting.
e.g. https://imgur.com/a/Hi3HVWG
please, any help here would be greatly appreciated
To add a line feed in a cell, if you are entering it from the worksheet:
A1: ="abc" & CHAR(10) & "def"
For the linefeed character, you can type abcalt+enterdef
If you are entering it from VBA, then the code line would be:
Range("A1") = "abc" & vblf & "def"

How to Trim Headings in a table with VBA code

I'm trying to trim the headings of a table with VBA code because some of them have spaces in front that varies every month, which makes it difficult for coding.
When I break down the code and run it step by step it works fine but when I run the whole macro it removes some of my headings and replace them with info from a different sheet row or just "column 1", "column 2", etc.
I believe I'm missing some code reference when it calls the (" & .Address & ") selection?
It's replacing the headings from a sheet where the cell is active. (If it was the last cell to click on before running the macro).
I've tried just using the Trim function, but because it's an array for the range, it doesn't work, and someone suggested to use the "evaluate" function.
I've tried using the trim function as a WorksheetFunction as well but it gave me an error "Run-time error 13" Type-mismatch". Which was on the following code:
With wsDispoData.ListObjects("Table_DispoData").HeaderRowRange.EntireRow
.Value = WorksheetFunction.Trim(.Value)
End With
This is the current code I'm using that replaces wrongly.
Trim headings
With wsDispoData.ListObjects("Table_DispoData").HeaderRowRange.EntireRow
.Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
Expected results should be for example:
Current headings: " SOH" and " Compo"
Trimmed: "SOH" and "Compo"
I would just enumerate through the header row and check each value
Dim Cell As Range
For Each Cell In wsDispoData.ListObjects("Table_DispoData").HeaderRowRange
Cell.value = WorksheetFunction.Trim(Cell.value)
Next Cell

Syntax for formula =SUM(range(cells(41,10),cells(49,10)))

The "formula" =SUM(range(cells(41,10), cells(49,10))) returns "#NAME"?
I need to create the formula with quotation and & and place it in certain cells.
Also the values in the Cells() function are variables in the code.
The example is a result of one placement of the formula at a particular cell. I've tried a : also to separate the cells(), also tried not using Range().
with Worksheets("Sheet1")
.Range("A1").Formula = "=SUM(" & .range(.cells(41,10),.cells(49,10)).Address & ")"
End with

excel vba: insert formula with dynamic cell reference using vba?

I am inserting the following hyperlink as a formula using vba:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\log.txt"",""View Log"")"
This works fine, however if my value in cell C was to change then my hyperlink becomes invalid and won't work because it has retained the value of the cell at the time in which the formula was entered.
I need a way of making a dynamic reference to my cell C in the event that the value should change then my link will also change.
Can someone please show me how to do this? Thanks in advance
Your code is taking the value from column C and building a string using that value that looks like this:
"S:\Tasks\FolderName\log.txt"
Instead, what you want to do is build the following string:
"S:\Tasks\" & C2 & "\log.txt"
To do that, use this VBA code:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\"" & C" & ActiveCell.Row() & " & ""\log.txt"",""View Log"")"

Resources