抵消 lastrow 的问题
Issue to offset lastrow
我无法从上一行偏移我的最后一行。事实上,我尝试 运行 下面的代码,我收到了
出现的错误消息
compile error: invalid qualifier…
问题显然来自我的变量lastRow4 = lastRow3.Offset(-1, 0)
当我删除我的这行代码并在我的 VBA 代码中将倒数第二行 Range("A3:F" & lastRow4).Select
替换为 Range("A3:F" & lastRow3).Select
时,我的代码有效但选择选择了我的范围直到 lastrow
(在我的代码中,它是 lastRow3
),这不是我想要的。
如果有人知道如何解决这个问题以使我的变量 lastRow4 不会 return 我收到任何错误消息,那就太好了。
非常感谢。
哈维
Sub gdfgdhgf()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer, lastRow3 As Integer, lastRow4 As Integer
Dim currentRowValue As String
sourceCol = 5 'column F has a value of 5
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
lastRow3 = currentRow
End If
Next
lastRow4 = lastRow3.Offset(-1, 0)
Range("A3:F" & lastRow4).Select
End Sub
以下不是代码。这是一个例子。修改并尝试:
Option Explicit
Sub test()
Dim LastRow As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Get the last row of column A sheet1
'In order to refer to line before Lastrow you can use:
.Range ("A" & LastRow - 1)
End With
End Sub
试试下面的代码,代码注释里面的解释:
Option Explicit
Sub gdfgdhgf()
Dim sourceCol As Long, rowCount As Long, currentRow As Long, lastRow3 As Long, lastRow4 As Long
Dim CurrentRng As Range
Dim currentRowValue As String
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Sheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
sourceCol = 5 'column F has a value of 5
With Sht
rowCount = .Cells(.Rows.Count, sourceCol).End(xlUp).Row ' for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
' I think you are trying to set it as a range
Set CurrentRng = .Cells(currentRow, sourceCol)
If IsEmpty(CurrentRng) Or CurrentRng.Value2 = "" Then
lastRow3 = CurrentRng.Row ' get the row of the CurrentRng.Row
End If
Next
lastRow4 = lastRow3 - 1 ' just subtract 1, no need to use Offset
.Range("A3:F" & lastRow4).Select ' <-- Not sure why you need to Select
End With
End Sub
我无法从上一行偏移我的最后一行。事实上,我尝试 运行 下面的代码,我收到了
出现的错误消息compile error: invalid qualifier…
问题显然来自我的变量lastRow4 = lastRow3.Offset(-1, 0)
当我删除我的这行代码并在我的 VBA 代码中将倒数第二行 Range("A3:F" & lastRow4).Select
替换为 Range("A3:F" & lastRow3).Select
时,我的代码有效但选择选择了我的范围直到 lastrow
(在我的代码中,它是 lastRow3
),这不是我想要的。
如果有人知道如何解决这个问题以使我的变量 lastRow4 不会 return 我收到任何错误消息,那就太好了。
非常感谢。 哈维
Sub gdfgdhgf()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer, lastRow3 As Integer, lastRow4 As Integer
Dim currentRowValue As String
sourceCol = 5 'column F has a value of 5
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
lastRow3 = currentRow
End If
Next
lastRow4 = lastRow3.Offset(-1, 0)
Range("A3:F" & lastRow4).Select
End Sub
以下不是代码。这是一个例子。修改并尝试:
Option Explicit
Sub test()
Dim LastRow As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Get the last row of column A sheet1
'In order to refer to line before Lastrow you can use:
.Range ("A" & LastRow - 1)
End With
End Sub
试试下面的代码,代码注释里面的解释:
Option Explicit
Sub gdfgdhgf()
Dim sourceCol As Long, rowCount As Long, currentRow As Long, lastRow3 As Long, lastRow4 As Long
Dim CurrentRng As Range
Dim currentRowValue As String
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Sheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
sourceCol = 5 'column F has a value of 5
With Sht
rowCount = .Cells(.Rows.Count, sourceCol).End(xlUp).Row ' for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
' I think you are trying to set it as a range
Set CurrentRng = .Cells(currentRow, sourceCol)
If IsEmpty(CurrentRng) Or CurrentRng.Value2 = "" Then
lastRow3 = CurrentRng.Row ' get the row of the CurrentRng.Row
End If
Next
lastRow4 = lastRow3 - 1 ' just subtract 1, no need to use Offset
.Range("A3:F" & lastRow4).Select ' <-- Not sure why you need to Select
End With
End Sub