vba 在 Excel 中打印行单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4338189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing row cell values in Excel
提问by Lorenzo
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?
我想在 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
回答by mechanical_meat
I think that doing Do UntilIsEmpty(ActiveCell)` isn't a good idea:
you could have some empty cells followed by non-empty cells.
我认为做Do UntilIsEmpty(ActiveCell)` 不是一个好主意:
你可以有一些空单元格,然后是非空单元格。
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
回答by Dr. belisarius
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
回答by Lorenzo
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

