[英]VBA to check if last cell equals cell on different sheet
我有一个用于记录信息的电子表格。 在一张工作表中,输入数据,然后VBA更新第二张工作表,以存储信息。
我想编写一个代码来检查日志最后一行中的单元格(每次更新时都会更改)是否等于输入页面上的单元格。
目的是阻止某人两次更新日志,我已经有一个警报,说它已被更新,但是想安装一个万无一失的系统来阻止两次记录一个条目。
我是VBA的新手,所以真的不知道从哪里开始。
以下是实现此目的的示例。 我添加了注释以解释VBA中发生的情况。
Option Explicit
。 这意味着除非声明所有使用的变量,否则代码将不会运行,这是一个好习惯 您将需要将其调整为所需的工作簿,工作表和单元格/列。
Public Sub Sample()
'Clearly declare variables, in the case we are using them
'to reference a workbook and a worksheet
Dim WkBk As Workbook
Dim WkSht As Worksheet
'Set the WkBk variable (which was declared as a workbook,
'which means it can only be used for workbook objects.
'I this instance we are refering to ThisWorkbook,
'which is as it sounds.
Set WkBk = ThisWorkbook
'We can now make a reference to a specific worksheet
'within our referenced workbook
Set WkSht = WkBk.Worksheets("Sheet2")
'This IF statement is comparing the value of cell
'A1 on Sheet1 to the the value of the last populated cell
'in column A of Sheet2 (the sheet we created a reference to)
If WkBk.Worksheets("Sheet1").Range("A1") = WkSht.Range("A" & WkSht.Rows.Count).End(xlUp) Then
MsgBox "It was a match"
End If
Set WkSht = Nothing
Set WkBk = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.