我有一个用于记录信息的电子表格。在一个工作表中输入数据,然后VBA更新存储信息的第二个工作表。
我想编写一个代码,用于检查日志最后一行(每次更新时是否会更改)上的单元格是否等于输入页面上的单元格。
目的是阻止某人两次更新日志,我已经有一个警告,它已经更新但是想要建立一个万无一失的系统来阻止一个条目被记录两次。
我是VBA的新手,所以不知道从哪里开始。
答案 0 :(得分:1)
以下是实现这一目标的一个例子。我添加了评论来解释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