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
Related
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
I've been working on a Macro in Excel that should go through every sheet in the workbook, count the number of rows in a given sheet, and then format those rows. The other day I was able to run it successfully, with the macro formatting the entire workbook, however the next time I attempted to run it, the value for the number of rows did not update, and it only formatted the rest of the sheets up to the number of rows in the first sheet (i.e. if the first sheet is 22 rows long, it will format every sheet, but only the first 22 rows of that sheet, leaving the rest unformatted). I have attempted trying some changes to the macro, but cannot figure out how to resolve the issue so that the row counter resets for each sheet it loops through. Any help in trying to get this macro working is appreciated.
The macro as I currently have it written is as follows:
Sub Formatting()
'
' Formatting Macro
'
' Keyboard Shortcut: Ctrl+Shift+F
'
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
lr = Cells(Rows.Count, "A").End(xlUp).Row
Range("$A$1:$X$" & lr).Select
With Selection.Font
.Name = "Century"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("G:G").Select
Selection.ColumnWidth = 75
With Selection
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Cells.EntireRow.AutoFit
Range("A1").Select
End With
Next ws
End Sub
For the sake of making your formatting subroutine more specific, I have pulled out the formatting itself to private subroutines, so you can see what you're working with. You format a designated range and its font, then separately format column G; both of which add bulk to your base functions and are repeated.
Beyond that, I have removed the Select items, aside from the "A1" select to reset position on each sheet.
Imparting my comments on your code (untested):
Sub Formatting()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
Dim lr as Long: lr = .Cells(.Rows.Count, "A").End(xlUp).Row
DesignatedRangeFormatting .Range("$A$1:$X$" & lr)
ColumnGFormatting .Columns("G")
.Cells.EntireRow.AutoFit
.Select
.Range("A1").Select
End With
Next ws
End Sub
Private Sub DesignatedRangeFormatting(rng as Range)
With rng.Font
.Name = "Century"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
With rng
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
Private Sub ColumnGFormatting(rng as Range)
With rng
.ColumnWidth = 75
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
Edit1: Added .Select before .Range("A1").Select to ensure the current sheet is selected, which resolves the RTE1004.
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
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