Excel File Open Macro - vba

So I'm trying to create a Macro That will update a cell when the file is opened. I'm getting the 424 Error and so I tried to do a better job defining my code/object but It still wasn't succesfull. I think I'm missing/overlooking something really easy but I can't figure it out and this is my first project so I'm trying to learn and gain a better understanding then just googling a segment of code that will work.
Private Sub Auto_Open()
Dim Try1 As String
Try1 = ActivateSheet.Cells(3, 2).Select
Tryl = "-"
' My first attempt is shown below
'
'Sheets("Current Calc").Activate
'ActivateSheet.Cells(3, 2).Value = "-"
End Sub

You've got a typo in your commented code
What you have...
Sheets("Current Calc").Activate
ActivateSheet.Cells(3, 2).Value = "-"
What it should be...
Sheets("Current Calc").Activate
ActiveSheet.Cells(3, 2).Value = "-"
Also, I should mention you should avoid using .Activate and .Select unless necessary. With that being said, I'd suggest the below code instead...
Sheets("Current Calc").Cells(3, 2).Value = "-"
EDIT:
When using Auto_Open, Excel must be opened MANUALLY in order for the code to execute; thus if it is opened via VBA this event will NOT trigger. If you want an event to trigger via VBA as well as manually, I'd suggest using Workbook_Open

Try with the following sub:
Private Sub Workbook_Open()
Dim Try1 As String
Try1 = ActiveSheet.Cells(3, 2).Select
Tryl = "-"
End Sub
Some advices:
write your code in lowercase. When you change a line, your code will change to upper and lower case automatically, this way you will detect if you have some typo error.
write a function or object ant type . This will open a dropdown list, this way you will also avoid typo error.

Related

Excel VBA: Click two buttons (shapes) that run two separate VBA scripts on spreadsheet open

