VBA to check if last cell equals cell on different sheet - excel

I have a spreadsheet that is used to log information. In one sheet you enter the data then VBA updates a second sheet where the info is stored.
I want to write a code that will check if a cell on the last row of the log (which will be changing as it updates each time) equals a cell on the input page.
The aim is to stop someone updating the log twice, I already have an alert that it has been updated but want to put a foolproof system in place to stop an entry being logged twice.
I am new to VBA so do not really know where to start.

The below is an example of achieving this. I have added comments to explain what is happening in the VBA.
In Excel, press Alt+F11
In the project window on the left, expand it if its not already and
double click on 'ThisWorkbook'
If it is not already there, type Option Explicit as the first
thing in the main window. This means the code will not run unless
all variables that are used are declared, this is good practice
Past the below code into the window
With the cursor in the code you can press F8 to run it line by line
to see what is happening or F5 to run it in one go.
You will want to adjust it to your required workbooks, worksheets, and cells/columns.
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

Related

Set updated date on the active tab only

I have done some things in SWIFT but this is my first project in VBA.
How can I set a field on an active tab to be the modified date of the tab? I have tried to use a Sub Workbook_open () procedure but this is my first attempt at VBA and after several ours of looking, I am no closer.
Some more detail: This workbook has multiple users who are assigned a specific tab. As their manager I need to know the last time they accessed their tab so I wanted to have that entered programatically. I have a "Reference" tab that holds variables and I have an updated date cell (D2) that is referenced on the individual tabs for each user.
Here is what I have tried but the debugger doesn't get passed the first line :(
`Private Sub Workbook_Open()
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("Reference")
Dim Entry As Range: Set Entry = Sheets("Reference").Range("D2")
Range(D2).Value = Date
'Debug.Print MyRNG.Value
Entry.Value = Date
End Sub`
Thank you in advance.
You can use the Worksheet_Activate event in the sheet module. It fires when a worksheet is activated. You can write code that puts a date/time stamp into a cell somewhere in the workbook.
Remember, this code goes into the Sheet module, not a code module.
Private Sub Worksheet_Activate()
' this writes just the date into the current sheet's cell A1
[A1] = Date
' this writes the date and time into A1 on Sheet2
ThisWorkbook.Worksheets("Sheet2").Range("A1") = Now
End Sub
Be aware that this code fires when ANYONE activates the sheet. If you want to log activities of specific people only, then you need to add more refinement.

In This Excel VBA Could some body tells me Why it refuses to take after parameter in the add method in worksheets object?

