VBA - Can't use Sheets object without error - excel

I have an Excel file with one sheet called Master.
I have a button to delete a value from the database. This is what I have written that's relevant:
Sub delete_this()
On Error GoTo ErrorCatch
simple = Sheets("Master")
.........
ErrorCatch
MsgBox(Err.Description)
End Sub
It fails immediately when I use Sheets, saying "Application-defined or object-defined error." However I'm using other code that I know works as a reference and they called this no problem (though their file had multiple sheets).
Also in general I'm new to VBA and find it pretty unintuitive with its error messages, and finding out variable values. So any advice there would also be appreciated.

As mentioned in a comment by Scott Craner, you need to use the Set statement whenever assigning an object to a variable when using VBA. This little gotcha doesn't exist in VB.NET, and is easy to forget about these days.
Set simple = Sheets("Master")

Related

VBA Run time error '91' - cannot figure out why error is occurring

I want to preface this with the fact that I have no real VBA experience. I just research the Microsoft VBA reference documents and build my code piece by piece from there.
I have created a user form to collect data that is entered into a table. I wanted to be able to go through each record and retrieve/update records as needed since information is typically gathered at multiple stages.
I am getting an error on the below code (bold line erroring out):
Private Sub AddRecord_Click()
ActiveSheet.ListObjects("Status").ListRows.Add
**ModifyTableRow StatusTable.ListRows(StatusTable.ListRows.Count).Range**
UpdatePositionCaption
End Sub
I don't know what I am doing wrong and have not been able to troubleshoot on my own. I must be misunderstanding something.
ListRows.Add returns the just-added row, so you can use that directly in a With block
Private Sub AddRecord_Click()
With ActiveSheet.ListObjects("Status").ListRows.Add
ModifyTableRow .Range
End with
UpdatePositionCaption
End Sub

Renaming an Excel worksheet generates an error

I am trying to rename an excel sheet to the contents of a cell on that sheet using VBA.
I have created a separate module called Rename_Sheets and various posts online suggest the following code:
Sheets(3).Name = Sheet3.Range("A5")
or very similar variations thereof.
Using debug.print, both parts of the above code return the expected results, i.e. the sheet name and the cell text.
When I run the code as quoted I get the message "Application-defined or object defined error".
I don't understand why I get the error message.
u have a typo
replace
Sheet3.Range("A5")
to
Sheets(3).Range("A5")
good luck
Sheet3.Range("A5") returns a Range Object. What you want instead is the Value Property of the returned Range Object:
Sheets(3).Name = Sheet3.Range("A5").Value
Note though, that once the code runs once and Sheet3 is renamed, it will start throwing an error again because "Sheet3" will not exist. If you made it Sheets(3).Range("A5").Value similar to the other side of the equal sign, this code would would work more than once.

MsgBox (ActiveWorkbook) - "Object doesn't support this property or method"

Good day.
This may be simple and all, but I would just like to know.
Within the first Sub of my Form, I placed at the very beginning of the code the following line:
MsgBox (ActiveWorkbook)
Such line would usually prompt the workbook active (as the call suggests). When placed in other sections of the project, it surely works, but not here. Instead, this error appears:
Run-time error '438':
Object doesn't support this property or method
So, I'd just like to gain more understanding on the subject. Thanks.
PS. The sub I'm placing that line of code in is a simple button_Click().
Thanks again.
I have to question the "Such line would usually prompt the workbook active (as the call suggests)" assertion.
MsgBox takes a Variant as an argument, but one that can be successfully cast to a String (MsgBox$ is almost always better practice).
ActiveWorkbook returns a Workbook object, and a Workbook cannot be cast to a String. I don't recall off the top of my head what the default property of Workbook is, but I know it isn't convertible into a String. If you want to get the name of the Workbook, you need this:
MsgBox$ ActiveWorkbook.Name
You would need that anywhere, Form, Class, Module, ...anywhere.
Better practice would be to get a reference to the active Workbook and use that throughout your code. See How to avoid using Select in Excel VBA macros.

Protected Sheet Creating Error

I have kind of a paradox problem with my macro enabled workbook (paradox meaning that the very same commands work just fine in another protected sheet of mine):
Once I start protecting my Worksheet, the commands ".Interior.Color" and ".Borders(xlEdgeRight).LineStyle" will continue to produce "application-defined or object-defined errors"
Here's an example of one macro showing that very error:
Private Sub Con1_Click()
Sheet1.Unprotect Password:="bla"
Worksheets("SOLL").Range("N23:S25").ClearContents
Range("N23:S25").Interior.Color = RGB(225, 225, 225)
Range("P23:Q25").Borders(xlEdgeRight).LineStyle = xlNone
Sheet1.Protect Password:="bla"
End Sub
Note: I added the Unprotect/Protect commands to get rid of other errors that kept occuring. Like I said, the two commands work just fine in another sheet, so I really can't figure out any source of the problem. If anyone has encountered similar problems yet or has any ideas for solutions, I'd be glad for the help!
Too long/confusing to put in comments, so I will put this as an answer, perhaps it will shed some light on your problem. You say:
All commands of that Macro refer to the 'ActiveSheet',
But this is not necessarily true. Observe the three different constructs you're using to refer to worksheets:
Explicit reference to a worksheet by its codename: Sheet1.Unprotect ...
Explicit reference to a worksheet by its sheet name: Worksheets("SOLL")...
Implicit reference to whatever worksheet is active at run-time: Range("N23:S25")..
There are many circumstances which falsify your assertion that "all commands of that macro refer to the ActiveSheet. For example, Sheet1 may (or may not) be the same as Worksheets("SOLL").
So, potentially this code is referring to as many as three different worksheet objects! Only the implicit, unqualified Range statements can be guaranteed to refer to the ActiveSheet.
Perhaps this test will shed some light on the matter. Modify your code as follows:
Private Sub Con1_Click()
MsgBox "Sheet1.Name is: " & Sheet1.Name
MsgBox "Worksheets("SOLL").CodeName is: " & Worksheets("SOLL").Codename
MsgBox "The activesheet is: " & ActiveSheet.Name
End Sub
You may also add the following information, which could be useful to those offering assistance:
Where is this button located? (On which worksheet?)
Where is the code for the button event located? In a sheet module or a standard module?

Error in Excel macro: Application-defined or object-defined error

I have a very basic problem in my excell macros. I recorded a macro with Excell and just selected one sheet on it, as you might imagine, the resulting code is as follows:
Sub Macro2()
Sheets("Graphs").Select
End Sub
but when I try to execute the macro above, I get the error: "Application-defined or object-defined error"
Anyone seen this before?
The error you are getting indicates that VBA could not determine which object you are referring to.
VBA is good because it is "smart" and allows you to program without specifying exactly what you are referring to.
VBA is bad because of exactly the same reason, because it can be very hard to determine why VBA is not using the object you want it to use unless you know which priorities VBA is using. The same is true not only for objects but also for "implicitly" typed variables, you may want VBA to be handling your variable as a number but VBAs "smartness" is causing it to handle it as a string.
The way around this is to explicitly type want you want VBA to do, in your case VBA is probably trying to reach a sheet named "Graphs" in a different workbook that doesn't have a sheet named "Graphs".
Try this instead:
Sub MyMacro()
Dim myWorkBook As Excel.Workbook
Dim myWorkSheet As Excel.Worksheet
Set myWorkBook = Application.Workbooks("nameofyourworkbook.xlsm")
Set myWorkSheet = myWorkBook.Sheets("Graphs")
myWorkSheet.Select
End Sub

Resources