Tab delimited data in excel cell - excel

I want to build a tab delimited string and insert it in cell "A1". For some reason, it does not identify the tab delimiter. Here is my code:
Worksheets("Sheet1").Range("A1").Value = Join(Application.WorksheetFunction.Transpose(Worksheets("Sheet1").Range("B1:B11")), Chr(9))
I even tried the "vbTab" instead of "Chr(9)" but I still see no tabs in the string. Its inserting a string without any deliminator.
Thanks in advance

The tabs are there but are not being displayed. Try
Dim stg As String
Dim pos As Long
Worksheets("Sheet1").Range("A1").Value = Join(Application.WorksheetFunction. _
transpose(Worksheets("Sheet1").Range("B1:B11")), Chr(9))
stg = Worksheets("Sheet1").Range("A1").Value
For pos = 1 To Len(stg)
Debug.Print Asc(Mid(stg, pos, 1)) & " ";
Next
Debug.Print
The Immediate Window will shows the ASCII values of your strings spearated by nines.

Excel cannot visually display tabs within a cell.
As far as Excel is concerned, tab means to move to the next cell. If you copy a string that has a tab in it and paste the result into Excel, it will put the text before the tab in your selected cell and the text after the tab in the next cell.
If you just need the tab to be there then you can rest assured that it is. If you want it to display visually then modify your question to explain what you are trying to do.

Related

Tag end-of-cell marker in word

I am exploring using word to "capture formatting" when converting to a text file.
The overall aim is to capture custom document structure and format when converting to a text file. i.e to generate a text file that says (BOLD) if the text if bold, or (UNDERLINED) if underlines etc.
Following previous questions, this generally works to generate text files capturing sentences as a while (i.e. not introducing unwanted line breaks) which can then be read using power query.
Ultimately this has proved useful but I have realised that a document's structure and formatting have valuable information that so far is not captured.
I only realised yesterday that it is actually possible using word to find and replace formatting features. Here in the image below, I have used word to recognise if the text is underlined and replace this with the text (HEADER) before it. The same can be done for presumably most formatting marks (e.g. Tab).
For the most part dynamically tag headers in the Text file which I can then use to create a function in power query to iterate over various similar documents and start separating the text into relevant sections.
This being said I have come across the end of cell marker as shown here:
Initially, I thought it would be feasible to find and replace such markers with another character (|), however, it seems that that formatting mark cannot be searched for in the above way.
This being said there is some hope with people seemingly doing similar things using VBA to get Chr(13) which is apparently the value for this and recognising it.
I just want to create a script to tag each end of the cell marker with some character.
Found this answer: https://answers.microsoft.com/en-us/msoffice/forum/all/add-character-before-end-of-cell-marker-in-word/2b7fe2c3-e96e-4d34-a887-b1ddf17d512f
'If you want to add the character to only the cell that contains the cursor, use this (see http://www.gmayor.com/installing_macro.htm if needed):
Sub AddToSelectedCell()
Dim cl As Cell
If Selection.Information(wdWithInTable) Then
Set cl = Selection.Cells(1)
cl.Range.Text = _
Left(cl.Range.Text, Len(cl.Range.Text) - 2) _
& "a" ' <= the character you want to add
Else
MsgBox "Put the cursor in the cell and rerun this macro."
End If
End Sub
'If you want to add the same character to the end of every cell in the table that contains the cursor, use this instead:
Sub AddToEveryCell()
Dim tbl As Table
Dim rw As Row
Dim cl As Cell
If Selection.Information(wdWithInTable) Then
Set tbl = Selection.Tables(1)
For Each rw In tbl.Rows
For Each cl In rw.Cells
cl.Range.Text = _
Left(cl.Range.Text, Len(cl.Range.Text) - 2) _
& "a" ' <= the character you want to add
Next cl
Next rw
Else
MsgBox "Put the cursor in the table and rerun this macro."
End If
End Sub

VBA script to remove certain text from merged cell to Text To Columns format

