VBA - Including a variable within the Activex Option Button naming - excel

I have an Order Sheet which includes a combination of check boxes and also option buttons.
There are 10 rows of 3 option buttons and 1 checkbox. Each row is grouped together.
The check boxes work fine and I am able to refer to them using a variable i which refers to the row number.
I have tried to do the same for the Option buttons. I.e I am trying to refer to the option buttons using variable i. However, I have been unable to find a solution.
This is the code I am using to refer to the check-boxes using variable i. This works fine.
If Sheets("Order Sheet").Shapes("Check Box " & i).ControlFormat.Value = 1 Then
I have used a similar logic to try and refer to the option buttons. However, all variations of my trial and error based on vaguely similar things that I have found online have failed.
If OLEObjects("OptionButton" & i + 30).Object.Value = True Then
The current error with the code snippet above is "Sub or function not defined". Highlighting the OLEObjects line of code.
Any help or direction on where to go with including a variable in the naming of the option buttons would be greatly appreciated.

You need to specify in which worksheet your OLEObjects is:
ThisWorkbook.Worksheets("Sheet1").OLEObjects
Also see the documentation: OLEObjects object.

Related

Excel VBA refer to worksheet that uses icon/emoji as part of name

I'm using Excel for Office 365 MSO 64-bit.
I want to write a VBA macro that selects different worksheets in a workbook based on the worksheet's name.
For example, I have two lines of VBA code that activate a workbook and then select a specific sheet in the workbook by the sheet's name.
Windows("myworkbook").Activate
Sheets("mysheet").Select
However, I have to work with some sheets that contain icons or emojis in them. For example, there is a worksheet that has this name: "🚑 Patient".
If I try to paste the icon/emoji into VBA like this: Sheets("🚑 Patient").Select, the icon does not show up in the VBA editor. Instead, I get Sheets("????? Patient").select.
I have also tried to use ChrW() to encode? the ambulance character (see here: https://www.compart.com/en/unicode/U+1F691)
When I run this macro below), I get an invalid procedure call or argument as noted below.
Sub SelectWeirdSheet()
Windows("MYWorkbook.xlsx").Activate
x = ChrW(128657) ' get invalid procedure call or argument here
Sheets(x & " Patient").Activate
End Sub
I also tried code for ambulance... also tried ChrW(&H1F691), but I get the same error.
My suspicion is that I am using the wrong argument for ChrW(), but I'm lost.
edit: So, the docs say that my argument for ChrW() is out of range. That helps explain the error, but I'm still missing a work-around.
Question: Is there a way to refer to use VBA to select worksheets that have an icon/emoji as part of their name?
I know you can also refer to worksheets by index number like this Sheets(3).Select.
However, there will be instances where I don't know the index of the sheet ahead of time, but I will know the name of the sheet, so it is preferable for me to call the worksheets by name.
Thank you.
In addition to the self-answered response, when working in a single workbook, the coder can assign a CodeName to the sheet in the VBA IDE, and then use that CodeName directly. This is really only valid if the Sheet is not re-created (i.e. is a permanent sheet in the book) at any stage, because a new/copied sheet will be automatically given a new CodeName by Excel.
For example, if given the CodeName shtPatient (see picture bellow), the code could be:
Sub SelectWeirdSheet()
' Windows("MYWorkbook.xlsx").Activate '<-- this approach has limitations
shtPatient.Activate ' See my comment below about the limitation - this will not work as expected in this example.
End Sub
Note: https://stackoverflow.com/a/10718179/9101981 explains why not to use Activate, but I have left the code as-is for the purposes of this answer. Also look at Using Worksheet CodeName and Avoiding .Select & .Activate. Another limitation noted is that the CodeName is only valid for the workbook that the code is in - so may not be applicable in this case.
I have highlighted the CodeName parts of the IDE in the image below, see how "Test Patient" is not called "Sheet7", but instead has a meaningful name that I gave it in the properties window below.
In order to properly address the emoji, it should be split into two separate unicode characters.
In this case, it would be x = ChrW(&HD83D) & ChrW(&HDE91)
Those two unicode characters make up the ambulance emoji.
So, this Macro now works.
Sub SelectWeirdSheet()
Windows("MYWorkbook.xlsx").Activate
x = ChrW(&HD83D) & ChrW(&HDE91)
Sheets(x & " Patient").Activate
End Sub
Found the solution on reddit of all places https://www.reddit.com/r/excel/comments/6pq1r1/vba_how_can_i_write_emojis_using_chrw/

Fill cell from userform (one out of several checkboxes)

I am sure someone else has asked this, but I can't find a post with an answer that fits my situation. I am trying to fill and excel sheet from a userform. The first cell will be filled with one of several choices that are available. Each choice is allotted its own checkbox. This is due to the format of the paper form that my database is based off of. The picture shows the separate checkboxes. How would I go about this? And is it possible to ensure that only one checkbox is used at a time?
Any assistance will be appreciated.
Please disregard this question. I found a simple stacked method of achieving my goal here. I used this example I found on a forum and tried stacking more than one. It worked, but only one can be checked at a time to create a record.
If "X" = True Then
Sheets("Y").Cells(emptyRow, 1).Value = ("Z")
End If
X = Checkbox Name
Y = Destination Sheet
Z = Checkbox Caption (or other desired caption)
If more than one is checked, the form will spit out the following error:
"Run-Time Error '9':
Subscript out of range."

Can't use more than one combo box on excel

