VBA Masters,
I have a merged range in Excel (A1:D7) that has a varying number of lines of text. Each new line of text starts with a "*" to denote the beginning of a new statement.
I need to be able to store each of those statements in a variable and write them out to their own individual cells rather than having it in the merged cell area.
I've attached an image of an example of the block of text.
Related
I need to be able to find every place in my document (hundreds of pages) where there is a formatting change without a space. For example:
a bold partnext to regular text
Or red text next to black with no space. I want to have my macro find each "word" (in the vba sense) like this, and execute code based on that character location accordingly. (The loop should identify the character position where the format change occurs... although I can do that part with a loop through the characters within the found word).
Is there a simpler way to do this than by looping character by character through the whole document and checking for a difference in formatting, which would be too resource-intensive?
Thanks for your help.
Problem: Pasting copied data from excel to specific location in a word file.
Currently I have code which can paste the value, but it does so to "paragraph1"
myDoc.Paragraphs(1).Range.Paste
How do I specify the exact location (by line) in which to paste the data?
Let me know if more info is required.
Thanks!
Mohd Akhtar
Word gives a number to each character in the document's body, from 1 up. It then defines a range with Range.Start to Range.End So, Paragraphs(1).Range might be equal to Range(Start:=1, End:=120).
The text contained in that range is Range.Text, Read/Write. Therefore, Paragraphs(1).Range.Text = "My new paragraph text" will replace the existing text in the document's first paragraph. ActiveDocument.Range(0, 0).Text specifies the range before the first character in the document.
In order to insert text at a specific location you have to find the location, meaning the Range. As you have seen above, if the range has a length of 0 you can insert before or between existing text, and if it has any length the new text will replace whatever was there before. New and old text need not have the same length.
Counting paragraphs is helpful to find a range. You can also count words or sentences. You can search for a specific word combination. Or you can use a bookmark. In all of these cases you define a range the text of which you can replace outright, or which you can use to find a location relative to it where to insert the text, such as the beginning or end or after the 3rd word or whatever.
You could also use some bookmarks:
You can choose where you put your bookmark and then write on it like this
ThisDocument.Bookmarks("NAME_OF_THE_BOOKMARK").Range.Text = THE_EXCEL_DATA
To place a bookmark you have to click on the selected area and then go on Insert->Bookmarks and then name it.
I have an Excel sheet with multiple pages. One of two pages is a data tab and has a long list of names and then several columns of data. (Cells A1 - F1 are headers, A2-A20 are named, B2-F20 are different pieces of data regarding each name). The second page pulls specific data (index function) from the data tab.
I have bolded info on the data tab, and when you index something, you lose formatting. Normally, I could create a VBA code to copy/paste special into the format (to preserve the bolded words), but the nature of the formatting page means it has to have merged cells. I can copy/paste the info but it does not preserve the formatting. If I try to paste special, I get the error "This operation requires the merged cells to be identically sized".
Is there a way to paste and preserve the bolded text without unmerging the formatted sheet?
You can do something like this:
Worksheets("Sheet2").Range("A1").Value = Worksheets("Sheet1").Range("A1").Value
Worksheets("Sheet2").Range("A1").Font.FontStyle = Worksheets("Sheet1").Range("A1").Font.FontStyle
This will transfer the font style to the from the data cell to the target cell, regardless if the cell is merged or not.
So, I have a custom function that concatenate different cells and put a comma between words.
For example, say I have "ABCD" "BC" then, this function will
output ABCD, BC. Now the problem is that the text will overflow in a cell and overlap with the cell next to that. In order to solve this problem,
I am thinking of just replacing the concatenated word with "Multiple" if more than 3 words are combined. Is there anyway to do this in a cell?
You can do this with conditional formatting AND keep the original underlying string as a raw value for other purposes.
Select the cells with the formula and create a conditional formatting rule based on a formula. =LEN(C2)-LEN(SUBSTITUTE(C2, ",", ""))>1
Click Format and go to the Numbers tab. Choose Custom from the list down the left side and supply the following for the Type: ;;;[color13]_((\multipl\e) I've opted to also make the font dark blue (colorindex # 13) and indent from the left.
Click OK to accept the formatting and then OK again to create the new rule.
As you can see in the sample image above, the underlying raw value remains (shown in the formula bar) but (multiple) is displayed.
More on custom number formatting codes at Number format codes
I have a cell in excel which contains a value, lets say 100000.
Now i want this value to have commas in between them to represent the thousands and millions i.e. 100,000. I can do this by changing the number format in the home menu.
Now i want this value to be copied from that cell and paste it as a label for a shape. When i am doing this the commas go away showing me just the numbers.
I want it to happen through VBA but this is not happening in excel itself.
Does anyone have a plausible solution for this?
In range object use Text property, like this:
Sheet1.Shapes(1).TextFrame.Characters.Text = Range("A1").Text