I have VBA code that takes a merged cell with text, which copies the column data from the workbook and paste the merged text data which places each into a new column. In my script I need it to remove certain strings within the merged cell, then add specific integers into the new cell according to the number of hours.
What my script does is that a user will select the file that needs to be extracted within windows explorer and will take the merged column and will get each text and make it into a new column as below.
Below is the workbook I am using to get the data. This has the merged cells. As you can see the highlighted portion is what I need extracted. My script takes all the data from that merged column. I just need the highlighted portion.
What I need to do is remove all the text you see in the beginning that is highlighted. The only strings that I need removed are "but" and "or". Their are numbers at the end of each text column and if the number is 8 then I need to replace the removed text from the beginning with an integer of 1. If there is a integer after the text that is being removed then add that number instead.
Below is what the outcome is supposed to look like. I managed to use Text To Columns manually within excel to get it to show like this.
Here is my script:
Sub Get_Data_From_File()
Dim FileToOpen As Variant
Dim i&, s, v
'Variable declaration
Dim sWorkbook As Workbook
'Create New Workbook
Set sWorkbook = Workbooks.Add
'Save Above Created New Workbook
sWorkbook.SaveAs Filename:="C:\Users\username\Desktop\MyFile\Test.xlsx"
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Worksheets("mydata").Range("I3").copy
Workbooks("Test.xlsx").Worksheets("Sheet1").Range("I3").PasteSpecial xlPasteValues
s = OpenBook.Worksheets("mydata").Range("I3").Value2
s = Split(s, vbLf)
ReDim v(0 To UBound(s), 1 To 2)
For i = 0 To UBound(s)
v(i, 1) = s(i)
Next
Workbooks("Test.xlsx").Worksheets("Sheet1").Range("I3").Resize(UBound(s) + 1).Value2 = v
End Sub
As you can see I need to repeat the text My_name_John_Doe for each the highlighted portion that his being sent to the new columns. Since it's three separate lines, but My_name_is_John_Doe is repeated three times.
If I can just get the Text To Columns and the strings removed in VBA that will be a great help.
To get the Text to Columns in excel manually, simply go to Data tab, select Text To columns. Check the Delimited option, click next, then under Delimiters, everything should be unchecked except the "Space" option.

How to remove first "=" in all excell cells

How to remove first "=" in all excell cells. There are cells where phone number has this character. Example: =1234/567 and this phone number will be calculated.
Edit: =1234/567 must be converted to 1234/567
Edit: To clear things abc=123 must not be changed, because = is in a middle of word.
I assume you are referring to the '=' sign in formulas. If Excel had regex replacements, you could do this (Word does), but I don't think it does.
How about a quick VBA solution?
Sub RemoveLeadingEquals()
Dim r As Range
For Each r In ActiveSheet.UsedRange
If Left(r.Formula, 1) = "=" Then
r.NumberFormat = "#" ' This *might* not be necessary
r.Formula = Mid(r.Formula, 2)
End If
Next r
End Sub
Check if the forced text formatting (commented above) is necessary. It won't hurt.
Select all cells contain your numbers
Press CTRL+H
Fill "Find What" input box with "="
Just left blank the "Replace with" input box
click "Replace All".
this is so simple you want to remove just = sign
so
press CTRL+f
enter =
Replace with nothing
that's it

Excel VBA cell character limit

I am building a macro to export data from a custom outlook form to an excel workbook. The data will be extracted in the string format from user-defined fields in the outlook form. The string data will then be entered into the values of the excel cells. The strings may contain a huge number of characters.
I understand that each excel cell can hold 32,767 characters. What happens if I try to enter a string with more than 32,767 characters in an excel cell? What will happen to the excess characters?
I realize that the characters do not appear, but can these lost characters be recovered somehow?
They are simply lost and there is no way to recover them. You can test this out if you like:
Sub test()
Dim i As Long
Dim text As String
For i = 1 To 32767
text = text & "a"
Next
text = text & "end"
Range("A1").Value = text
'"end" will be lost
End Sub

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