Application.Goto Run-Time Error 5 - excel

I have been an active user of the information on this site and it has generally served me very well. I am struggling with this one however... I built an excel document a while back with this code:
Application.Goto Range("PART_NUMBERS").Cells(Application.Match(ROP_UPDATE.Label5.Caption, Range("PART_NUMBERS"), 0))
and it works beautifully! Basically it checks to see if a checkmark on a userform is checked and if it is it then finds the label associated with that checkmark and it finds it in a range where i can use other lines of code to do stuff. anyway... I am building another document where i need to do essentially the same thing and i built the code accordingly:
Application.Goto Range("po_s").Cells(Application.Match(PO_Checker.Label1.Caption, Range("po_s"), 0))
it gives me run-time error 5 every time. Identical code basically and i have looked over my named ranges and changed them a few times. I am stumped. Why does this code work good on my first document but not the one i am working on now? both are macro enabled. Any suggestions help. Thank you!

Related

Select headers in table - method range of object _ global failed - works when run with button? but not stepping through

When stepping through my code i get error 1004 on this line.
Range("tablename[[#Headers],[startheadername]:[lastheadername]]").Select
Now the weird thing is, I use it every month without issue via a button.
This error only pops when im stepping through the code.
this works instead
ActiveSheet.ListObjects("Tablename").HeaderRowRange(1).Resize(, 7).Select
The error code involves the words "range of object" which made me think that maybe there was a way to use the object in another manner. my original code was just a simple range reference.
I dont have particular insight into listobjects, i mainly copy pasted and tested. But headerrowrange(1) is the first column header in the tablename, resize(,7) expands it from 1 to 7 and .Select does what it does.
So this new code selects the first 7 table headers without error 1004.
This site got me there https://exceloffthegrid.com/vba-excel-tables-code/

How to Prevent Free Distribution of Excel Spreadsheet

I reviewed the answer to the question at the link below. This seems to match my need, but I am not able to get it to work for me. What am I doing wrong so this will work. This code can be a great tool for me to limit the free distribution of my spreadsheet which I have commercialized.
Thank you for the help.
How to prevent free distribution of a commercial Excel spreadsheet
When I put the code into the modules and save the file, when the file is opened I am receiving the following error.
Compile Error:
Sub or Function not defined
It then highlights the reference to "Refresh_Serials".
I am running this currently on Excel 2007. Do I need to try it on a newer version of Excel maybe?
I have used the same code as included in the link below.
I would expect it to do as expected per the original post, although I am not 100% sure exactly what the outcome should be. Maybe part of my gap at this point is that I haven't yet established the HTML table. Could I use a "Google Sheet" instead with a table in it?
If you want this Workbook to be compatible for Specific Users only. Then one way to achieve it is to add a WorkBook_Open event. Like:
Private Sub Workbook_Open()
If Not Application.UserName = "Mohit Bansal" Then
MsgBox "Not the Correct User"
ThisWorkbook.Close False
End If
End Sub
Change the Name as Per the User, and lock the VBA Project. You can even make a list of names to check from if multiple users are going to use it.
This will make sure that only one user is able to use the Workbook.
There are obviously other complicated & efficient ways to achieve this, but I prefer this one for Simplicity.
Looking at the linked to code source (what the actions are that follow that line) and the coding style of the author I think that Refresh_Serials is in fact a label. It should be Refresh_Serials:. The lack of the ":" means the compiler is expecting a sub/function of that name. As there are no GoTo statements these labels, there is another proper label further on,SerialVerified:, are unnecessary and confusing. They could simply be comments. Better still, go more Single Responsibility Principle and have functions/subs with names akin to those labels and shift the relevant re-factored code into them.

VBA not updating on Protected Sheet -- Excel Office 365

