Currently I am writing a macro that autofills different sheets in a workbook from a parent sheet. I have written some vba script to pull through a value that is a date and concatenate that with other text.
Worksheets("sheet name").Range("B24").Value = "Text <" & Worksheets("sheet name").Range("G17").Value & ">. more text for three years."
I want to bold the word "three". I already recorded a macro to do this however the issue I am running into is the length of the date won't always be the same, it could be 11/22/2022 or 1/22/2022. The value is being placed and pulled from a merged cell.
Sheets("sheet name").Select
Range("B24:E24").Select
ActiveCell.FormulaR1C1 = _
"Test <" & Worksheets("sheet name").Range("G17").Value & ">. more text for three years."
With ActiveCell.Characters(Start:=1, Length:=146).Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
With ActiveCell.Characters(Start:=147, Length:=5).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
With ActiveCell.Characters(Start:=152, Length:=7).Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
You'll need to use the instr function.
Here's some sample code that you could probably levarage. It's based somewhat on yours. If all you're doing is bold, you don't need all that other stuff you picked up from macro recording.
Sub makePartBold()
Const cellAddress = "B24"
Const textToFind = "three"
Dim startPos As Long
startPos = InStr(1, Range(cellAddress).Value, textToFind, vbTextCompare)
With Range(cellAddress).Characters(Start:=startPos, Length:=Len(textToFind)).Font
.FontStyle = "Bold"
End With
End Sub
Related
I want to use shortcut to coloring selected text in a cell by macro. However, the only thing I can do is to coloring selected text in a cell. For example:
Sub Bold14PtMacro()
With Selection.Font
.Name = "Calibri"
.Size = 30
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Selection.Font.Bold = True
End Sub
Is there any method to accomplish this funciton?
Thanks in advance.
I used the macro recorder to change some parts of a text in a cell as the same color as the background which "hides" that part. E.g. for "Text Here 1", the "1" is changed to the same color as the background so users would only see "Text Here" for visual purposes. However, reusing this macro gives an unintended color. The cell background color is RGB (226, 239, 218)
Before macro is run:
Intended Result:
Unintended Result:
Sub Macro1()
Range("D6").Select
ActiveCell.FormulaR1C1 = "Text Here 1"
With ActiveCell.Characters(start:=1, Length:=10).Font
.Name = "Calibri"
.FontStyle = "Bold"
.Size = 20
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With ActiveCell.Characters(start:=11, Length:=1).Font
.Name = "Calibri"
.FontStyle = "Bold"
.Size = 20
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
.ThemeFont = xlThemeFontMinor
End With
End Sub
Edited code that was trimmed down from the macro recorder:
Sub Macro2()
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws.Cells(6, 4).Characters(start:=11, Length:=1).Font
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
.ThemeFont = xlThemeFontMinor
End With
End Sub
Not sure why that's happening but you could always get rid of the stuff that's been recorded but not really needed.
With Range("D6")
.Characters(Start:=11, Length:=1).Font.Color = .Interior.Color
End With
I would like to create the form control button by macro, as discussed here:
Insert form control button via VBA
Adding command buttons to worksheet at run time and also define events
and explained here:
https://www.mrexcel.com/board/threads/vba-code-to-create-macro-insert-form-control-button-assign-macro-to-button.832394/
Unfortunately, suddenly I got an error: Expected fonctuon or variable" with debugger pointing roughly the Selection statement.
Sub SunButton()
'
' Macro1 Macro
'
'
ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8).Select
Selection.OnAction = "Sun"
Selection.Characters.Text = "Sun"
With Selection.Characters(Start:=1, Length:=3).Font
.Name = "Calibri"
.FontStyle = "Bold"
.Size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 32
.Placement = xlFreeFloating
.PrintObject = False
End With
End Sub
I don't know what can be wrong here.
According to this thread:
https://www.mrexcel.com/board/threads/compile-error-expected-function-or-variable.308885/
it occurs, when you have another macro called "selection" but I don't have it in my case.
How can I remove this error and proceed my recorded macro?
Your code is wrong, as you are applying button properties to its Font. Try:
Sub SunButton()
'
' Macro1 Macro
'
'
With ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8)
.OnAction = "Sun"
.Caption = "Sun"
With .Characters.Font
.Name = "Calibri"
.FontStyle = "Bold"
.Size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 32
End With
.Placement = xlFreeFloating
.PrintObject = False
End With
End Sub
Anyhow, it is good to avoid 'Selection' when possible...
Sub testButtonCharDif()
Dim bt As Button
Set bt = ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8)
With bt
.OnAction = "Sun"
.Characters.Text = "Sun"
With .Characters(Start:=1, length:=3).Font
.name = "Calibri"
.FontStyle = "Bold"
.size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 32
End With
.Placement = xlFreeFloating
.PrintObject = False
End With
End Sub
I am trying to create macro for an assignment which does the following:
a) Formats the active cell as follows: font type Arial, font size 14, bold font and horizontally centered
b) Deletes the entire row below the active cell
c) Deletes column A regardless of the location of the active cell
However, there is an error thrown when the macro is passed into the grader, which says "Object variable or with block not set". Please refer to the generated VBA below for the steps I took to solve this.
Any help/advice will be greatly appreciated!
Sub FormatCells()
'
' FormatCells Macro
'
'
Application.CutCopyMode = False
With Selection.Font
.Name = "Arial"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
With Selection.Font
.Name = "Arial"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Selection.Font.Bold = True
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
End Sub
I revised your code, removing the unnecessary parts. I hope it solved your problem.
Sub FormatCells()
'a) Formats the active cell as follows: font type Arial, font size 14, bold font and horizontally centered
With ActiveCell.Font
.Name = "Arial"
.Size = 14
.Bold = True
End With
ActiveCell.HorizontalAlignment = xlCenter
'b) Deletes the entire row below the active cell
ActiveCell.Offset(1, 0).EntireRow.Delete
'c) Deletes column A regardless of the location of the active cell
Columns("A:A").Delete
End Sub
If Selection.Font.Bold = False Then
Selection.Font.Bold = True
Else
Selection.Font.Bold = False
End If
This works if a range is selected.
How to create the same if a cell is in edit mode, and only a part of its content is selected ?
This is what you would use to format parts of a cell, but you can only do this after the edit is complete - like Tim Williams said.
With ActiveCell.Characters(Start:=3, Length:=2).Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With