I have a background for my VBA project which contains pictures. These pictures I would like to make clickable, in the way that the click triggers a click event. I've tried adding pictureboxes, buttons and labels but I couldn't manage to make them invisible without making the click event not work (setting the Visible property to false causes this). Googling the problem, most solutions made the button look transparent by changing the color and borderstyle so the control appeared to be a part of the mono color background. I however have a specific picture that I would like to see beneath the button.
To add a macro manually
Right-click on the picture you want to assign the macro to.
Choose Assign Macro from the options.
Assign the macro that you want to run when the picture is clicked.
Click OK to finish.
To add a macro programmatically
If the macro is in the same workbook:
ActiveSheet.Shapes("Picture 1").OnAction = "ThisWorkbook.test"
If the macro is in a different workbook:
ActiveSheet.Shapes("Picture 1").OnAction = "'Book1'!test"
For more examples, look at this and this.
Related
I can make it popup on opening the document, but what if I close the userform and want to it to show up again in the same document? I know in excel you can add a button onto a worksheet directly and make it show the userform on click, but this button is unavailable for microsoft Word. Is there a solution besides initiating the script by hand?
To display a VBA userform, you can trigger it from a macrobutton field, from a form field, from a shape, from a QAT button or from an ActiveX button. There are probably a couple of other methods I'm not remembering at the moment. Each is a little different in the exact steps, but all will run the command:
UserFormName.Show
I'm having a problem with radio option buttons on a UserForm I have created in excel. When the UserForm is loaded it is defaulted to OptionButton1. If I toggle to OptionButton2 and then toggle back to OptionButton1 excel will be unresponsive and crash when I close the form. If I just toggle to OptionButton2 and close the form or don't toggle them at all, everything will be fine. It only happens if I toggle more than once in a given runtime. Also, the option buttons are on a frame on the userform if that makes any difference.
I have commented out ALL code that is triggered by events attached to the controls and it still happens. Also, the code works perfectly fine in the case that I only toggle the options once. It will crash simply by changing the option to 2 and back to 1 without executing any other code. I have another option button group on the form and I don't have any issues with that group. I can toggle them as many times as I want.
I'm not able to share my actual workbook and have been unsuccessful recreating the problem in an example workbook. I'm not sure what else I can provide that would be of use. If anyone has any suggestions based on my description of the problem, it would be greatly appreciated. I have run out of ideas.
Things I've tried:
Removing all code associated with the controls
Changing and removing the group name for the set of option buttons
deleting the option buttons and remaking them
changing the names of the option buttons
copying and pasting the existing working option buttons and renaming them
EDIT: I was able to save as on my original workbook and delete everything out except the working and broken option controls. Here is a link to the sample book which is a bare skeleton of my actual workbook:
Link to workbook example.
Even with everything gone, the problem still happens. If you open the userform, you can toggle between the "Option A" and "Option B" buttons, but if you try to toggle between "Option 1" and "Option 2" excel will crash when you close the form.
Your User-Form and /or controls are corrupt. In line with what Commitern suggested, instead of full export and recreation, try this:
1.Delete your faulty user form. in this case `Test`.
2.Save and close Excel.
3. Go to C:\Users\<<yourusername>>\AppData\Local\Temp\VBE
4. Here delete all the exd files.
5. Re-open workbook add new form, name it `Test` and all option buttons.
All works fine.
Remember if you try to add a new user form and rename it as the old one Test without saving the file, you will get error.
https://support.microsoft.com/en-us/kb/244238
I have an excel toolbar created by VBA macros. When cell is in edit mode, I'm unable to perform any toolbar actions, the event itself is not getting triggered. Button events are getting triggered when we come out of that editing cell.
Is there any way to handle this? I want to allow users to click on buttons even when cell is in editing mode.
Thanks.
VBA will never run if a cell is in edit mode. If you are typing inside a cell, VBA is halted. Infact, if you press ALT + F11 and bring up the VBA editor window, and then go to edit a cell in Excel, you'll find you cant ALT + TAB back to the editor, it's gone. You cant even get back to the editor whilst you are in edit mode. (Obviously when you click outside Excel, the cell is no longer in edit mode, so you can select it from the windows tray)
You may have to use a form to allow users to edit cells, this way VBA will still run whilst the form text box is being typed in. Then submit it to the cell when the text box is complete
With one of my event handlers (double click cell contents in a gridview) I bring up another form that gives a summery of the contents of the row that was clicked...
When doing this, I set the original form to disabled, so that the user cannot interact with it until the summery form is closed.
Problem is that I would like to keep my labels on the original form white, but being that they are disabled as well.. they turn grey, as they are supposed to.
I have tried to set their color with the event(enabled has changed) But my half-witted solution didn't get me the results that I was looking for..
Sounds like what you're actually after is a modal form. This forces the user to only interact with the new form you've opened (within your application) but they can still read what is behind. The following link is a good start:
http://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx
I have a .doc with a command button (cmdStart) which opens a form.
After populating the form I click a button to close the form and populate the .doc.
I want to hide the initial cmdStart on the .doc as well when the form closes,
Ive tries document.shapes(1).visible=false and cmdStart.visible=false but none seems to work.
Any ideas?
thanks
(ps I cant just opne the form from the autonew, I need the cmdStart visible to begin with)
You have several options to deal with that. However, you won't be able to hide your command button but you will be able to remove it.
Removing a command button can be done by the following code:
Private Sub CommandButton1_Click()
CommandButton1.Select
Selection.Delete
End Sub
(Note that usually you would be able to hide text in Word by setting the font hidden, e.g. by calling Selection.Font.Hidden. However, this does not affect controls.)
If you deleted the button and you need it later on again you will have to re-create it. In that case it might be a good idea to mark the position of the button with a bookmark.
Another option would be to use a MACRO button field. Such a field can be inserted from the Insert Field dialog and can be used to launch a macro.
If you really wanted to "hide" the button, you could set the Height and Width to 0.75 and it's virtually gone. Then resize back to "show". I've also seen people put them inside tags and hide the tag. Hope this helps