在Excel中打印行单元格值

时间:2010-12-02 17:58:12

标签: vba excel-vba excel-2007 excel

我想在Excel行中打印值。我可以到达行并选择它,但我如何循环遍历细胞?或者是否有我可以阅读的行对象?

Range("A1").End(xlUp).Offset(1, 0).Select

    Do Until IsEmpty(ActiveCell)

        r = Rows(ActiveCell.Row).EntireRow.Select

        For Each cell In r.Cells
            Debug.Print cell.Value
        Next cell

    Loop

3 个答案:

答案 0 :(得分:2)

我认为做Do Until IsEmpty(ActiveCell)`并不是一个好主意:
你可以有一些空单元格,然后是非空单元格。

此代码如何为您服务?

Sub print_some_values()

    Dim c As Long
    Dim r As Long
    Dim max_col As Long

    r = ActiveCell.Row
    max_col = ActiveSheet.UsedRange.Columns.Count

    For c = 1 To ActiveSheet.UsedRange.Columns.Count
       Debug.Print ActiveSheet.Cells(r, c).Value
    Next c

End Sub

答案 1 :(得分:1)

我不确定您要实现的目标,但此代码会打印当前行,直到找到空单元格

Sub a()

Dim r As Range
Dim c As Range

    Set r = Rows(ActiveCell.Row)
    For Each c In r.Cells
        If (IsEmpty(c)) Then Exit For
        Debug.Print c.Value
    Next c

修改

我认为这就是你要找的东西:

Sub a()

Dim TheArray As Variant

TheArray = Range("A4:E4").Value
Debug.Print TheArray(1, 4)

End Sub

答案 2 :(得分:0)

我使用混合方法,“>”告诉我行是否脏(已编辑)

Dim r As Range
Dim c As Range
Dim max_col As Long

max_col = ActiveSheet.UsedRange.Columns.Count

Range("A1").End(xlUp).Offset(1, 0).Select

Do Until IsEmpty(ActiveCell)
    Debug.Print ActiveCell.Value

    If ActiveCell.Value = ">" Then
        Set r = Rows(ActiveCell.Row)

        For Each c In r.Cells
            'print the value of each cell in the row
            If c.Column = 1 Then
                'skip
            ElseIf c.Column <= max_col Then
                Debug.Print c.Value
            Else
                Exit For
            End If
        Next c
    End If

    ActiveCell.Offset(1, 0).Select

Loop
相关问题