[英]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.