繁体   English   中英

如何在VBA中使用Instr查找单元格

[英]How to use Instr in VBA to find a cell

我正在尝试编写一个宏,它会告诉我工作表中术语“Rep 1”“Rep 2”的位置

我是 VBA 的新手,我想在实习时写这个。 我曾尝试使用 get 函数,但它不起作用。 我找到了 Instr 函数,并且很好奇如何在我的代码中实现它。

Sub Value()
    Dim rCell As Range
    Dim Char As Integer
    For Each rCell In Selection
    CharCount = Len(rCell)
    Char = InStr(1, rCell, "(")
    rCell.Characters(1, Char - 1).Font.Bold = True
    Next rCell
End Sub

这只是我想做的一个例子。 我正在使用的数据

使用查找功能

使用通配符 *,此代码将查找所有以 Rep 开头的值(因此它涵盖了 Rep1 和 Rep2)。 将所有匹配项设置为粗体。

Set Results = ActiveSheet.Cells.Find("Rep*", LookIn:=xlValues, MatchCase:=False)
If Not Results Is Nothing Then
   firstAddress = Results.Address
   Do
        Results.Font.Bold = True
        Set Results = ActiveSheet.Cells.FindNext(After:=Results)
    Loop While Not Results Is Nothing And Results.Address <> firstAddress
End If

循环遍历选定的单元格并将其中包含“Rep 1”的单元格“加粗”是一种可能的解决方案:

在此处输入图片说明

Sub BoldValue()

    Dim myCell As Range
    Dim char As Long

    For Each myCell In Selection
        If InStr(1, myCell, "Rep 1") > 0 Then
            myCell.Font.Bold = True
        End If
    Next

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM