使用VBA,如何在列中选择单元格,直到我按下设置字符串

时间:2016-09-15 15:45:57

标签: excel vba excel-vba

我是VBA的新手,我希望编写一个动态VBA宏来在指定的列中启动,并逐步突出显示该列中的每个单元格,直到我到达包含单词“NULL”的单元格。但我不想选择包含“NULL”的单元格。

是否有任何人可以提供简单的VBA脚本来执行此操作?

让我们说我的数据是在第1天:

ColumnHeading
123   
NULL

然后在第2天:

ColumnHeading
123
13
16
NULL

3 个答案:

答案 0 :(得分:0)

下列任何一项都应该有效。如果你在专栏中多次“空”,你需要稍微调整一下,正如我所提到的那样。

Sub t2()
Dim nullCell As Range
Set nullCell = Columns("A").Find(what:="Null")
Debug.Print nullCell.Address

'If "Null" can appear many times, and the cell with the highest row number is the one you want, use this (uncomment out to use):
'Set nullCell = Columns("A").Find(what:="Null", Searchdirection:=xlPrevious)
'Debug.Print nullCell.Address

End Sub

(假设你的专栏A有列表)

答案 1 :(得分:0)

您可以将此编码放在工作簿开放事件中

dim I as integer
I=sheet1.range("A").end(xldown).row

sheet1.range("A"&i+1).value="NULL"

答案 2 :(得分:0)

下面的代码将遍历所有带有数据的列,每列将搜索找到“ NULL ”的行,然后为当前列设置Rng。 p>

Option Explicit

Sub FindNull()

Dim LastColumn          As Long
Dim Col                 As Long
Dim NullCell            As Range
Dim Rng                 As Range

' modify to your sheet name
With Worksheets("Sheet1")

    ' let's say your data starts from row 2
    LastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column

    ' starting from Col "A" >> modify to your start Column
    For Col = 1 To LastColumn
        Set NullCell = Columns(Col).Find(what:="Null")

        ' for debug : print address of "NULL" and Row (per Column)
        Debug.Print NullCell.Address & " ; " & NullCell.Row

        ' set Rng for Curren Column
        Set Rng = Range(Cells(2, Col), Cells(NullCell.Row - 1, Col))

        ' for debug : print address current's column's Range
        Debug.Print Rng.Address

        ' do your stuff with the Rng found per column

    Next Col

End With

End Sub
相关问题