Excel thinks Combo Box is clicked when sheet is calculated in VBA - vba

I have a number of private subs in Sheet code that refer to ComboBoxXX_Click. If I run another macro in the same Sheet that has SheetX.Calculate anywhere in the macro (referring to the sheet that contains the ComboBox) in the VB Editor using the step-into function, when it comes to execute the .calculate line it jumps to the ComboBoxXX_Click Macro. This makes me think that Excel is thinking that the ComboBox is clicked when the sheet is recalculated.

Try a different option instead of ComboBoxXX_Click
I suggest looking at the list of events that can be triggered on MSDN for that control.
try MouseDown

If you could add more information and blocks of code to help us help you. Also have you stepped through your code to find out at what point it is calling the ComboBoxXX_Click?

enter image description here
Here is an example of what I mean. Once the step into hits the Sheet4.Calculate on the first macro it skips to the second one (ComboBox25_Click) and if I step into the second one, once it reaches the Sheet4.Calculate line it also skips to a random ComboBoxXX_Click Macro.

Related

vba , Autofill userform from another workbook cell selection

I have a workbook that used to be one with many sheets, I have now split the sheet to different workbooks.
The problem I now have is userform population from cell selection. When all the sheets were together. This code worked great.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (UFMJobSelectForm.ActiveControl Is Nothing) Then
Call UpdateJobSelectForm
End If
End Sub
However now the Userform is in one workbook and this code is in another. I don't want to reference library as I need it to open and close so it can be accessed for other people to use.
Thanks for any help in advance.
Edit:
What i have is 4 different workbooks with jobs on and i want to select the job and the userform populates with job details.
The code I have detects the userform is open and then calls then populates the userform using updatejobselectform. Which worked when all the sheets were in the same workbook. however no longer works now i have separated them.
When i run this code now the sheets are in there own work books i get Error: runtime erroe 424 object required.
So what i am asking is dose anyone know how i can check a userform is loaded from in a different workbook and how i can get the useform to interact with cell selection from a different workbook.
thanks again.
This looks like a code that primafacie must fire when selection change event occurs, and call a function that must update the said form.
If the code and the said form are dependent, while splitting up should they not be together.
In case there are some other constraints that do not let you do so, and are not defined in the question do feel free to update the question / comment below.
Happy to help!

Sheet.Activate changes sheet but continues to edit data on previous sheet

I have a dialog box with a couple of buttons that launch macros to activate and change to different sheets.
The problem I am having is after I click the button, the macro activates the new sheet and I see it. But when I go to delete data, add data or try to delete a row "Nothing happens" the data on the screen is still there. If I go back to the previous sheet, the cells and rows that I had intended to delete were deleted in that sheet. It is very wierd and never seen anything like that. It appears that my macro code is note doing enough to actually change to the new sheet. I do not have this problem if I click a different sheet tab to change to it. Or if I click the dialog button to go to the new sheet and quickly do a ctrl-pgDown and Ctrl-PgUp to change from another tab and back that seems to fix the problem.
This is the code in my macro I am using to try to change to the desired tab.
Private Sub Report1Button_Click()
On Error GoTo Handler
Sheets("Report1").Activate
If StayOpenBox.Value = False Then
Unload MainMenu
End If
Exit Sub
Handler:
MsgBox "Sheet 'Report1' not found or renamed"
End Sub
Thanks for any help or suggestions
UPDATE:
Here is code that I use to call the dialog box. I have a shape on the other sheet that is assigned to this macro to open dialog box
Sub ShowMainMenu()
With UserForm1
.Show
End With
End Sub
Also there is no further code to make edits to the new sheet. My Button click simply switches to the other sheet and when I attempt to make edits manually, they are actually done on the previous sheet which is not the one I am currently looking at. So anything I do, Bold text, delete text, delete row, etc, is not done on the current sheet I am looking at, but when I return to the previous sheet the changes where made there. Im on Excel 2013, I have reproduced this problem in 2 separate files, but I will try on a different computer and older version of excel. Screenshot of my situation is below.
UPDATE 2:
I ran this xlsm file on a 2nd computer with Xls 2007 and was not having the problem. So I ran the macro on a 3rd computer that also has Excel 2013 and it is experiencing the same problem. So it is not computer specific and seems to be a problem in XLS 2013 but not in XLS 2007. I will try to find a computer with Excel 2010 to test as well, but something about this code is causing a problem in 2013 but not in older versions of excel.
When you run VBA code, it will default to using the ActiveSheet if you don't define the Sheet. When you have objects/methods that you want performed on a specific sheet, you should always specify! You can do that one of two ways:
Sheets("Report1").[Object].[Method]
'or
Sheets("Report1").[Method]
or you can pass the Sheet name to a variable and use that for shorter code
Dim Report1 As Worksheet
Set Report1 = Sheets("Report1")
Report1.[Object].[Method]
'or
Report1.[Method]
Try changing the sequence of lines in your code. I had a similar situation and it turned out that I inserted the "delete sheet" code in between the commands that were copying data from sheet1 to sheet2. When I put the deletion lines (commands) after I finished copying sheets everything started to work correctly. Many commands activate one sheet while performing and this immediately disactivates another sheet. So if you used "ActiveSheet" sommand somewhere it may be incorrectly understood - the command may be executed not on the sheet you meant.
Just use the full address for the range you are trying to manipulate, for example instead of:
Sheets("mySheet").Activate
Range("A1:B10").Cut
Sheets("myOtherSheet").Activate
Range("A1:B10").Paste
use:
Sheets("mySheet").Range("A1:A10").Cut Destination:=Sheets("myOtherSheet").Range("A1:B10")
I just had the same problem, also with Excel 2013. So even if the thread is over 9 month inactive, I want to share my solution in case somebody gets here through a Google search.
The solution was really simple. Call the userform with:
UserForm1.show vbModeless

