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"
Related
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!
When I enter a value on "Sheet 3" (in any Cell on the Page From B5 to OD70 & B124 to OD300) I need it to give an error (Allow the name to be entered but like color it red or something to alert them) if it doesn't match exactly a value in "Sheet 1" Column B.
I've tried some If statements with Conditional formatting but that seems to be extremely difficult for that many cells.
I'd go with a Worksheet event handler, since massive CF tends to clutter the sheet calculation and behaviour
Just activate Sheet3 code pane (right click on its tab name and choose "View Code") and place the following code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Intersect(Target, Union(Range("B5:OD70"), Range("B124:OD300"))) Is Nothing Then Exit Sub
If Target.Value <> Sheet1.Cells(Target.Row, 2).Value Then MsgBox "Attention" _
& vbCrLf & vbCrLf & "The input value:" _
& vbCrLf & vbTab & Target.Value _
& vbCrLf & vbCrLf & "doesn't match the one in Sheet1 cell B" & Target.Row, vbCritical
End Sub
There should be no difficulty applying CF. Follow these steps.
On Sheet3, select any cell. I selected C2.
Call up CF > New Rule > "Use a formula to determine which cells to format
Enter the formula =ISERROR(MATCH(C2,Sheet1!$A:$A,0)), where C2 is the address of the cell you selected to apply the CF to.
Select the formatting you want (e.g. red fill) and click OK.
From the Ribbon's CF menu select Manage Rules ...
In the dialog box that opens select the rule you just set up. In the Applies To field you will see =$C$2 (or the address of the cell you selected.
Replace this formula with =$B$5:$OD$70,$B$124:$OD$300
Press OK and the format is applied.
Cells which don't have an exact match in Sheet1!A:A will be highlighted. If you don't wnat the format applied to blank cells, use the formula below instead of the one above.
=AND(LEN(C2),ISERROR(MATCH(C2,Sheet1!$A:$A,0)))
I've been stuck on this problem for a very long time. I've come up with ways around it but each one is bringing more problems to the table.
My macro copys and pastes info from Microsoft Word into Excel and then formats the excel sheet.
Some of the cells however show a #NAME? error. The reason for this is that the cell copies in " =------- List SC". I need to get into this cell and remove the "=------" and leave only the string List SC.
I cannot just over ride it however because each time it changes. It might be "List FL" or "List BP" or any other variation.
I cannot do an InStr or any other function because the VBA code only reads it as an error and doesn't read the formula bar. It only reads "#NAME?" or recognizes an error.
Does anyone have suggestions as to what I can do?
I have included a picture of the issue. The highlights are the error and formula bar.
Thank you!
Jonathan
My macro copys and pastes info from Microsoft Word into Excel...Some of the cells however show a #NAME? error.
You must edit the string you get from Word before you paste it into Excel. You must remove all invalid characters from the string. Then you can paste it as a formula.
If ----List is the value you want in the cell, then precede it with a single quote: '----List so it will be interpreted as text.
In VBA, use the .Formula method to access the actual formula used and do the string replace on it.
With Cells(1,3)
.Formula = Replace(.Formula, "=------ ", "")
End With
So it turns out that I can store the formula into a string and add an " ' " as the first character in the string. I can then replace the existing formula in the cell with the string (below called stfF).
This is my code.
Dim strF As String
For x = 4 To 100
strF = "'" & Cells(x, 1).Formula
Cells(x, 1) = strF
Next
Here is the problem:
My table is very large and the column width is not enough to show all the text in it.
Since the text/value is generated by a formula in the cell, if I click on the cell, the Formula Bar will display the formula and not the value. Of course.
However, I think it is very useful to quickly know the content of the cell generated by the formula. If I expand the column width every time is not quick and clean.
Do you know if there is a way to solve the problem?
No there is not a way to show it in the formula bar if there is a formula in the cell.
The best thing I can suggest is to leave row 1 empty and merge A1:N1 together or similar, ,freeze row 1 if necessary
then put this formula into A1
=INDIRECT(CELL("address"))
When you select a cell on the sheet , press F9 on the keyboard and you can read the full value of the active cell in row 1
With a little VBA in the sheet you can also have it automatically update the cell
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Calculate
End Sub
Here's another solution. Simply assign a key to the following macro:
Sub DislayCellValue()
Dim outp As String
outp = "The value in the active cell is:" & Chr(13) & Chr(13) & ActiveCell.Value
MsgBox Prompt:=outp
End Sub
To display the value in a cell, click on the cell and hit Ctrl-(key).This might be helpful.
Excel 2007- I have countless old Word Tables that I'd like to put into Excel. I'd like to split the contents of the cell into two cells. Most of the cells have a very similar format (I don't need to split the ones without this format)- Text (Date). I've tried using "LEFT" or "RIGHT" but since the text string and date string are variable lengths there are no good straightforward ways. For example-
Cell A1-
"Market Value (6/16/09)" [or "Addition (12/15/09)", etc.]
I'd like to split the cell into-
Cell A1- "Market Value" and
Cell B1- "6/16/09"
Obviously if it takes the A1 data and puts it into B1/C1 I could care less.
I've seen some other split VBA modules but they don't seem to be doing the trick for me. I've looked for ways to split it using CSV but that doesn't seem to be useful either. So is there a way to use the "(" or ")" as a marker to copy text before or after the "("?
So is there a way to use the "(" or ")" as a marker to copy text before or after the "("?
Yes
Example
Cell A1- "Market Value (6/16/09)"
Sub Sample()
Dim Ar() As String
Ar = Split(Range("A1").Value, "(")
Debug.Print Ar(0) '<~~ This will give Market Value
Debug.Print Ar(1) '<~~ This will give 6/16/09)
'<~~ And the below will give you 6/16/09
Debug.Print Replace(Ar(1), ")", "")
End Sub