When I use Excel it allows me to set specific words within a cell to bold or italic or even change the text size.
Is there a way of doing this in VBA?
I have two cells with text. One cell contains a list of words which I separated into an array. The other cell contains a few sentences.
I want to write a macro that highlights all words from cell 1 in cell 2.
My idea was to use the array and InStr to search for the position of my words in cell 2. Once found I wanted to split cell 2, format one word an put everything back together.
Maybe this is possible via Word?
I believe the answer could be found here:
excel vba: make part of string bold
Specifically,
ActiveCell.FormulaR1C1 = "name/A/date" & Chr(10) & "name/B/date" & Chr(10) & "name/C/date"
With ActiveCell.Characters(Start:=25, Length:=4).Font
.FontStyle = "Bold"
End With
Related
I am just getting started with VBA, using my old copy of Word 2010. I want to resize two of the columns in a three column table and format the text that is in the columns. This code was generated by Word's macro recorder:
Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=3, _
NumRows:=14, AutoFitBehavior:=wdAutoFitContent
With Selection.Tables(1)
.Style = "Table Grid"
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
End With
Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
Now I want the first two columns to be 1.3 inches wide and the third column to be 5.1 inches wide. Then I want to change the formatting of the text in the third column to increase the font size. The macro recorder doesn't seem to record when I resize columns. Any suggestions as to how to edit this code?
Controlling tables is rather complicated. Word does much of it the way it sees fit, but if you use VBA you must take control. It really isn't desirable that you should make your life even more miserable by invoking the Selection object. Try and make your object the table itself, for example, like this.
Dim Tbl As Table
Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=14, _
NumColumns:=3, _
DefaultTableBehavior:=wdWord8TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
' wdWord8TableBehavior = doesn't change table size to match content
The table will be inserted following the selection. You may want to collapse your selection range before running this code. But now that you have a table object you can format it to your heart's content without selecting anything. There are dozens of properties. One of them which you will want to determine is Tbl.PreferredWidth = InchesToPoints(7.7) Word will not set this property automatically to the total width of your columns.
Your table has 3 columns which you can address as Tbl.Columns(1) to 3. You can set the width for each column, like Tbl.Columns(3).Width = InchesToPoints(5.1)
Similarly, you can address each row as Tbl.Rows(1) and up. Individual cells are addressed by Row and Column numbers, for example, Tbl.Cell(1, 3) which is the 3rd cell in the first row. Avoid merging cells because that will prevent VBA from being able to count them off.
The text in a cell is contained in its range, for example, Tbl.Cell(1, 3).Range.Text. You can both read and write this property. Bear in mind that Word keeps a paragraph-end mark at the end of each cell's range. When you write to a cell Word will add it for you, even if you thought of adding it yourself. But when you read a cell's text you need to remove the last character, for example,
Dim Rng As Range
Set Rng = Tbl.Cell(1, 3).Range
With Rng
.End = .End - 1
Fun = .Text
End With
Here Fun is the variable (As String) that contains the actual text part of the cell's contents.
You can address the paragraphs within a cell as part of the range, for example, Tbl.Cell(1, 3).Range.Paragraphs(1). I always avoid having more than 1 paragraph into any cell. However, you could address more paragraphs as (2) etc. You can apply all formatting available for paragraphs to each paragraph in each cell, and all the text in each paragraph can be subjected to all the formatting Word has available for text.
I am working with a word template document that extensively uses tables to separate values. This has been great for most of my VBA code, as it makes it very simple to set values where I need to make edits. However, there is one place, in the header, where a single cell contains three lines of text. I need to set the value of JUST the first two lines in this cell.
QUESTION:
How do you set the value of JUST the first two lines of text in a given cell? Or, more broadly, can you set Range to be one line within a cell?
What I have right now, which sets Range as the whole cell and sets the value as "NEW TEXT":
ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
With ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
.Range.Tables(1).Cell(1, 2).Range.Text = "NEW TEXT"
End With
Thank you for any expertise you can lend me.
To replace (eg) the second line:
With ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
.Range.Tables(1).Cell(1, 2).Range.Paragraphs(2).Range.Text = "NEW TEXT" & vbLf
End With
I'm trying to build a small macro that allows the user to format multiple different documents at once.
I would like for the user to be able to enter into a particular cell within the document containing the macro a particular piece of text.
I then want for this piece of text to be able to be drawn upon in the macro while affecting a different document.
For instance, a code to add another column might say
Worksheets(1).Range("A1").EntireColumn.Insert
Instead of specifying the column (A), I would like it to draw on a value in the host document. For instance, the user types "G" into the particular cell, and then clicks a button to run the macro, and the macro will dynamically know to affect column G in all excel documents it targets based off of the value in the host document.
I hope this makes sense.
Any suggestions for the sort of functions I should be looking at to make this work?
"Any suggestions on the sort of functions I should be looking at?"
Here's a few...
To get the value which is entered...
If the cell will always be in the same address, say A1:
' Define a string variable and set it equal to value in A1
Dim cellText as String
cellText = ThisWorkbook.ActiveSheet.Range("A1").Value
or instead of using Range you can also use Cells which takes a row and column number.
cellText = ThisWorkbook.ActiveSheet.Cells(1, 1).Value
If the cell changes then you may need to look into the Find function to look for a label/heading next to the input cell. Then you can use it (easily with Cells) to reference the input...
Once you have this variable, you can do what you like with it.
To put this value into cell B3 in another (open) workbook named "MyWorkbook", and a sheet named "MySheet" you can do:
Application.Workbooks("MyWorkbook").Sheets("MySheet").Range("B3").Value = cellText
To insert a column at cellText, do
Application.Workbooks("MyWorkbook").Sheets("MySheet").Range(cellText & "1").EntireColumn.Insert
Notably here, the & concatonates the strings together, so if
cellText="B"
then
cellText & "1" = "B1"
Further to your comment about moving values between sheets, see my first example here, but always refer to the same workbook. If you find yourself repeatedly typing something like
ThisWorkbook.Sheets("MySheet").<other stuff>
then you can use the With shorthand.
With ThisWorkbook.Sheets("MySheet")
' Starting anything with a dot "." now assumes the with statement first
.Range("A1").Value = .Range("A2").Value
.Range("B1").Value = .Range("B2").Value
End With
Important to note is that this code has no data validation to check the cell's value before using it! Simply trying to insert a column based on a value which could be anything is sure to make the macro crash within its first real world use!
How would one use the contents of a text box to reference a specific cell? I would like to be able to enter a cell range (A1:A10, for instance) into two text boxes and then apply a function to that specific range of cells.
Range(textBox1.Text & ":" & textBox2.Text)
(I have avidly searched the forum, but the only similar 'replace' questions I could find were related to Python, Java etc, and not VBA)
I have a table within a MS Word (2010) document (it has two columns but only the second column has text in)
Some cells in the second column have one line of text and NO paragraph mark
Other cells in the second column have two lines of text and TWO paragraph marks (^p)
There is no regular pattern between these two types of cells
Where there is a second paragraph mark, this always occurs directly before the end-of-cell marker
I really need a macro to remove (replace with " ") this second (=final) paragraph mark from each cell where it occurs, but to ignore any first paragraph marks within the cells.
I would be extremely grateful if someone out there has the time and inclination to help me with this macro. As you might have guessed I have little experience in VBA despite attempting to give myself a crash course: I am more used to recording macros which is not an option in this more intricate case.
My hopes were raised when Google found me this- which looks like it could be adaptable http://www.vbaexpress.com/kb/getarticle.php?kb_id=334; but I have no idea how to tweak it to only replace a SECOND (/final) occurrence of ^p.
Thank you in advance.
How about:
Dim tbl As Table
Dim c As Cell
For Each tbl In ActiveDocument.Tables
For Each c In tbl.Columns(2).Cells
'The end of a cell without a carriage return is vbCr & Chr(7)
If Right(c.Range.Text, 3) = vbCr & vbCr & Chr(7) Then
c.Range.Text = Mid(c.Range.Text, 1, Len(c.Range.Text) - 3) & Chr(7)
End If
Next
Next