There are tons of questions already posted with a similar problem, but none of those solutions works for me. I'm using Office 365, so maybe it's something that changed there. Anyway, here is my issue.
I have a file that pulls info from one main tab into other tabs. On the subsequent tabs, I have VBA that auto-hides rows depending on the value that was pulled from the main tab. Anytime the data in the main tab is updated, the subsequent tabs should also update. I had an issue with making this work, but I worked it out by using another cell to store the previous value that had transferred (comparing them, and running the code any time they were dissimilar). The problem now is, of course, that it wont run when the tabs are protected. The tabs need to be protected to prevent end-users from corrupting the formulas and formatting, but I can't make it work with any of the solutions posted on StackOverflow. My code looks something like this:
Sub Worksheet_Calculate()
If Range("Old Value") <> Range("New Value").Value Then
Range("Old") = Range("New").Value
Rows.EntireRow.Hidden = False
<<Insert code to auto-hide based on "New Value" cell>>
End If
End Sub
I've tried adding Worksheet.Unprotect Password:= and Worksheet.Protect Password:= commands on either side of the If statement, and I get a 424 Object Required error. I've tried using the "UserInterfaceOnly = True" command with my password protection, and it changes nothing (still get the trying to change a protected sheet error). I've tried creating another subroutine for unprotecting and re-protecting, then using a GoTo to run the original subroutine; but that didn't work either. I see a lot of solutions using ActiveSheet commands, but I don't think I can use them. I'm updating the main tab, and the action is happening on another tab. I'm fairly new to coding, and I'm teaching myself as I go; so, I could be missing something obvious.
It works perfectly when unprotected, so I guess I may have to just leave it that way. I hate wasting time fixing the files when end-users do their best to ruin them, though, so I'd love to leave it protected.
This may not be the final answer, but I seem to have stumbled upon a solution. Since the only thing I want happening on the protected sheet is for rows to auto-hide, I left row formatting unprotected. It seems to work fine that way. You would think there's a way to run your VBA normally without having to jump through hoops because of protection....but, I'll take it for now.

Excel cell locked when it is not locked

I have experienced the exact same problem as mentioned in this post.
Excel thinking cells are locked when they aren't
I tried to ask for clarification by responding / reviving the thread but it appears this is not the correct protocol. I only came across this post after numerous search string searches and this is the only relevant "hit" I could find.
My spreadsheet is similarly complex and has been running flawlessly through Excel 98, 2003 and 2010. When I use Excel 2013 (and I believe same problem happens with Excel 2016) I find cells which are unprotected (for data entry) become locked following use of a macro button, typically navigating to view a graph and then returning. Properties of the cell confirms the cell is not protected.
Reading the above post and the Microsoft links, I have only a shallow understanding of the problem i.e. SDI / Single Document Interface.
The above referenced post received a solution that was acknowledged as having resolved the issue. Perhaps I did not fully understand but no matter where I placed the code snipped it did not help me.
The code offered was:
Private Sub UserForm_Layout()
Static fSetModal As Boolean
If fSetModal = False Then
fSetModal = True
Me.Hide
Me.Show 1
End If
End Sub
There was another suggestion to try: MyForm.Show False I did not know where to pace this similarly to the main suggested code to resolve.
VBA did not like me using "Me" (Me.Hide / Me.Show 1). I tried place the code snippet in several places ... the main opening macro, the navigation macro to and from the sheet that is affected, changing "Me" to the sheet name all to no avail. It is a sheet but since the unique variables are gathered here, it may loosely be viewed as a "Form".
I used to be a competent amateur but never an expert. I am appealing for a kind person to help direct me to overcome this "Excel 2013" problem. I have tried and failed many times and spent many hours trying to fix only to find the problem is because I'm using Excel 2013. There are even now no issues using Excel 2010 which I am able to access via a friend.
I'm sorry my first post is so detailed and hopefully not over-complex. I only wanted to give the correct information about the problem.

Sheet / Range Select (combined) not working

Something has changed in my VBA that is not allowing me to complete certain routines. I have listed a very simple example below. If anyone has experienced this I would be really appreciate any support in resolving this issue.
Simple example of issue:
When I use the following code it works fine.
Sheets("Sheet1").Select
Range("B3").Select
When I combine them I get a "1004" error
Sheets("Sheet1").Range("B3").Select
I checked the reference/document library and nothing appears to have changed in here. It has to be something simple but I just can't put my finger on it.
If you absolutely must do it in a single line of code then swap the Select for an Application.GoTo which accepts both worksheet and cell range.
application.goto range("Sheet1!B3")
However, it is almost never necessary (and most often counter-productive) to use the Range .Select method to reference a cell or cells to work on. See How to avoid using Select in Excel VBA macros for methods on getting away from relying on select and activate to accomplish your goals.
You already have your answer:
first Select the worksheet
the Select a range on that worksheet
Your code will work if you happen to be on Sheet1 when it is run, but will fail if you are not on Sheet1. In my opinion VBA is kind of dumb with regard to this issue.

Resources