I got an error 424: Object Required.
It's a very simple script, I can't see where I'm going wrong.
The strange thing is, this worked many times before. I am only getting these errors today.
I rename sheet one as "All Data". Then add another sheet and rename it "List".
The error happens when I try to activate the "List" sheet. Object Required. I guess it can't detect the new sheet? Where am I going wrong? As I said this worked 100 times before.
Sheets(1).Select
Sheets(1).Name = "All Data"
Sheets.Add After:=ActiveSheet
Sheets(2).Select
Sheets(2).Name = "List"
List.Activate ' error happens here
I have also tried List.Select instead of Activate. Same error.
If List is not the VBA name of a sheet it has to be defined:
Dim List As Worksheet
Set List = ThisWorkbook.Worksheets("List")
or make sure the VBA name of that sheet is actually changed to List in the properties window of the VBA editor.
List.Activate activates a sheet which VBA name is List
Worksheets("List").Activate activates a sheet which tab name is List
note that these 2 naming systems are completely independent.
You might benefit from reading
How to avoid using Select in Excel VBA.
Worksheet(1).Name = "All Data"
Worksheet.Add(After:=Worksheet("All Data")).Name = "List"
Worksheet("List").Activate ' don't use activate! see link above
or
Worksheet(1).Name = "All Data"
Dim List As Worksheet
Set List = Worksheet.Add(After:=Worksheet("All Data"))
List.Name = "List"
List.Activate ' don't use activate! see link above
Don't mix Sheets with Worksheets they count differently. Sheets contains all types of sheets like worksheets, chart sheets, etc. but Worksheets only contains worksheets.
If you have 2 worksheets and 1 chart sheet:
Worksheets.Count is 2 but
Sheets.Count is 3.
Related
I am writing a script for a workbook that is to be used by multiple departments. The button click imports a weekly report to a master data worksheet, creates a new sheet/Tab, asks what to name that new worksheet (Which will be the week date the report references.), place an MS Query table onto the new sheet, filtered from the master, clears the master sheet and prepares the work book to repeat the same thing when the next weekly report gets added (...and is all working at the moment.). The next time someone updates the workbook, it will the button click to again use the same script to add yet another worksheet for the new week and so on, but, since I started from recorded macros to writing the rest of what I need, everything in the script is referencing the first new worksheet by worksheet name, Like this example:
Application.Goto Reference:="W11_9_2020"
Sheets("W11_9_2020").Select
Range("A1").Select
Here (Above) I would like this to be a generic reference to the latest sheet in the script, not the exact name so it is reusable the next x amount of times the workbook gets updated.
The next time someone updates this, it will still add a new sheet, but once it gets to the part where the query table is created and the new sheet is formatted, data added, blah blah, it's just going to repopulate the first sheet again since it used that sheet name in the script (Or error out).
I tried using:
Set Worksheets(Worksheets.Count) = wsDest1
wsDest1.Range("I1").Select
ActiveCell.FormulaR1C1 = "Week"
wsDest1.Range("J1").Select
ActiveCell.FormulaR1C1 = "Total Shipped"
wsDest1.Range("K1").Select
ActiveCell.FormulaR1C1 = "Total On Time"
wsDest1.Range("L1").Select
ActiveCell.FormulaR1C1 = "Total Late"
wsDest1.Range("L2").Select
..in the hopes of generically using a reference to the latest added worksheet (Which is always "Add after") but doing that brings an error stating that it is an illegal use of a procedure and highlights this line -->> Set Worksheets(Worksheets.Count) = wsDest1.
I guess in trying to write this for unlimited reuse, I would like the sheet references to be variables or something so it always references the latest added sheet before the button click would add another and repeat/rerun the same script for any given new update.
Is there another way to generically(?) identify the latest sheet/tab in the body of the script without the exact sheet name being identified?
Keep in mind that the method to add a new worksheet also returns a reference to that worksheet. So, create the reference when you create the worksheet, and then refer to it later.
Sub AddSheet()
Dim sheetName As String
Dim wsNew As Worksheet
'Get the sheet name, however you're currently doing it
sheetName = "NewSheet"
Set wsNew = Worksheets.Add()
wsNew.Name = sheetName
'Do the rest of the stuff
wsNew.Range("A1").Value = "Hello"
End Sub
I'm really new at VBA and have learned what I know so far from internet searches, but I cannot find a resolution to my issue.
I have two workbooks, one with information on all of my company's current projects and another with just the active projects. The Active Projects workbook is where we store all the documents that need reviewing for each project. Each project has it's own worksheet.
When I create a new worksheet in the Active Projects workbook, I would like to use a macro to fill in the relevant project information from the All Projects Workbook.
I have seen code that copies cells and ranges from one workbook to another, but they have the sheet names hard coded in. Like this:
'Copy range to in selected row to clipboard
Workbooks("All Project.xlsx").Worksheets("All Open").Range("B" & (ActiveCell.Row)).Copy
'PasteSpecial to paste values, formulas, formats, etc.
Workbooks("Active Projects.xlsm").Worksheets(InputBoxValue).Range("A2").PasteSpecial Paste:=xlPasteValues
I thought to use an input box to ask for the worksheet name where the copied data would be pasted, but after hours of research, I cannot find out to use the result of the input box for the worksheet name.
Thank you for furthering my VBA education
FYI - this is at high risk of someone putting in the wrong sheet name given the nature of free-form text. That is something you will need to handle on your end so I suggest you look up:
How to check if a sheet given name exists on a book
How to loop a InputBox until an acceptable input is given
Dim Sheet_Name As String
'Get Input
Sheet_Name = Application.InputBox("Enter Sheet Name", Type:=2)
'Use Input
MsgBox ThisWorkbook.Sheets(Sheet_Name).Name
Dim InputBoxValue As String
InputBoxValue = InputBox("Enter sheet name")
'PasteSpecial to paste values, formulas, formats, etc.
Workbooks("Active Projects.xlsm").Worksheets(InputBoxValue).Range("A2").PasteSpecial Paste:=xlPasteValues
Below code will rename active sheet :
Sub Rename_Worksheet()
Dim Str As String
Dim Ws As Worksheet
Set Ws = ActiveSheet
Str = InputBox("Please provide new name for Worksheet : " & Ws.Name)
Ws.Name = Str
MsgBox "Worksheet renamed successfully to " & Str
End Sub
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.
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 am trying to copy an excel worksheet into another excel workbook. This generally wouldn't be too difficult, except I am moving my worksheet into a workbook that is saved using variables.
Here is a bit of my code:
Sheets.Add After:=Sheets(Sheets.Count)
ActiveCell.FormulaR1C1 = "a"
Sheets("Policy Search").Move After:=Workbooks(fname).Sheets(SheetName)
"policy search" will always be named the same, and this is the workbook I am trying to move. However, I am trying to move it to a file that is dynamically saved using the variable "fname", and the sheet name "SheetName".
Every time I try and run this bit of code, I am getting a Subscript out of range error. Any ideas on how to fix this?
Thanks!
Try this:
Sub MoveSheet()
Sheets("Policy Search").Select
Sheets("Policy Search").Move After:=Workbooks(fname).Sheets(SheetName)
End Sub
It assumes "Policy Search" is the active sheet.