The code is:
Sub Copy_Filtered_Table()
Dim aSheet As Worksheet
Dim i As Long
i = ActiveSheet.Index
ActiveSheet.AutoFilter.Range.Copy
-> Set aSheet = ActiveWorkbook.Worksheets.Add(After:=i)
aSheet.Range("A1").PasteSpecial
End Sub
The workbook format is .xlsm Excel 2016
it has sheets after and before the active sheet
Also I Have tried doing it with out the aSheet variable like this
ActiveWorkbook.Worksheets.Add After:=i
It did not work too. both cases gives error 1004 Method 'Add' of object 'Sheet' faild.
If I ommited the After parameter it works but putting the result new sheet before the active sheet which exactly I am avoiding.
In order to insert relative to an existing sheet, you need to provide the actual sheet rather than its index, using one of:
Set aSheet = ActiveWorkbook.Sheets.Add(After:=ActiveWorkbook.Worksheets(i))
ActiveWorkbook.Sheets.Add After:=ActiveWorkbook.Worksheets(i)
Which one you choose depends on whether you immediately want to do something with the sheet without searching for it again but I suspect you already know that, given your question.
As an aside (though I haven't tried), I suspect getting the index of the current sheets then turning that back into the current sheet object is a bit unnecessary. You may want to just try:
Set aSheet = ActiveWorkbook.Sheets.Add(After:=ActiveWorkbook.ActiveSheet)

Naming a Cell in Excel

I'm trying to create a series of macros to audit some financial models.
The first macro I’m trying to create is one that names the current cell. Why? I want to name the cell, after that I’m going to record a macro to click the “Trace Precedents” and go to the cell that has the relationship.
After that I need to go back to the original cell, thats the named one. That's easy on the go function, but I need to the naming macro working
My recorded code for the naming macro is as follows:
Sub Namer ()
ActiveWorkbook.Names.Add Name:="Name1", RefersToR1C1:="=Workings!R42C6"
ActiveWorkbook.Names("Name1").Comment = ""
End Sub
I have the following problems:
I need to name the current cell on a workbook with a lot of sheets. I’m gonna be moving between sheets but my recorded code has a “fixed” sheet.
How can I fix that? Name the current cell on the current sheet
Something like this should help you ...
Public Sub CreatesNames()
Dim objSheet As Worksheet
For Each objSheet In ThisWorkbook.Worksheets
objSheet.Names.Add Name:="Name1", RefersTo:=objSheet.Range("A1")
Next
End Sub
... something to note, names can be created at the worksheet level or at the workbook level, so, if you're going to be creating the same name across worksheets then you need to use a Worksheet object, not the Workbook object.
So to use the active cell ...
Sheet Level
ActiveSheet.Names.Add Name:="Name1", RefersTo:=ActiveCell
Workbook Level
ActiveWorkbook.Names.Add Name:="Name1", RefersTo:=ActiveCell
I hope that helps.

Dynamically populate Combobox with colum content - time error 424

I'm very new to DBA so probably it's a banal mistake but I looked around and I did not found anything that could help me.
I'm trying to populate a combobox dynamically using the content of a column (column "A" in this specific case) using a macro linked to a button. If the analyzed cells are empty everything goes smoothly and the message "done!" appears, but if there is any data in the cells I get the error "424 object required access".
I don't know if it would help: I took the code from this youtube video https://www.youtube.com/watch?v=x8O59GtatH8 and adapted it (just removed the listox) the complete code is at 5.35
I'm probably misunderstanding something very basic. I am guessing the declaration of the combobox.
Sub prova_stessa_scheda()
row_review = 1
Dim TheSheet As Worksheet
Set TheSheet = Sheets("Listino_prezzi")
Do
DoEvents
row_review = row_revieew + 1
item_in_review = TheSheet.Range("A" & row_review)
If Len(item_in_review) > 0 Then ComboProva_Change.AddItem (item_in_review) 'this is the command that gives the error
Loop Until item_in_review = ""
MsgBox "Done!"
End Sub
I expected that when the macro gets triggered the combobox gets filled with the value written in the cells of column "A" instead I got the error 424.
If you put your code into the Worksheet module of the sheet where the combobox is placed, VBA is assuming that you want to access the element CombProva of that sheet (this is what is done in the video, only with different names).
However, if you put your code into a regular module, VBA does not know what CombProva is. You have to tell VBA that you want to access it from a specific sheet. There are several ways to do so:
(1) Use
With ThisWorkbook.Sheets("Listino_prezzi")
.ComboProva.AddItem (item_in_review)
End With
Note that the following code will throw an compiler error. This is because TheSheet is of type Worksheet, so it could be any worksheet, and a worksheet does not have anything named ComboProva.
Dim TheSheet As Worksheet
Set TheSheet = Sheets("Listino_prezzi")
With TheSheet
.ComboProva.AddItem (item_in_review) ' <-- Compiler error
End With
(2) You can access the sheet also by its CodeName. If you look to the video: The sheet itself was renamed to Admin Site, but the CodeName is still Sheet1 (the CodeName can only be changed in the VBA editor in the Property-window). So you can write
With Sheet1
.ComboProva.AddItem (item_in_review)
End With
(3) You can access the combobox by name from the Shapes-collection of the sheet (basically everything that is put on a sheet but not within an cell is a Shape). However, as you are dealing with ActiveX-controls, this is a little bit ugly.
Dim sh as Shape
Set sh = TheSheet.Shapes("ComboProva")
With sh.DrawingObject.Object
Call .AddItem("Variante x")
End With

I want to read data from two excel sheets to one excel sheet with button using VBA?

enter image description hereI want to read data from two sheets whereas user will enter only date and after clicking button data will be populated in sheet.
Title Date
Enter Week Start Date "7/11/2016" (Button)
Name Project-ID Project Name Project Start Date Project End Date Sum Sum * 20
This is actual format of requirement.
in your line
Set objsheet = bjExcel.ActiveWorkbook.Worksheets("Config_InputAllocation_Weekly")
Set objsheet2 = objExcel.ActiveWorkbook.Worksheets("Config_Project")
you try to set both sheets active, which is not possible. There only be one active sheet. But you don't have to set them active. Use the following technique:
Dim objExcel as Object
Set objExcel = CreateObject("Excel.Application")
Dim myWb as workbook, myws1 as worksheet, myWs2 as worksheet
Set myWb = objExcel.Workbooks.Open("C:\Users\ABC\Documents\NSL\Automation Macro\NSL_DM_Tracker.xlsm")
Set myWs1 = myWb.Worksheets("Config_InputAllocation_Weekly")
Set myWs2 = myWb.Worksheets("Config_Project")
Then you can easily call the data from both sheets whether they are active or not:
Msgbox myWs1.Cells(1,1).value
Msgbox myWs2.Cells(1,1).value
Then the other problem is that you have defined an Sub searchdata() but it is not called within your CommandButton1_Click() Sub.
Moreover use Option Explicit setting, that is, put this line at the very top of your code:
Option Explicit
This will help you to debug your code.
Last but not least, in your Sub searchdata() no Worksheets are defined. Thus it will not know which one (myWs1 or myWs2) to use and when.
Good Luck,
Viktor

Resources