如何在Excel 2013/2016中使用VBA宏来选择给定范围内的所有粗体单元格
- 2021-11-12
- 来源/作者: 菜鸟图库/ 菜鸟图库
- 482 次浏览
本文将讲述如何在Excel中选定区域中选中所有粗体字的单元格。如何在Microsoft Excel 2013/2016中使用VBA宏来选择给定范围内的所有粗体单元格。
- 使用查找和替换选择粗体单元格
- 使用VBA选择粗体单元格
假设您有一个数据区域为B1:C5的数据列表,你想选择该区域中所有具有粗体字体格式的所有单元格。那么我们该怎么办呢?这篇文章将通过下面的两种方法来选择指定区域中的所有粗体单元格。
方法1:使用查找和替换选择粗体单元格
如果要选择具有粗体格式的所有单元格,可以在工作表中使用“查找和替换”功能来查找特定文本或特定格式的单元格,例如:单元格颜色,粗体字体等。我们只需执行以下步骤即可查找并选择所选单元格区域中的所有粗体格式单元格:
步骤1:选择包含要查找的粗体单元格的范围。
步骤2:选中“开始”选项卡,单击“编辑“组下的“查找和选择”命令。然后从“查找和选择”下拉列表中单击“查找”。 “查找和替换”对话框将打开。
步骤3:单击“查找和替换”对话框中的“选项”按钮。
步骤4:单击“查找内容”区域中的“格式”按钮,然后从“格式”下拉列表中选择“格式”。并且将打开“查找格式”对话框。
步骤5:单击“查找格式”对话框中的“字体”选项卡,在“字形”列表框中选择“加粗”选项,然后单击“确定”按钮返回“查找和替换”对话框。
步骤6:单击“全部查找”按钮,然后将搜索所选单元格区域中所有的具有粗体字体格式的单元格。
步骤7:按键盘上的Ctrl + C键选择所有结果,然后所有具有粗体格式文本的单元格将会被选中。
方法2:使用VBA选择粗体单元格
您还可以使用Excel VBA宏来在给定单元格范围内选中所有粗体单元格。只需执行以下步骤:
步骤1:打开您的Excel工作簿,然后单击开发工具选项卡下的“Visual Basic”命令,或者只需按“ALT + F11”快捷方式。
步骤2:然后将出现“Visual Basic编辑器”窗口。
步骤3:单击“插入” – >“模块”以创建新模块。
步骤4:将以下VBA代码粘贴到代码窗口中。然后单击“保存”按钮。
Sub SelectOnlyBoldCells()
Dim myRange As Range
Dim cell As Range
Dim tempRange As Range
Set myRange = Range("A1", "D3")
For Each cell In myRange
If cell.Font.Bold = True Then
If tempRange Is Nothing Then
Set tempRange = cell
Else
Set tempRange = Union(tempRange, cell)
End If
End If
Next cell
If Not tempRange Is Nothing Then
tempRange.Select
End If
End Sub
- Sub SelectOnlyBoldCells()
- Dim myRange As Range
- Dim cell As Range
- Dim tempRange As Range
- Set myRange = Range("A1", "D3")
- For Each cell In myRange
- If cell.Font.Bold = True Then
- If tempRange Is Nothing Then
- Set tempRange = cell
- Else
- Set tempRange = Union(tempRange, cell)
- End If
- End If
- Next cell
- If Not tempRange Is Nothing Then
- tempRange.Select
- End If
- End Sub
Sub SelectOnlyBoldCells()
Dim myRange As Range
Dim cell As Range
Dim tempRange As Range
Set myRange = Range("A1", "D3")
For Each cell In myRange
If cell.Font.Bold = True Then
If tempRange Is Nothing Then
Set tempRange = cell
Else
Set tempRange = Union(tempRange, cell)
End If
End If
Next cell
If Not tempRange Is Nothing Then
tempRange.Select
End If
End Sub
注意:此VBA代码将使用另一个名为tempRange的范围,该范围接收具有粗体格式的单元格引用。 UNION函数用于将地址连接在一起。完成该过程后,将选择范围tempRange。您需要根据自己的需要更改myRange变量的值。
步骤5:返回当前工作表,然后运行上面的excel宏。点击执行按钮。
步骤6:让我们看看结果:
标签(TAG) excel教程