Печать значений ячеек строки в Excel - PullRequest
1 голос
/ 02 декабря 2010

Я хотел бы напечатать значения в строке 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 ]

2 голосов
/ 02 декабря 2010

Я думаю, что делать 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 голос
/ 02 декабря 2010

Я не уверен, чего вы пытаетесь достичь, но этот код печатает текущую строку, пока не будет найдена пустая ячейка

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
0 голосов
/ 03 декабря 2010

Я использовал смешанный подход, ">" говорит мне, если строка грязная (была отредактирована)

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
...