I would like to change the colour of two cells (C3:C4) to red every time my workbook is opened.
The code I have tried is in my Workbook_Open event but I'm getting Application-defined or object-defined error. Here is the code:
Private Sub Workbook_Open()
Worksheets("Balance Sheet").Range("C3:C4").Interior.Color = vbRed
End Sub
Is my syntax wrong or is it more that this can't be done during the workbook_open? And how can I correct it?
Ensure that your worksheet is called "Balance Sheet". Any typo would make Excel to not find and return an error.
As you wrote its not a typo.
So please check if makros are enabled in the excel file. In the default configuration makros are blocked and therefore nothing happens. Your code is working...
https://support.office.com/en-us/article/Enable-or-disable-macros-in-Office-files-12b036fd-d140-4e74-b45e-16fed1a7e5c6
Ah, it appears I was getting the error because I was trying to edit a locked sheet. Added lines to unlock and lock and it works fine.
Slightly embarrassing but lesson learned for next time.
Related
I created a new macro earlier and it was working fine but now I get this error. I have not changed anything including the columns or worksheet name, any ideas why it's erroring now? The row that is highlighted in yellow when I try to debug it is:Columns("L:L").Select.
Sub FTE_Joiners()
FTE_Joiners Macro
Sheets("Joiners").Select
Columns("L:L").Select
Range("L2").Activate
Selection.NumberFormat = "0"".""0""""0"
End Sub
You can reduce that to
Sheets("Joiners").Columns("L:L").NumberFormat = "0"".""0""""0"
There's nothing in what you've told us that would suggest a problem.
Read this.
it's a very simple macro and I read tons of threads, none of which resolves my case. I get the .Caption line highlighted. I tried to Dim the form as new form and it does not help as well.I have a form which I want to call from a button which is apparently not done correctly from my side.
Sub BtnAdd_Click()
frmAddCB.Caption = "Add Transaction"
frmAddCB.Show
End Sub
Cheers,
When executing this step by step with F8, just before the line that was highlighted on my standard module when executing with F5, it jumps to the Private Sub UserForm_Initialize() sub which is in the Form code itself. In that Initialize sub I had this line of code which was causing the error in Excel 2010:
TransTypeComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("TransType").RefersToRange))
After I changed this to the old-school way with a loop, it started working properly. Probably something about my dynamic range or something about the RefersToRange is not working the same way after Excel 2010.
For Each TT In Range("TransType")
With Me.TransTypeComboBox
.AddItem TT.Value
End With
Next TT
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
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 have one macro, which is called when a cell change occurs. This macro selects images, deletes them, and inserts another image depending on a cell value using the following code. I have the same code for two sheets.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Shapes.SelectAll
Selection.Delete
'insert image code here.
End Sub
In one sheet, it's working perfectly fine and deletes all images, while in the other sheet, it gives me the runtime error "Out of Memory" and highlights the following line:
ActiveSheet.Shapes.SelectAll
Can anyone tell me why this is happening? It works perfectly fine in one and not in the other.
One other thing I want to tell you is it was working fine when I gave this Excel macro to my client; both sheets were working fine. Suddenly after 2 days, he started getting the error on one sheet on which he was working a lot.
I don't know why this is happening. Can anyone tell me what's the reason for this and how I can solve it?
Can you provide your insert image code?
If you are changing the current selection yourself in that code, then this procedure would be called endlessly. You should consider disabling events while processing this event handler as per the following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
' do something
Application.EnableEvents = True
End Sub