How do I run VBA code on ListObject Change?

I know that I can write the following code in a sheets' VBA-object to run code on a worksheet change.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Is there anything similar that I can write to run code whenever I filter a certain ListObject?
Only in some cases. Say we include a new column in the table that we fill with the value 1. Elsewhere we insert an
=SUBTOTAL()
formula to sum that column. As the filter is operated the number of visible rows will vary. The SUBTOTAL() function will re-calculate.
At this point a Calculate Event macro would catch the re-calculation!
Try this...
I added a ListBox Form Control to my Excel sheet..
Next, I assign a macro to this object by Right Clicking and choosing "Assign Macro". (you may need to be in "Design Mode" to make this happen -- check the Developer Ribbon)
By default - the Macro Name is populated with a change event macro name. Click "New" to create the macro.
Your macro is added to a Module. Hope this helps!

Copy data from one part of a userform to another automatically

I am trying to work with an Excel form using VBA that I have customized (I found the original on the web).
I have two tabs: one is called Planning-Deleted, the other is called Planning-Deleted Data. All the data that I enter in the first tab is copied to the second tab when I press a command button called Add to Database.
Is it possible to skip the command button altogether and record the data entered in cell D5 and D6 directly in the second tab and clear these cells for the next records? I have no knowledge of programming and I would appreciate as much clarity and specificity as possible.
If more details are needed, please let me know.
You'll need some trigger to cause the copy operation to happen, for example:
Using the 'Change' event, you can trigger some action any time some value is entered into a sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
Sheet2.Range(Target.Address).Value = Target.Value
End Sub
This code will copy any data entered into the first sheet into the same cell on the second sheet. 'Target' in this case is the cell where data was entered.
I'm not sure if this what you want though, since you also want to clear the cells after the copy. Can you describe when the copy-and-erase should occur, if not from clicking a button?
Also, your question doesn't seem to match your question's title. Can you clarify a bit what you are trying to accomplish?

How to shell to an exe and pass cell values from Excel worksheet

Is this possible in Excel?
I have a workbook with multiple worksheets. I wrote some vba code in a code module to shell to an exe and pass it cell values as arguments.
What I want to be able to do is select a cell or row in any of my worksheets and then call my Shell Sub while passing the values from a couple cells to the Sub. A hot-key combination would be best.
The part I am having trouble with is calling a sub in a code module from a hot hey.
Can you help with this? Any sample code?
Much appreciated!
Found my answer! It was in a post from Leith Ross found here...
http://www.excelforum.com/excel-programming/590157-keyboard-shortcut-to-run-sub-module-in-vb.html
If your macro is a Sub then its easy to assign a shortcut key to your macro. While in Excel, type ALT+F8 to bring up the Macros List. Select the name of your macro by clicking it. At the bottom right corner of the dialog, click "Options..". You can then assign a shortcut key and add a description of the macro for other to see when it is selected in the Macro List.
That's exactly what I needed.