How to Open workbook? - excel

I have this macro that used to work nicely for a while.
I replaced seemingly irrelevant function Insert Picture for Add Shape Picture in different Sub and all sudden. It stopped working with error 1004 on line 3.
Can you advise what I have wrong there, please?
Public Sub CopyData()
ThisWorkbook.Worksheets(2).Range("C:Z").Clear
Workbooks.Open Filename:=ThisWorkbook.Path & "\Book1.xlsx"
Workbooks("Book1.xlsx").Worksheets(1).Range("A1:Z200").Copy
ThisWorkbook.Worksheets(2).Range("C1").PasteSpecial
Application.CutCopyMode = False
Workbooks("Book1.xlsx").Close SaveChanges:=False
ActiveWorkbook.Sheets(1).Activate
End Sub

The targeted sheet for Paste function was protected (locked). It works now.

Related

Excel VBA, ActiveWorkbook.Save trigger the event Workbook_BeforeSave, but the code line in Workbook_BeforeSave is passed without any result

I've searched this site and found similar problem but still different.
Those I found in this site (such as this, this and this) involve too much code (which maybe the reason it doesn't do correctly) and also involve Workbook_BeforeClose event.
Mine is very simple as follow :
Sub RefreshData()
ActiveWorkbook.Save
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.DisplayAlerts = False
MsgBox "test"
Range("A1").Select
Range("A3").Value = "test"
Sheets(Array("TABEL", "DATA")).Copy
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path + "\Backup\blablabla " & Format(Now(), "yymmdd hh mm ss") & ".xlsx", FileFormat:=51
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
When I run the RefreshData sub, it trigger to Workbook_BeforeSave sub.
Within the code in Workbook_BeforeSave, when the yellow line pass the msgbox code it show the result as Excel shows the message box. But when the yellow line pass the rest of the code, it does nothing, no error. Range("A1") not selected but it does have the "test" value. New workbook with sheets DATA and TABEL not created, letalone is saved to the backup folder and close it. Yet, the workbook with the macro is saved.
My question : is that normal ?
My hope : someone will be kind enough to replicate the code above in a new workbook in his Excel app and run it. If all the codes in Workbook_BeforeSave work accordingly, then there's something wrong with my Excel app and I hope someone can inform me what might be the cause within my Excel app.
Any kind of help would be greatly appreciated.
Thank you in advanced.
More a suggestion than an answer, but:
Try properly qualifying ranges:
change Range("A1").Value = test
into ThisWorkbook.Range("A1").Value = test
Also if it's about ThisWorkbook, use that instead of ActiveWorkbook.

How to assign macro to button without using "active sheet" in VBA?

I am trying to change the macro assigned to a button from a different sheet than where the button is. This is how far I've gotten:
Sub Macro1()
ActiveSheet.Shapes.Range(Array("Button 1")).Select
Selection.OnAction = "Macro2"
End Sub
However, the above code requres me to be on the active sheet. I have tried the following:
Sub Macro1()
Sheet1.Shapes.Range(Array("Button 1")).Select
Selection.OnAction = "Macro2"
End Sub
However, this will give me an "Object doesn't support this property or method" error.
Why doesn't it work when "ActiveSheet" is replaced with "Sheet1"?
And why can't I collect the two lines of code into one line?:
Sub Macro1()
Sheet1.Shapes.Range(Array("Button 1")).OnAction = "Macro2"
End Sub
Any help would be appreciated!
Please, simple try:
Sheet1.Shapes("Button 1").OnAction = "Macro2"
Of course, a macro named "Macro2" should exist in a standard module. If in a sheet code module, the sheet CodeName is necessary in front of the macro name ("Sheet1.Macro2")...

Screenupdate vs Selection issue - VBA Excel

I'm facing an odd situation. I have a button on a sheet which runs many functions, being one of those opening another file:
If Not IsItOpen(ENDERECO2) Then
Workbooks.Open Filename:=ENDERECO1
End If
'ENDERECO2 has the file's name
'ENDERECO1 has the full path of the same file
'IsItOpen is a private function as follows:
'Private Function IsItOpen(Name As Variant) As Boolean
' On Error Resume Next
' IsItOpen = Not (Application.Workbooks(Name) Is Nothing)
'End Function
After opening the other workbook, when it isn't already opened, I bring focus to the first sheet, as I want the second one to be opened on the background. To do that, I use:
'At the very beggining of the code
Dim CEL As Range
Set CEL = Selection
'And at the end of it all
CEL.Select
All the described code works perfectly.
The problem I've been having: as this button runs many things at once, I wanted to add an "Application.Screenupdating = False" at the beggining and "... = True" at the end, so it won't flicker too much when calculating. The thing is that when I added the Screenupdating stuff, it will still open the second workbook as desired, but it won't bring the focus back to the main workbook. Instead, it stops at the recently opened workbook and there it stays.
What could be the interference of the Screenupdating on the CEL.Select command?
Any ideas?
Cheers
Thanks Taelsin. Guess when we don't know exactly why, we improvise lol. This worked fine:
If Not IsItOpen(ENDERECO2) Then
Application.ScreenUpdating = True
Workbooks.Open Filename:=ENDERECO1
Application.ScreenUpdating = False
End If
It is good enough. Cheers!

Overwrite data without asking

I want to edit this macro to overwrite previous data in another workbook without prompting. See attached code. Any help would be greatly appreciated.
Sub AV()
Workbooks.Open Filename:="T:\Cleveland\Avon\Monthly Sales\Monthly Sales 2018.xls"
Windows("Sales_By_Day_Location Analysis.xlsm").Activate
Sheets("AV").Select
Range("A1:AC88").Copy
Windows("Monthly Sales 2018.xls").Activate
Sheets("Avon").Select
Range("A1:D1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'esp
End Sub
As per comments from John, and after tidying up your code a little, I believe something like the code below would do what you are expecting:
Sub AV()
Application.DisplayAlerts = False
Workbooks.Open Filename:="T:\Cleveland\Avon\Monthly Sales\Monthly Sales 2018.xls"
Workbook("Sales_By_Day_Location Analysis.xlsm").Worksheets("AV").Range("A1:AC88").Copy
Workbook("Monthly Sales 2018.xls").Sheets("Avon").Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'esp
Application.DisplayAlerts = True
End Sub
You should attempt not to use Activate or Select statements. Also your copy range is a lot bigger than your paste range, so for the purpose of this answer I've changed the paste range to A1.
If you want to then save the workbooks and close it without a prompt, you could do something like:
Workbook("Monthly Sales 2018.xls").Close SaveChanges:=True

Auto Open used to execute another macro with VBA

I have an excel book titled Can Opener that is opened by my task scheduler. It opens another workbook on a shared drive that my task scheduler cannot access, and then closes itself.. Can Opener works fine. The problem I am having is that the other workbook has code that, upon open, if it is 7pm system time executes an update macro to get new data from the servers. Once this is done the file saves and closes. The data workbook opens, but no update is occurring, and it does not automatically save and close. I have tied breaking the code down so it would at least update, and this is not working either. I have checked several forums and other locations and I still cannot figure out what the problem is. Can I get a little help?
Sub DataBook_Open()
Application.EnableCancelKey = xlDisabled
If Hour(Now) = 7 And Weekday(Now, vbSunday) < 7 Then
Run_Update
Me.Save
Application.Quit
Else: Me.Save
Application.Quit
End If
End Sub
Run_Update is the name of the macro in my module that merely executes that other macros in the module that create the server connection and run the SQL's. This works fine if manually ran, or run with a button, so I know the error is not here. Also I am trying to run this code every night at 7pm.
I really appreciate any help I can get guys.
Are you sure DataBook_Open() executes at all?
It might not unless you're calling it from Auto_Open() or Workbook_Open().
Try this minimal test files. Replacing the paths with your actual folders.
--can_opener.xlsm--
Sub OpenOtherWorkbook()
Dim sWbkPath As String
sWbkPath = ThisWorkbook.Path & "\" & "test_data.xlsm"
Dim wbkData As Workbook
Set wbkData = Workbooks.Open(sWbkPath)
End Sub
--test_data.xlsm-- in the ThisWorkbook Object
Private Sub Workbook_Open()
If Hour(Now) = 9 Then 'replace with your condition
UpdateData
ThisWorkbook.Save
DoEvents
Else
DoEvents
End If
ThisWorkbook.Close
'Application.Quit
End Sub
Sub UpdateData()
ThisWorkbook.Sheets(1).Range("A1").Value = Format(Now, "yyyy-mm-dd:hh\hmm")
End Sub

Resources