Excel 在最后一个单元格中查找值并将数据与运行宏进行比较
*编辑 这就是最终起作用的原因。插入新行时,以下解决方案不运行AddProjExcel 在最后一个单元格中查找值并将数据与运行宏进行比较,excel,vba,Excel,Vba,*编辑 这就是最终起作用的原因。插入新行时,以下解决方案不运行AddProj Sub Worksheet_Calculate() Dim X As Range Set X = LastCell 'The X is superflous, you could just use the LastCell variable If Sheet5.Range("A" & Rows.Count).Value < X.Value Then X.Value = Me.Ra
Sub Worksheet_Calculate()
Dim X As Range
Set X = LastCell 'The X is superflous, you could just use the LastCell variable
If Sheet5.Range("A" & Rows.Count).Value < X.Value Then
X.Value = Me.Range("A" & Rows.Count).Value
AddProj
End If
End Sub
我正在尝试读取列的最后一个单元格中的数据。
“X”的值应为最后一个单元格的值。
然后,我希望将“X”与行数进行比较,如果行数小于“X”,则执行宏“AddProj”。
一旦“X”和列A是相同的值,就不需要做其他事情
出于某种原因,它不起作用。
此代码位于我希望进行比较的工作表上。
请参阅下面我的代码:
Private Sub Worksheet_Calculate()
X = LastCell
If Sheet5.Range("A" & Rows.Count).Value < Sheet5.Range("X").Value Then
Sheet5.Range("X").Value = Me.Range("A" & Rows.Count).Value
AddProj
End If
End Sub
Sub LastCell()
Range("A1").End(xlDown).Select
End Sub
提前感谢。试试这个:
Sub Worksheet_Calculate()
Dim lRow As Long
lRow = Sheet5.Cells(Sheet5.Rows.Count, 1).End(xlUp).Row
If Sheet5.Cells(lRow, 1) > lRow Then
Sheet5.Cells(lRow, 1) = lRow
AddProj
End If
End Sub
X是一个变量,但您将其称为
“X”
。也要避免使用。选择,因为它不是必需的,即使在这种情况下也不做任何事情,因为首先子不能返回值,第二个。选择也没有返回值。计算最后一行的最佳方法是:Sheet5.Cells(Sheet5.Rows.Count,1)。End(xlUp)。row
这里是关于UPGs的一个很好的答案
Dim lRow As Long
lRow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
If lRow >= Sheet1.Cells(lRow, 1) Then
Exit Sub
Else: AddProj
End If
您的两个解决方案都没有实际运行AddProj将模板粘贴到Sheet1中。请看上面我所做的。根据您的描述,我用模拟数据测试了上面的内容;i、 e.如果行数为,请检查此SO问题,它将帮助您理解工作表\u计算
Sub Worksheet_Calculate()
Dim lRow As Long
lRow = Sheet5.Cells(Sheet5.Rows.Count, 1).End(xlUp).Row
If Sheet5.Cells(lRow, 1) > lRow Then
Sheet5.Cells(lRow, 1) = lRow
AddProj
End If
End Sub
Dim lRow As Long
lRow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
If lRow >= Sheet1.Cells(lRow, 1) Then
Exit Sub
Else: AddProj
End If