I have made two active X combo boxes for a list of 220 sites. Before making the active x combo box i have made a sheet that searches through my data and finds the sites that match with what i am typing as i go.
I then used the name manage, refering to the formula in the first cell of the list
=Sheet1!$G$2:INDEX(Sheet1!$G$2:$G$220,COUNTIF(Sheet1!$G$2:$G$220,"?*"))
I have then writen this in the ListFillRange in the properties of my combo box.
It works fine for one, but once i had made the second one and selected the site the first one will no longer let me search through.
I have used the same formulas on both but they originate from different sheets to see if this fixed the problem however that was unsuccessful. (the boxes are on different sheets) When i click on the next sheet after using the box on the first sheet, it still shows part of it as if it is crashing.
The boxes are independent so I'm not sure what to do next as i need to add another 3 on separate sheets.
I am also using this code for each box
Private Sub ComboBox1_Change()
ComboBox1.ListFillRange = "MList"
Me.ComboBox1.DropDown
End Sub
and similar for the other button but with a different range.
There is no need to use VBA for this, the Change Events specifically, if you just want to use and fill the combo boxes with Named Ranges.
The scenario I think you try to do is:
Create Named Ranges that will be the source of your combobox:
Fill the range with your data, select the range, Right Click, Select Define Name and give the range a name. MList in your case I believe.
Create Combobox:
Goto Developer Tab, Insert in your case ActiveX ComboBox, Draw it on your sheet, right click the ComboBox, select properties, find ListFillRange in properties and enter the name of the Named Range you created in step one
Repeat for Combobox 2, with the same or a different Named Range depending on what you try to do
Leave Design Mode
Boths Comboboxes can now be used to type in what you are looking for.
If this is not what you tried to do, please try edit your question and in detail try to explain what you try to do and what you like to accomplish by doing so.

Excel 2007 - Capture OWC11 Spreadsheet Control's Right-Click Context Menu Selection

I know this is probably simple, but I have spent 2 hours Googling for the answer and I can't find it. I am using the Office Web Components 2003 (v11) Spreadsheet Control on an Excel 2007 userform. I am using the BeforeContextMenu event to create a custom context (right-click) menu. I'm using the code sample from the Spreadsheet Control's help file to create the menu. The code is:
Sub Spreadsheet1_BeforeContextMenu(x, y, Menu, Cancel)
Dim cmContextMenu(4)
Dim cmClearSubMenu(2)
cmClearSubMenu(0) = Array("&All", "ClearAll")
cmClearSubMenu(1) = Array("&Formats", "ClearFormats")
cmClearSubMenu(2) = Array("&Values", "ClearValues")
cmContextMenu(0) = Array("Cu&t", "owc2")
cmContextMenu(1) = Array("&Copy", "owc3")
cmContextMenu(2) = Array("&Paste", "owc4")
cmContextMenu(3) = Empty
cmContextMenu(4) = Array("Clea&r", cmClearSubMenu)
Menu.Value = cmContextMenu
End Sub
The custom menu appears perfectly when the spreadsheet is right-clicked. What I can't figure out is how to capture the menu item that I select. Can someone please point me to VBA code that will enable me to capture and use the selection?
Thanks In Advance -
After more Googling I found the answer to this problem in the Help for the OCW11 Spreadsheet Control. The BeforeContextMenu event lists the code shown in my original post. I could see that the menu items were defined with statements like:
cmContextMenu(0) = Array("Cu&t", "owc2")
The "Cu&t" is the menu item label, and the "owc2" is the constant (I assume) that is supposed to execute the Cut command (commands are known as "OCCommands" in OWC11). The problem is that the "owc2" constant wasn't firing the Cut command. I checked the "Programming Information" section of the Help, underneath "Enumerations," in the entry titled "OCCommandID" for the OCCommand ID constants. That's exactly where they are located. I looked up the Cut constant, which is 1001, and replaced the "owc2" with 1001, and the Cut command worked perfectly! I did the same thing for the "&Copy" and "&Paste" commands, with 1002 and 1003, respectively, and they worked as well.
My problem may be that I didn't set up the proper references to the OWC constants. Or it could be that the constants changed and weren't updated correctly in the Help documentation. Whichever may be the culprit, the above method fixed the problem.

How to get the index of a combo box in Excel spreadsheet by using VBA?

I have an Excel 2003 file. In the first sheet, I put a combo box. After that, I assign a macro to handle the change event. The macro is located a module in the VB Editor's Project Explorer box. In the macro, I want to write some lines to get the index of the combo box's selected item index. What should I write?
I wrote the line
If ActiveSheet.cbFilter.Index = 1 Then
but it kept raising error (the name of the combo box is "cbFilter"). The error message is "Object doesn't support this property of method". Please help me.
Many thanks in advance,
Haris
Try removing the "ActiveSheet." part of the statement, my guessing is the combo is already inside that sheet.
Source: Experience
Regards
have you added items to your combo list?
try these pieces of code in your debugger: first call Preload() once, then select anything from the combo, then run Readout() .... this should give you a hint.
Sub Preload()
ActiveSheet.ComboBox1.AddItem "111"
ActiveSheet.ComboBox1.AddItem "222"
ActiveSheet.ComboBox1.AddItem "333"
End Sub
Sub ReadOut()
ActiveSheet.[A1] = ActiveSheet.ComboBox1.ListIndex
ActiveSheet.[A2] = ActiveSheet.ComboBox1
End Sub
also you should check that you have created a reference to MSForms 2.0 Object library (FM20.DLL - or similar)
EDIT:
I just tested the case of empty Combo ... index will be -1
Try this. If you look into Object brower cbFilter object is property of sheet1 and can be referred as below from module.
MsgBox Sheet1.cbFilter.ListIndex
If Sheet1.cbFilter.ListIndex = 1 Then

Resources