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
Related
I have been running my VBA code on a different computer without flaw, then I get a new computer and when I try to run my code, it says the following "Compile error in hidden module: Sheet4". Then when I press "OK" it takes me to my code and highlights the first ".ComboBox1" part of the following code.
Private Sub ComboBox_Change()
End Sub
Sub SpinButton1_SpinUp()
If Me.ComboBox1.ListIndex = Me.ComboBox1.ListCount - 1 Then Exit Sub
Me.ComboBox1.ListIndex = Me.ComboBox1.ListIndex+1
End Sub
FYI I am running a macro that calls SpinButton1_SpinUp. I haven't changed anything else about the code from the old computer to the new computer so now I don't know how to get rid of the error and be able to run my code properly.
Thank you in advance.
I receive every week a file that has always the same structure and format and what I want to do is to get a message when double clicking in some the cells of one of the columns.
I have the code and it works but I have to paste everytime in the worksheet once I open it. I want make this code "generic" so I can use it automatically everytime I open one of these workbooks without having to copy every time or do anything rather than the double clicking to get the message.
Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, cancel As Boolean)
If Not Application.Intersect(target, myrange) Is Nothing Then
cancel = True
MsgBox "some message"
End If
End Sub
Some comments.. the sheet i need to work with it's always called the same and the column I need to work with is always the same too. The only change is the number of rows it contains.
After some research I don't know if I should do this in a class module or as an addi-in. I'm a begginer in VBA so this is out of my scope...yet.
Thanks!
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.
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 would like to prevent a cell from being edited as I need to preserve the formula in it. The problem is, I want to copy paste a large range of values from another workbook over into my sheet, and the target range for pasting includes some cells which must not change. Thus, a simple lock option wouldn't work, as this would prevent me from doing this operation in one go.
I tried using a code wherein the cell changes back to its intended formula upon change of the cell, however, this yields an infinite loop. See simplified example below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F28")) Is Nothing Then
Range("F28").Formula = "=if(E28=0,0,G28/E28)"
End If
End Sub
Many thanks for your help, much appreciated.
Edit:
An automated solution that would transfer from this other workbook wouldn't work as I receive these files in various formats and workbook names.
If your problem is infinite loop than here goes quite common solution for the problem. You just need to switch off events for a while and than switch them on back. Your code could looks like:
Private Sub Worksheet_Change(ByVal Target As Range)
'turns off events
Application.EnableEvents = False
If Not Intersect(Target, Range("F28")) Is Nothing Then
Range("F28").Formula = "=if(E28=0,0,G28/E28)"
End If
'turns on events back
Application.EnableEvents = True
End Sub
You have to make a Sub that enters the "resident" formulas. Just make a call to this sub after pasting the new values.