从一行中查找匹配值 Excel 的单元格

Find match value Excel cell from a row

这是一个 Excel 问题。 我有一个具有以下值的 Excel 行:

单元格值 1, 2, x, ,1=,2=, ,x=,2
单元格地址 a1,b1,c1,d1,e1,f1,g1,h1,i1

我想从上面的行中获取所有非空单元格地址。

即
a1,b1,c1,e1,f1,h1,i1

是否可以使用 vba/vbs 来完成这项工作?

非常感谢

您可以使用.SpecialCells(xlCellTypeConstants)

为了演示,运行 这个,并立即观察结果 window

Sub demo()
    Dim rng As Range, rNonEmpty As Range
    Set rng = [A1:I1]
    Set rNonEmpty = rng.SpecialCells(xlCellTypeConstants)

    Debug.Print rNonEmpty.Address
End Sub

以下代码将检查 10 行乘 1000 列的区域,最后将显示单元格 A20 中非空白单元格的地址:

Sub no_blank_cells()
   Dim wks As Worksheet
   Set wks = ActiveSheet
   m = ""
   maxrows = 10
   maxcolumns = 1000
   For i = 1 To maxrows
       For j = 1 To maxcolumns
           a = Cells(i, j)
           If a <> "" Then
               m = m & Cells(i, j).Address(RowAbsolute:=False, ColumnAbsolute:=False) & ", "
           End If
       Next j
   Next i
   msg = MsgBox(m, vbInformation)
   wks.Cells(20, 1) = m
End Sub

如果要改变搜索区域,修改变量maxrowsmaxcolumns的值。

Sub WriteNonNulValue()
    '/*SELECT SHEET TO ANALIZE*/
    Worksheets("Sheet1").Select
    '/*SELECT ROW TO READ*/
    Row_to_read = 1
    '/*SELECT ROW IN WHICH WRITE*/
    Row_to_write = 2
    '/*NUMBER OF COLUMNS TO ANALIZE*/
    Columns_to_analize = 11
    '/*COUNTER WRITE*/
    Columns_to_write = 1
    For i = 1 To Columns_to_analize
        If Trim(Cells(Row_to_read, i)) <> "" Then
            '/*WRITE ADDRESS NO EMPTY CELLS*/
            Cells(Row_to_write, Columns_to_write) = Cells(Row_to_read,i).Address
            '/*INCREMENT COUNTER WRITE*/
            Columns_to_write = Columns_to_write + 1
        End If
    Next i
End Sub