尝试使用Excel VBA从列A中获取值

Sub iterateThroughAll() ScreenUpdating = False Dim wks As Worksheet Set wks = ActiveSheet Dim rowRange As Range Dim colRange As Range Dim LastCol As Long Dim LastRow As Long LastRow = wks.Cells(wks.Rows.Count, "A").End(xlUp).Row Set rowRange = wks.Range("A1:A" & LastRow) 'Loop through each row For Each rrow In rowRange 'Find Last column in current row LastCol = 1 'wks.Cells(rrow, wks.Columns.Count).End(xlToLeft).Column Set colRange = wks.Range(wks.Cells(rrow, 1), wks.Cells(rrow, LastCol)) <-------------- I get a Run-time error 1004 Application defined or object defined error. 'Loop through all cells in row up to last col For Each cell In colRange 'Do something to each cell Debug.Print (cell.Value) Next cell Next rrow ScreenUpdating = True End Sub 

我得到一个应用程序定义或对象定义的错误。 代码看起来不错,但不知道为什么它不在这里工作。

我正在尝试将列A中的所有使用的单元格

 Option Explicit Sub iterateThroughAll() Application.ScreenUpdating = False Dim wks As Worksheet Set wks = ActiveSheet Dim rowRange As Range, rrow As Range Dim colRange As Range, Cell As Range Dim LastCol As Long Dim LastRow As Long LastRow = wks.Cells(wks.Rows.Count, "A").End(xlUp).Row Set rowRange = wks.Range("A1:A" & LastRow) 'Loop through each row For Each rrow In rowRange 'Find Last column in current row LastCol = 1 'wks.Cells(rrow, wks.Columns.Count).End(xlToLeft).Column Set colRange = wks.Range(wks.Cells(rrow.Row, 1), wks.Cells(rrow.Row, LastCol)) 'Loop through all cells in row up to last col For Each Cell In colRange 'Do something to each cell Debug.Print (Cell.Value) Next Cell Next rrow Application.ScreenUpdating = True End Sub