在列中使用列号而不是A1?

我有这个子行在A列,A1到数据的最后一行,并将这些单元格中的值更改为“文本”:

Sub Sample() Dim lastRow As Long lastRow = Range("A" & Rows.Count).End(xlUp).Row Range("A1:A" & lastRow).Value = "text" End Sub 

是否有一种方法可以在Range中使用R1C1符号而不是A1符号来定义列?

我希望最终使用一个variables的列号,并通过几个列执行重复的行动,然后停在有数据的最后一列。

相反,最好是保留A1符号,因为在这里你可以指出我使用A1符号来遍历列的方法吗?

我很抱歉,如果这是一个头脑发热的问题,我已经search了几天之前张贴。 可惜初学者。 :)谢谢你,查克

@Siddarth,非常感谢,你的代码让我朝着正确的方向前进。 使用你的第二个子我发现它改变了所有单元格的值“文本”。 我有兴趣一次操纵一列,并修改代码只改变列A,你看到任何语法问题?

 Sub Sample2() Dim ws As Worksheet Dim rng As Range Dim LastCol As Long, LastRow As Long Set ws = ThisWorkbook.Sheets("Results") With ws LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row Set rng = .Range(.Cells(1), .Cells(LastRow, 1)) Debug.Print rng.Address rng.Value = "text" End With End Sub 

随后我命名了一个variables,我希望能够继续工作。

 Sub Sample2A() Dim ws As Worksheet Dim rng As Range Dim LastCol As Long, LastRow As Long Dim StarttCol As Integer StarttCol = 1 Set ws = ThisWorkbook.Sheets("Results") With ws LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row Set rng = .Range(.Cells(StarttCol), .Cells(LastRow, StarttCol)) Debug.Print rng.Address rng.Value = "text" End With End Sub 

您可以使用列名称/数字而不必使用RC符号。 例如

 Sub Sample() Dim ws As Worksheet Dim rng As Range Dim LastCol As Long, LastRow As Long Dim LastColumn As String Set ws = ThisWorkbook.Sheets("Results") With ws LastRow = .Range("A" & .Rows.Count).End(xlUp).Row LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column '~~> Return column name from number LastColumn = Split(.Cells(, LastCol).Address, "$")(1) Set rng = .Range("A1:" & LastColumn & LastRow) Debug.Print rng.Address rng.Value = "text" End With End Sub 

相同的代码也可以写成

 Sub Sample() Dim ws As Worksheet Dim rng As Range Dim LastCol As Long, LastRow As Long Set ws = ThisWorkbook.Sheets("Results") With ws LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row Set rng = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) Debug.Print rng.Address rng.Value = "text" End With End Sub 

只需使用单元格来指定范围。 例如:范围(A1)==范围(单元格(1,1))

Range(A1:A10)就是Range(Cells(1,1),Cells(1,10))