Printing row cell values in Excel
I would like to print the values in an Excel Row. I can get to the row and select it, but how do i loop through the cells? Or is there a row object that i can read?
Range("A1").End(xlUp).Offset(1, 0).Select
Do Until IsEmpty(ActiveCell)
r = Rows(ActiveCell.开发者_JAVA技巧Row).EntireRow.Select
For Each cell In r.Cells
Debug.Print cell.Value
Next cell
Loop
I think that doing Do Until
IsEmpty(ActiveCell)` isn't a good idea:
you could have some empty cells followed by non-empty cells.
How does this code work for you?
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
I'm not sure what you are trying to achieve, but this code prints the current row until an empty cell is found
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
Edit
I think this is what you are looking for:
Sub a()
Dim TheArray As Variant
TheArray = Range("A4:E4").Value
Debug.Print TheArray(1, 4)
End Sub
I used a mixed approach, the ">" tells me if the row is dirty (has been edited)
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
精彩评论