简体   繁体   English

Excel VBA选择多个单元格

[英]excel VBA selecting multiple cells

Good day, 美好的一天,

I'm looking for assistance in figuring out my problem. 我正在寻找解决我的问题的帮助。 I wanted to find a cell in Column A that contains the text "Grand Total". 我想在A列中找到一个包含文本“总计”的单元格。 Find is the keyword since it may sometimes entered in A14, or A12, or A20. 查找是关键字,因为有时可能会在A14,A12或A20中输入它。 When the text is found, I wanted to select that cell, and the next cell in column B. Let's say the text is found in A14, then select A14 and B14. 找到文本后,我想选择该单元格,然后选择B列中的下一个单元格。假设在A14中找到了文本,然后选择A14和B14。

In that way, I would like to continue and edit the format as font = bold, fill color, and font color (I will figure it out soon). 这样,我想继续并将格式编辑为font = bold,fill color和font color(我很快就会知道)。

I can't seem to find a code that will help me so I appreciate any help. 我似乎找不到能帮助我的代码,因此,我感谢任何帮助。 Below is a code I found but it seems to be not working FOR ME. 以下是我找到的代码,但似乎对我不起作用。 Credits for this code from this link: How to select a range of rows using two variables in VBA 此链接的代码来源: 如何在VBA中使用两个变量选择行范围

Dim Consultant1 As Integer, Consultant2 As Integer
Dim ConsultantRange As Range

Dim rngFind As Range
Set rngFind = Columns("A:A").Find(What:="Grand Total", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

If Not rngFind Is Nothing Then
    Consultant1 = rngFind.row + 1
End If

Set rngFind = Columns("A:A").Find(What:="Grand Total", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

If Not rngFind Is Nothing Then
    Consultant2 = rngFind.row - 1
End If

If Consultant1 > 0 And Consultant2 > 0 Then
    Set ConsultantRange = Range(Cells(Consultant1, 2), Cells(Consultant2, 2))
    With ConsultantRange.Selection.Font.Bold = True
    End With
End If

How about: 怎么样:

Sub qwerty()
    Dim rngFind As Range
    Set rngFind = Range("A:A").Find(What:="Grand Total", After:=Range("A1"))
    rngFind.Resize(1, 2).Select
End Sub

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

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