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

时间:2015-01-27 10:13:19

标签: excel excel-vba vbscript vba

这是一个Excel问题。 我有一行Excel行,其中包含以下值:

cell value      1, 2, x,  ,1=,2=,  ,x=,2
cell address   a1,b1,c1,d1,e1,f1,g1,h1,i1

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

i.e.
a1,b1,c1,e1,f1,h1,i1

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

非常感谢

3 个答案:

答案 0 :(得分:0)

您可以使用.SpecialCells(xlCellTypeConstants)

要演示,运行它,并在Imediate窗口中观察结果

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

    Debug.Print rNonEmpty.Address
End Sub

答案 1 :(得分:0)

以下代码将检查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的值。

答案 2 :(得分:0)

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