I currently have this VBA -
Sub StartTimer()
Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"
End Sub
Sub AutoCalc()
Application.CalculateFull
Sheets("Kine").Range("B603:E603").Copy _
Destination:=Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1")
Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"
End Sub
The .OnTime command is working perfectly without the Copy/Paste section, which is great.
This gives me a list of values from an SQL query that will auto-update, as well as an average at the bottom of each column of values.
I'm trying to set this up so that the average will automatically be added onto columns in Minitab but I believe that the Macro is stopping the Auto-Update in Minitab.
So my idea is to copy-paste the averages into an Excel Workbook that has no macros of its own and then link that to Minitab.
Have I put in the Copy-Paste code wrong or is there some issue with where the macros need to be stored and how?
Quick Edit - I should add that current code gives "Run-Time Error 9, Subscript out of range" and highlights copy/paste code.
I've found the solution.
My destination workbook was open in a separate window so the source wasn't recognising it as being open. Bit of a nightmare!
It's necessary to have both workbooks open in the same instance of Excel.
Additionally, my original paste code only pasted "#REF". I have changed that to -
Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1").PasteSpecial xlValues
Works much better.
One more thing, in case anybody might find it useful. The source workbook must be active in order to carry out the auto-update.
Adding below line sorted out most issues though its still a work in progress -
ThisWorkbook.Activate
Related
I am a VBA beginner trying to determine the "normal" behavior of excel during a timed call to a macro using Application.OnTime. In my code (below) I am calling ActiveWorkbook.RefreshAll but I think this question may be relevant to the timed call to any macro. As a newbie, I want to understand the behavior and limits of VBA so that I can determine if VBA is the right tool or if I should use another solution in certain cases (perhaps python / openpyxl).
My set up: I am using Microsoft Excel 365 (annual subscription) and the Account page says I am using Version 2006 (Build 13001.20384 Click-to-Run). I have a single workbook containing a single Sheet1 which contains the formulas to import stock data using the built in feature under the Excel Data tab. I have several rows (one stock per row) of columnar data corresponding to the stock fields such as ticker symbol, price, etc.
To automatically refresh the data, I borrowed & modified code to refresh the data every minute. The code works fine. The "issue" I have is that between refreshes, if I start editing any cell on the sheet and continue to do so during the time when the next automatic call to the refresh should occur, it indefinitely delays the execution of said call until the moment I complete (or cancel) the cell edit. When it does the refresh upon my edit completion that refresh does seem to work correctly.
If I start a cell edit and do not complete it for several minutes then only a single refresh occurs when I complete (or cancel) the edit, even though several intervals may have passed (it does not makeup/catchup the missed/delayed calls).
This behavior is OK with me in this application, but I would like to understand if this is considered "normal" behavior, and if I should expect it to always behave this way. Perhaps this question extends beyond a refresh call but also to any timed call to any macro that may be delayed as a result of a cell edit in progress.
Here is my code.
This is in a file called VBA-Test-02-StockData.xlsm under ThisWorkbook (Code):
Private Sub Workbook_Activate()
'ActiveWindow.WindowState = xlMaximized
Application.Speech.Speak ("Activating stock workbook")
Call AutoRefresh
End Sub
This is in the same xlsm file under Module1 (Code):
Sub RefreshStockData()
Application.Speech.Speak ("Refreshing stock data")
ActiveWorkbook.RefreshAll
End Sub
Sub AutoRefresh()
Call RefreshStockData
Application.OnTime Now + TimeValue("00:01:00"), "AutoRefresh"
End Sub
I have finished a small Project at work and distributed the File to my coworkers.
I saved the file as .xlsm to enable makros. I have used the code for about 2 weeks now and never had any problems with it.
However when i sent them to my Coworkers one makro doesnt work.
I have multiple makros in the File and only one of them doesnt work anymore so i believe its the makros problem and not the new Excel version:
Sub Loomisinsert()
'Deletes old Data and inserts new
Sheets("MSE").Range("B2:J1000").Clear
Sheets("MSE").Range("B2:B2").Select
Sheets("MSE").Paste
'deletes unnecessary data
Sheets("MSE").Range("K3:N5000").Delete
Sheets("MSE").Range("P3:Q5000").Delete
Sheets("MSE").Range("S3:U5000").Delete
Sheets("MSE").Range("L3:L5000").Delete
End Sub
The user is only supposed to copy Data from another Workbook and use an excel button to insert the data at the right place and delete the correct cells.
As you can see i tried to specify which sheet to use for each command. And it works fine for me but not on my Coworkers Computer.
They have Excel 2013 and 2010 the same i use.
Below are some suggestions. Modify and try:
Option Explicit
Sub Loomisinsert()
With ThisWorkbook.Worksheets("Sheet1")
'Deletes old Data and inserts new
.Range("B2:J1000").Clear
.Range("B5").Copy .Range("B2") '<- Easy way to copy - paste
'Deletes unnecessary data
.Range("K3:N5000,P3:Q5000,S3:U5000").Clear'<- Does not need to use "L3:L5000" because included in "K3:N5000". Additionally, have in mind that *.Delete* may cause problem with columns & rows. It s more preferable to use *.Clear* or *.Clearcontents*. *.Clear* used to clear both formats and value. In the other hand *.Cleatcontents* just empty the cell from value.
End With
End Sub
I have a strange issue I can not make run following subroutine :
Sub Copy_Workbook()
Workbooks("book1").Sheets("Sheet1").Range("a1").Copy _ Workbooks("book2").Sheets(2).Range("a2")
End Sub
I have both excels xlsm, both are open and are open in the same instance.
I do receive the problem Out of range.
If anyone has any idea why it could not work can you please advice , thanks.
The following sub will work.
Sub Copy_Workbook()
Workbooks("book1").Sheets("Sheet1").Range("a1").Copy Workbooks("book2").Sheets(2).Range("a2")
End Sub
You have to confirm:
Two workbooks are open, Book2 have at least 2 (two) sheets. Check all spelling are correct for sheet name.
If your workbooks haven't been saved then referencing without their file extension will work.
Once you save them then you must include the extension.
You can confirm this by creating a new workbook then going to the Immediate pane in the VBA IDE and typing ?workbooks("Book1").name. It will return "Book1".
Save the workbook in .xlsm format. Go back to the VBA IDE Immediate pane and re-execute ?workbooks("Book1").name. It will throw a "Subscript out of range" error.
Changing your code to:
Sub Copy_Workbook()
Workbooks("book1.xlsm").Sheets("Sheet1").Range("a1").Copy _
Workbooks("book2.xlsm").Sheets(2).Range("a2")
End Sub
will work!
I’m running excel 2010 on windows 7.
The following macro does what you would expect, ie (1) inserts a new worksheet, (2) adds a rectangle, and (3) activates cell A1.
Sub addSheetAndButton()
Dim buttonSheet As Worksheet
Set buttonSheet = ThisWorkbook.Sheets.Add
buttonSheet.Shapes.AddShape(msoShapeRectangle, 100, 100, 100, 50).Select
buttonSheet.Range("A1").Activate
End Sub
My problem is when I try to run it with a Worksheet.Activate event, for example:
Private Sub Worksheet_Activate()
Call addSheetAndButton
End Sub
This time (1) a new worksheet is not inserted, (2) the rectangle is added to the worksheet associated with the activate event, (3) cell A1 is not activated and (4) the rectangle remains activated.
What am I doing wrong? Any help would be greatly appreciated.
Thanks very much for trying this. I’m sorry it has taken me a while to get back, but I’ve been experimenting with this. I have discovered that when I exit and restart Excel, the addSheetAndButton macro works as expected under the Worksheet.Activate event. However, I have a different macro that uses another Worksheet.Activate event to update some charts. On what seemed to be a random basis, this would create a “run time error 6” - an overflow that resulted from trying to assign an out of bounds value to a variable.
I never got this error if I bypassed the Worksheet.Activate event/macro and ran the chart update macro by hand. After resetting VBA/Excel from the “run time error 6”, the addSheetAndButton macro behaved exactly as I described in my original post. The only way to cure it was to exit and restart Excel.
I think (hope) that I have traced the source of the run time error back to a line in my chart update macro where I had selected rather than activated a worksheet before reading data from it – though I am surprised at the result. It is worth pointing out that once Excel entered the “not as expected” state, the on-screen message from the following two lines was the name of the previous active sheet and not “Sheet1”.
Worksheets(“Sheet1”).Activate
Msgbox ActiveSheet.Name
Again, the only apparent way to cure this was to exit and restart Excel.
Thanks again for your help.
I'm trying to create a VBA in Excel 2010 that takes info from another spreadsheet that I'm not allowed to alter and bring it over to the spreadsheet with my macro built in. Here's my code:
Sub BringUpWorkbook()
Workbooks("RITE 1624.xls").Activate
End Sub
I have several VBA books, have visited dozens of sites on the Internet, including those here at stackoverflow.com, and cannot find a reason why I'm receiving the run-time error. The workbook is already open, I've tried adding everything trailing the title, I've tried removing the .xls, I've even did all of the above with in a variable. Any ideas?
Make sure the extension is correct. If it's a excel-2010 file like you indicate, you may need to update your code to reflect the extension of your "RITE 1624" file (e.g. .xlsx for a 2010 Excel workbook, .xlsm for a 2010 Excel macro-enabled workbook, or whatever the extension is.
Sub BringUpWorkbook()
Workbooks("RITE 1624.xlsx").Activate
End Sub
EDIT:
To make sure you have the right name of the workbook, you can print the name of each workbook in an immediate window.
Open up the VBA editor, and then press Ctrl+G to open the Immediate Window (or do View > Immediate window). Then run the following Macro:
Sub OpenWkbkNames()
For Each Workbook In Workbooks
Debug.Print Workbook.Name
Next
End Sub
Note that this will give you the names of all the open workbooks in the same Excel instance as your macro. If your RITE 1624 file is in a separate Excel instance, then the instance that your macro is in will not be able to see that file (and it won't appear in the output of the OpenWkbkNames code above). The simplest way to resolve this is to open all of your necessary files from the Excel instance that contains your macro.
Sub BringUpWorkbook()
Workbooks.Open("RITE 1624.xls").Activate
End Sub