I have a spreadsheet completely locked down and control all sorting and filtering through VBA. I also have another script that hides the sheet on close and saves the file automatically to keep that sheet hidden.
I've been trying to figure out how I can use VBA to 'click' on one button (shape) which would clear anything that's been filtered and then 'click' on another button (shape) which would sort the spreadsheet alphabetically. The buttons (shapes) already work perfectly with user-interaction but I would also like these buttons (shapes) to automatically get triggered when the sheet is opened.
The first button is assigned to macro, SearchBox, associated with the following VBA -
Sub SearchBox()
Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant
Set sht = ActiveSheet
On Error Resume Next
sht.ShowAllData
On Error GoTo 0
Set DataRange = sht.ListObjects("DataTable").Range
mySearch = sht.Shapes("UserSearch").TextFrame.Characters.Text
If IsNumeric(mySearch) = True Then
SearchString = "=" & mySearch
Else
SearchString = "=*" & mySearch & "*"
End If
For Each myButton In sht.OptionButtons
If myButton.Value = 1 Then
ButtonName = myButton.Text
Exit For
End If
Next myButton
myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
DataRange.AutoFilter _
Field:=myField, _
Criteria1:=SearchString, _
Operator:=xlAnd
sht.Shapes("UserSearch").TextFrame.Characters.Text = ""
End Sub
The second one is much more simple which just sorts the data table by that specific column -
Sub Sort_Name()
Dim oneRange As Range
Dim aCell As Range
Set oneRange = Range("A4:H1162")
Set aCell = Range("A4")
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
End Sub
Basically, I'm still learning and I feel it's possible to just trigger these buttons with a script but I've yet to figure it out. Any help would be appreciated.
In the code-behind for ThisWorkbook, you will be able to handle workbook events, including the Open event, which is fired by the workbook when it is opened.
Navigate to the module (double-click ThisWorkbook in the VBE's project explorer [Ctrl+R]), then locate the dropdowns at the top of the editor's code pane. From the left-hand dropdown, select Workbook; then, from the right-hand dropdown, select the Open event; the VBE automatically creates a method stub with the correct signature for you:
Private Sub Workbook_Open()
End Sub
Notice that the underscore has a very special meaning in VBA; when naming your procedures (especially implicitly or explicitly Public ones), consistently stick to PascalCase and avoid Snake_Case; this may not matter now, but as you progress as a developer you'll come to appreciate consistency in naming, and when you start working with interfaces, that underscore-in-public-members thing will start making the difference between code that works and code that doesn't even compile: taking the good habits early will save you headaches later.
Now, you need to invoke two procedures in that handler.
When you do this:
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
You're invoking the Sort method of the oneRange object, which is an instance of the Range class.
When you do this:
MsgBox "Hi!"
You're invoking the MsgBox function that's in the VBA library, under the Interaction module (find it in the object browser [F2]). This would be equivalent:
VBA.Interaction.MsgBox "Hi!"
So, to invoke your SearchBox and then your SortName method, all you need to do is this:
Private Sub Workbook_Open()
SearchBox
SortName 'formerly known as Sort_Name
End Sub
Procedures do something - their names should always start with a verb, they're actions. "SearchBox" looks like a name, not an action. In programming, names are more like classes - a Range, a Workbook, etc.; consider renaming SearchBox to better convey what it does. If that's hard to do, it's likely because your procedure does too many things - like getting the name to use for filtering, and then applying a filter to a specific table, and then clearing the text of some shape.
You'll also want to watch out for implicit ActiveSheet references; currently SortName is assuming what the ActiveSheet is, and this is very likely to cause issues down the line.
So the best way to explain how I did what I was wanting to do is to look at what the button itself is calling to by right clicking it and clicking assign macro again. You'll see it look like this -
'YourSpreadSheetName.xlsm'!Sheet2.SearchBox
For me, what was throwing me off is I wasn't including the Sheet2 which is where the code is I'm trying to call.
By placing this code within the ThisWorkbook section with the Workbook_Open script, I was able to get it working the way I wanted it to -
Call Sheet2.SearchBox
Thanks again for the help all who commented.

VBA code crashes after saving - says the situation lies with the XML "Run-time error '429'

I made a worksheet for the company I work for to help with pricing out custom designs. A few months ago I made a macro that can save the parts to a text file that can be pulled from at a later date if we wanted to quote out the same design. Everything was working perfectly, until one day I go to open it up and got the error message
We found a problem with some content in 'File.xlsm'. Do you want us to try
to recover as much as we can?
When I click yes, it then comes up with the worksheet the macro was on completely un-formatted and says it could only open the file by repairing or removing the following part
Repaired Part: /xl/worksheets/sheet3.xml part.
This is weird because the only xml code I use is just to create a drop-down menu when the saved design names are loaded. Nothing has changed since the final revision of the code other than the amount of designs that have been saved. The boxes I had as buttons tied to macros have been deleted and none of the code for this sheet works. What it shows when I view the code now is Sheet_Thumbnails
All other macros work, the other sheets are fully functional. When I try and run the code on this sheet I get
Run-time error '429':
ActiveX component can't create object
This has to be when compiling because I can't even debug where this is happening. The best answer I get when I look this error up is that I am not using the "New" keyword when calling a file or object from somewhere else. But I have looked through my code and don't see anywhere that applies. Luckily a co-worker saved a copy off our server to her computer so I have a backup, but when I open this and run the macros then save and re-open, the same crash happens.
Here is the code with xml:
Sub MakeList(ByRef r As Range, ByRef Config As String)
r.Clear
If Not Config = "" Then
r.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=Config
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
End Sub
Can anyone help me? I am at a total loss for why this has happened and why it keeps happening. Is it the validation part? Why would it happen after working for months?
Thank you in advance.
EDIT 1
Exporting all of the code and creating a new workbook did not solve the problem.
Thanks to Profex, the problem has been found and is in the validation. Essentially one of my lists was too long. The formula used in validation is not supposed to be beyond 255 characters. Even though Excel doesn't give any warning on this, when I would create the drop down menus, although it would populate each item from the list, after saving closing and re-opening, apparently this would corrupt the coded sheet. So now the question lies with how to add values into a drop-down menu without clearing and re-initializing with a longer list. Should I post a new question for that?
In Excel, Cell Validation Lists have a 8191 character limit (which is way too long for a user to pick from anyway).
Anything over 254 characters will corrupt the file upon save/re-open.
Here is something similar to what I have done in the past to create Dynamic Validations lists:
It uses your MakeList() subroutine, and requires another GetList() function to get the validation list for the specified cell.
Since this code is in the Workbook module, I also added another function called IsSheetTheOneICareAboutWithValidations(). If you use the WorksSheet_SelectionChange Event from in a specific sheet module, this isn't required; but you would have to change the scope of m_ValidationCell and m_ValidationList to be Public.
This code is untested and goes in the ThisWorkbook module:
Option Explicit
Private m_ValidationCell As Excel.Range
Private m_ValidationList As String
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not m_ValidationCell Is Nothing Then
m_ValidationList = m_ValidationCell.Validation.Value
m_ValidationCell.Validation.Delete
End If
End Sub
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
If m_ValidationList <> vbNullString Then
With m_ValidationCell.Validation
.Add Type:=xlValidateList, Formula1:=ValidationList
End With
m_ValidationList = vbNullString
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Not m_ValidationCell Is Nothing Then
m_ValidationCell.Validation.Delete
Set m_ValidationCell = Nothing
End If
If IsSheetTheOneICareAboutWithValidations(Sh) Then
' Since we're changing the Validation each time there is a new Selection;
' It's the Active Cell that matters, not the Target range
' Add a validation list to any cell in column 4, after the header (in row 1).
If ActiveCell.Column = 4 And ActiveCell.Row > 1 Then
List = GetList(ActiveCell)
MakeList ActiveCell, List
' Should probably add this next line to you MakeList() function
Set m_ValidationCell = ActiveCell
End If
End If
End Sub
Private Function GetList(Target As Range) As String
GetList = vbNullString ' or whatever you want
End Function
Private Function IsSheetTheOneICareAboutWithValidations(Sh As Object) As Boolean
IsSheetTheOneICareAboutWithValidations = (Sh.Name = "Pricing")
End Function
Recovery
It looks like you just had a bad save. Sometimes it just corrupts the file and there isn't much you can do, other then hope you have a backup.
Right Click in the Folder > Properties > Previous Versions
If you don't have a backup, it might just help to move everything to a new file.
Create a New Workbook
Select All cells from your first sheet (click above the 1, left of the A)
Press Ctrl+C to Copy
Select All cells in your New Workbook/Sheet
Press Ctrl+V to Paste
Repeat for all Worksheets
On the VB side of things, you can just Drag the Forms/Modules/Classes from the Old file to the New One.
Issue
Did you know that all New Office documents are really just ZIP files...
Go ahead, rename the file to File.xlsm.zip
Inside the file you'll see a folder structure which should have .../xl/worksheets/sheet3.xml
This is what excel is complaining about! That file is either missing or wrong.
Code
I don't know how you're calling Makelist, so I can't verify that the range R that you are passing is valid.
Please remove the Select/Selection from your code. You don't need to select anything in the front end GUI of Excel to access/change the cells. Also you didn't check if R was Nothing.
Sub MakeList(ByRef r As Range, ByRef Config As String)
If Not r Is Nothing then
r.Clear
If Not Config = "" Then
With r.Validation
...
End With
End If
End If
End Sub

VBA Excel 2016 - Run-time error '1004'

I am trying to write code to copy and paste data from one sheet to the master sheet in the same workbook based on 5 drop down lists (using data validation) on the master sheet and have written this code for it as below, however every time I get the run time 1004 error and don't know how to solve it, is there a way to fix this?
Private Sub GO_Click()
Worksheets("Dashboard").Select
If Worksheets("Dashboard").Range(B3) = "National Gallery" And
Worksheets("Dashboard").Range(B4) = "unframed" And
Worksheets("Dashboard").Range(B7) = "Product Costings" And
Worksheets("Dashboard").Range(B5) = "N/A" And
Worksheets("Dashboard").Range(B6) = "N/A" Then
Worksheets("(7b)").Activate
Worksheets("(7b)").Range(A8, F23).Copy_
Destination = Worksheets("Dashboard").Range(D11)
Else: MsgBox ("No Data")
End If
End Sub
after copy the line put this :
Worksheets("Dashboard").activate
range("D11").pasteSpecial xlpastevalues 'or just paste depends on your need
don't forget to put " inside range("x")
hope it works
Jean-Pierre Oosthuizen is correct. adding commas to your ranges will fix the 1004 issue for you. The below code should now work for you.
Private Sub GO_Click()
Worksheets("Dashboard").Activate
If Worksheets("Dashboard").Range("B3") = "National Gallery" And _
Worksheets("Dashboard").Range("B4") = "unframed" And _
Worksheets("Dashboard").Range("B7") = "Product Costings" And _
Worksheets("Dashboard").Range("B5") = "N/A" And _
Worksheets("Dashboard").Range("B6") = "N/A" Then
Worksheets("(7b)").Activate
Worksheets("(7b)").Range("A8", "F232").Copy_
Destination = Worksheets("Dashboard").Range("D11")
Else: MsgBox ("No Data")
End If
End Sub
Unless B3, B4, etc are global variables, you're asking for .Range(null) which isn't valid. As #Jean-PierreOosthuizen said, you want .Range("B3").
Also unless you have a WorkSheet named "(7b)" (with the parens "()" included), your reference to Worksheets("(7b)") will fail next.
Bonus code review:
Please work on proper indentation - it makes your code much more readable! Future you (like you a week from now) will thank present you for doing so. Rubberduck VBA* will do that for you as well as pointing out lots of other things that will make your code better, like:
Eliminate Worksheets("Dashboard").Select - You're doing an excellent job of explicitly specifying all your worksheet references, so you don't need to .Select one.
Eliminate Worksheets("(7b)").Activate - Same as above
Eliminate the multiline Else: MsgBox ("No Data") and replace it with two lines of code - it's much more readable.
*I'm not yet a contributor to the Rubberduck project, but I'm a happy user and have learned quite a bit about better coding from them

Excel VBA: Variables resetting to 0 for some reason

here's my current code:
Private Sub Workbook_Open()
sumn1 = Sheets("Main").Cells(1, 1).Value
sumn2 = Sheets("Main").Cells(2, 1).Value
sumn3 = Sheets("Main").Cells(3, 1).Value
sumn4 = Sheets("Main").Cells(1, 2).Value
sumn5 = Sheets("Main").Cells(2, 2).Value
sumn6 = Sheets("Main").Cells(3, 2).Value
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Main").Cells(1, 1) = sumn1
Sheets("Main").Cells(2, 1) = sumn2
Sheets("Main").Cells(3, 1) = sumn3
Sheets("Main").Cells(1, 2) = sumn4
Sheets("Main").Cells(2, 2) = sumn5
Sheets("Main").Cells(3, 2) = sumn6
End Sub
So, while the workbook is open and it's being worked in, after some actions the variables sumn1, sumn2..etc. (which are global variables) are getting added values, like +10 to sumn1, or +5 to sumn2 and so on. Since I want them saved after closing the workbook, I save them in a cell that I have hidden with ";;;".
The problem is, sometimes it works correctly, but sometimes (usually after longer time since workbook has been closed since) the variables reset to 0.
So first of, is my approach an alright one on how I am saving the data or its just not working because this approach is bad? If it's an alright approach then I guess I should search for my mistake somewhere else then.
All your variables are also reset to zero when "reset" is clicked within the VBA editor, or an "End" statement is reached. There could be other occasions too although I can't think of any. You are currently only guarding against the workbook being closed and reopened.
So your approach is brittle.
What you could do is remove the global variables altogether, and always refer to the worksheet for the setting and retrieval of their values.
You can improve your approach with a couple of helper routines
Dim gVars as Variant
Sub LoadVariables()
' Load data from s/s into variable
gVars = Sheets("Main").Range(Cells(1, 1), Cells(3, 2)).Value
End sub
Sub SaveVariables()
' Save data from variable into variables
Sheets("Main").Range(Cells(1, 1), Cells(3, 2)).Value = gVars
End sub
Then in your code use LoadVariables when you need to use the data (usually at the start of whatever sub needs it) and SaveVariables when you have updated the values. Obviously this needs more error handling, etc.
The loading of the entire variables set into a single array also makes it easier to maintain the code (less chance of typos) and handle iterations if needed.

Excel Macro for updating "change date" makes Excel crash

I have an excel file in which I have several tables with the field "Last change:". If any change is made to that table, the field should update to the current date.
I implemented a macro that did what it should, but unfortunately it disabled the "Reverse" (Strg + Z) function which was very annoying. Therefore I edited the macro such that the Reverse-functionality would work again. My macro now looks like this:
Option Explicit
Public Merker
Private Sub Worksheet_Change(ByVal Target As Range)
If Not ThisWorkbook.ReadOnly Then
Application.OnUndo "Rev. Change", "Wiederherstellen"
Merker = Cells(3, 2)
Cells(3, 2) = Date
End If
End Sub
Sub Wiederherstellen()
Cells(3, 2) = Merker
End Sub
When I apply this macro, Excel crashes as soon as I make a change to the document.
Is there something invalid in my code? I am really wondering since there is no error message or wrong behavior but only the application crashing. This crash happens reliably every time.
Thanks for any help!
You event is likely to be calling itself in an infinite loop. Try disabling Events to stop the code re-calling itself on this line Cells(3, 2) = Date
Private Sub Worksheet_Change(ByVal Target As Range)
If Not ThisWorkbook.ReadOnly Then
Application.EnableEvents = False
Application.OnUndo "Rev. Change", "Wiederherstellen"
Merker = Cells(3, 2)
Cells(3, 2) = Date
Application.EnableEvents = True
End If
End Sub