Excel 2007 VBA Code- Splitting Cells - excel

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

Related

How can I remove the currency symbol from values in selected cells?

I export Excel files from a website. The exported files have number values with a currency sign before them in a number of columns although the number format of the cells is "General".
I want to remove the currency sign.
Another tricky thing is the headers of these columns and even the column indexes are not constant.
Is there a way to select the cells/columns and remove the currency sign that can work on any Excel file I am working on? Is there an easier solution?
Example of the problem:
try
Range("K2:M6").Replace What:="$", Replacement:=""
or if your prefer selection before:
Selection.Replace What:="$", Replacement:=""
(it's just as quick by hand: just press ctrl+h)
Sub RemoveDollarSigns()
Dim cell As Range
For Each cell in Selection
cell.Value = val(replace(replace(cell.Value, "$", ""), ",", "")
Next cell
End Sub

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

#NAME? How to read a formula bar to change the formula of an error - NOT overwritting it

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

formatting excel cell

I am in trouble. I have thousands of customer data having name, address, zip. I have data in excel and the problem is in zip code. The Zip should be 5 character long and if it is not, we have to add zero at front to make equal to five character. Since there are thousands of data and it is not feasible to change it one by one, can somebody suggest me to format the column of zip so that it could be 5 character long.
You can use the custom format and use the type : 00000
Write the following formula in front of that zip column:
=IF(LEN(A1)>5,"Invalid",RIGHT("00000" & A1,5))
Just replace the cell reference name A1 to the first cell of zip code column and then drag the cell down. Now all those zip codes that are less than five characters will be filled with leading zeros. Also It will show Invalid in cells which have a zip code of more than 5 characters length.
Apply this formula to the cell values, i.e. for A1 in this example:
=TEXT(A1,"0000#")
Copy/paste the formatted values (as text) into the desired column if you need them in a specific column.
Simply mark the whole column.
Than right click to get into the "format cells" settings.
There you go to "special format" and there you should mark "ZipCode".
Here is a picture of the options. It is in german, so I hope you will find it anyway ;)
Quick macro to convert the range to Text format, and append leading zeros if the cell is less than 5 characters.
Sub AddLeadingZeros()
Dim rng As Range: Set rng = Range("A1:A10") '<modify as needed, the cells containing ZIP'
Dim cl As Range
rng.NumberFormat = "#"
For Each cl In rng
If Len(cl.Value) < 5 Then
Do
cl.Value = "0" & cl.Value
Loop While Len(cl.Value) < 5
End If
Next
End Sub
select your column
Format-> cell
Special format-> zip code
make sure you save the changes... it shouldn't remove zero...
or try a "user defined" format... but I never tried it.

How to Insert Double or Single Quotes

I have a long list of names that I need to have quotes around (it can be double or single quotes) and I have about 8,000 of them. I have them in Excel without any quotes and I can copy all of the names and paste them no problem but there are still no quotes. I have looked and looked for an Excel formula to add quotes to the name in each row but I have had no luck. I have also tried some clever find and replace techniques but no have worked either. The format I am looking for is this:
"Allen" or 'Allen'
Any of those would work. I need this so I can store the info into a database. Any help is greatly appreciated. Thanks
PS:
I have found other people online needing the same thing done that I need done and this solution has worked for them but I do not know what do with it:
You can fix it by using a range
variable (myCell for example) and then
use that to iterate the 'selection'
collection of range objects, like so
Sub AddQuote()
Dim myCell As Range
For Each myCell In Selection
If myCell.Value <> "" Then
myCell.Value = Chr(34) & myCell.Value
End If
Next myCell
End Sub
Another solution that also worked for others was:
Sub OneUglyExport()
Dim FileToSave, c As Range, OneBigOleString As String
FileToSave = Application.GetSaveAsFilename
Open FileToSave For Output As #1
For Each c In Selection
If Len(c.Text) <> 0 Then _
OneBigOleString = OneBigOleString & ", " & Chr(34) & Trim(c.Text) & Chr(34)
Next
Print #1, Mid(OneBigOleString, 3, Len(OneBigOleString))
Close #1
End Sub
To Create New Quoted Values from Unquoted Values
Column A contains the names.
Put the following formula into Column B
= """" & A1 & """"
Copy Column B and Paste Special -> Values
Using a Custom Function
Public Function Enquote(cell As Range, Optional quoteCharacter As String = """") As Variant
Enquote = quoteCharacter & cell.value & quoteCharacter
End Function
=OfficePersonal.xls!Enquote(A1)
=OfficePersonal.xls!Enquote(A1, "'")
To get permanent quoted strings, you will have to copy formula values and paste-special-values.
Assuming your data is in column A, add a formula to column B
="'" & A1 & "'"
and copy the formula down. If you now save to CSV, you should get the quoted values. If you need to keep it in Excel format, copy column B then paste value to get rid of the formula.
Easier steps:
Highlight the cells you want to add the quotes.
Go to Format–>Cells–>Custom
Copy/Paste the following into the Type field: \"#\" or \'#\'
Done!
Why not just use a custom format for the cell you need to quote?
If you set a custom format to the cell column, all values will take on that format.
For numbers....like a zip code....it would be this '#'
For string text, it would be this '#'
You save the file as csv format, and it will have all the quotes wrapped around the cell data as needed.
Or Select range and Format cells > Custom \"#\"
If you save the Excel file as a CSV format file, you might find that the result is convenient to inserting into a database, though I'm not sure all of the fields would be quoted.
I would like to thank Guria for the answer
from https://www.exceldemy.com/
I would like to summarize the methods, there are more than 4 methods:
Let A1 be your cell where you want to insert quotes.
1 .
For Double Quotes:
=CHAR(34)&A1&CHAR(34)
For Single Quotes:
=CHAR(39)&A1&CHAR(39)
2 .
=CONCATENATE("'",A1,"'")
3 .
="'"&A1&"'"
4 . Apply Custom Format.
Suppose you have a number and you have to insert quotes on that number:
Right click the cells:
Then click Format Cells
You will get this screen:
In the Type box write
'#'
Click 'OK' at the bottom of the screen.
You will get the result:
If you have text written in the cell then:
Click 'OK' at the bottom of the screen.

Resources