Dynamic conditions related to radio buttons in excel - vba

How to make the conditions of radio button dynamic? for eg. if i have one radio button "yes" in A100 and the condition related to it is appearing in A103. And, if i am inserting the row i want both(button and its value) to get shifted accordingly .
Sub OptionButton21_Click()
Range("A103") = " Not Applicable "
End Sub
Sub DynamicRange()
'Best used when only your target data is on the worksheet
'Refresh UsedRange (get rid of "Ghost" cells)
Worksheets("Sheet1").UsedRange
'Select UsedRange
Worksheets("Sheet1").UsedRange.Select
End Sub
Thanks in advance

Use a named range
1 -Select the target cell (i.e. A103) and give it a name in the "Name Box" on the top-left of the screen. Suppose you chose the name "MyTargetName"
2- Make the control to move with cells.
Right-click the control --> Format control --> Properties --> Move but dont size with cells
3- In VBA code, refer to the target cell by its name instead of its address:
Sub OptionButton21_Click()
Range("MyTargetName") = " Not Applicable "
End Sub
This will make the referenced target shift correctly, along with the control, whenever new rows are inserted.

Related

Populating a dynamically added listbox from dynamically added combobox selection

The userform has 3 columns, a combobox (list of vendors), and listbox (list of vendors' product / service) and a textbox with the price of the selected product / service.
Example of menu (3rd column item not added)
The user presses a button to add a row, which populates within a scrollable frame a new row of the 3 columns. My goal is to have the listbox's options change according to the selection in the combobox on that row.
I have a listener (see my previous post on combobox(number)_change() to see how this works) which is the following ...
Private Sub ControlHandlerCollection_ComboBoxChange(ComboBox As MSForms.ComboBox)
MsgBox ComboBox.Name
End Sub
I wrote this to test if the change sub would work for ComboBox #87 for example. However, I don't know how to tell the same numbered listbox to change its list options according to the selection of ComboBox#
If I had a listener specifically for ComboBox1_Change() then I'd simply write the change for ListBox1 within it, but I'm writing this for a dynamic 'infinite' amount of ComboBoxes.
My current plan of attack would be to write within the sub in the code box above a block of code to parse the result of ComboBox.Name to grab the number as the result would be for example "ComboBox15" I could write a variable assigned to "ListBox" + (ComboBox.Name - "ComboBox") to call ListBox(number) however I'm not sure I can actually then call a variably based name of a listbox.
OK my father helped me come to this solution. It's a little static, but it works perfectly for what I need.
Had to edit the vendor data into multiple sheets and used the Define Name tool under Data to name the range. The listbox changes as per the selection of combobox.
Dim listBoxName As String
listBoxName = "myList" & ComboBox.Tag
Dim rangeName As String
rangeName = "company_1"
Select Case ComboBox.value
Case "Company 1"
rangeName = "company_1"
Case "Company 2"
rangeName = "company_2"
End Select
Dim listBox As Control
Set listBox = Me.Controls(listBoxName)
listBox.RowSource = rangeName
The above code was added to the private sub mentioned in the original thread.

Checkbox placement issue while filtering data in protected worksheet excel

I have a protected worksheet, with cell protection as both Locked and Hidden. In first column I have Check Box (Form Control) against each row.
Problem is when I try to filter data, there is a extra check box in last row.
I tried linking cell to check box and enabled option 'Move but don't size with cells' but still behavior is same. Is there a way to resolve this?
well seems like checkboxes are bad guys
but there's a prison even for them
make sure your sheet has at least one cell that calculates
make sure each checkbox is completely inside its cell, i.e. its
borders (which appear at selecting it) must entirely fall within the cell
where you chose it to be
set your sheet calculation as "automatic" (Formulas->Calculation Option-> Automatic)
place the following code in the sheet code pane
Private Sub Worksheet_Calculate()
Dim shp As Shape
For Each shp In Me.Shapes
shp.Visible = shp.TopLeftCell.EntireRow.Height <> 0
Next
End Sub

How to reset Excel internal button count?

I have an an Excel sheet that uses VBA to generate some form control buttons on the fly.
the buttons are cleared and then new buttons are created.
I have noticed that even though old buttons are deleted Excel is keeping an internal register of each button. New buttons have button name of over 11K
I don't know if there is some sort of limit excel will allow for this and I don't want to run out of buttons.
I am not sure if this growing registry of buttons past is causing the file size to grow.
I would like to be able to reset the increment back to 0
Anyone have any idea how I can go back to button_0 ? (without starting a whole new Excel sheet)
Seems internal button count is sheet specific. Solution is to copy sheet, rename old sheet, then rename new sheet to old sheet name. Then delete old sheet. Viola! button count reset.
I found the answer somewhere in a forum, but I couldn't retrace it.
You could also pragmatically create the button by a function/sub-routine as a workaround.
Example: below function adds a button and limits the count to a fixed number (in my case, it is the total buttons available).
In my sheet, the first button is named "Button 687" (before I use the macro) and the second button is named "Button 2".
But, it is quite not dynamic when you want to add Drop Down or other form control etc. Macro recording helps you figure out the syntax, methods and properties of the form control you want to add though.
I am not sure why "buttons" is not listed in Properties/Methods after you typed "Activesheet." but Activesheet.Buttons(1).Name or Activesheet.buttons.add are valid codes.
Public Sub Add_Button(ButtonLabel$, ButtonSize#, BFontSize#, BFontName$)
Dim Button__ As Object
Dim One_Unit#: Per_Unit = Application.CentimetersToPoints(1)
'Creates a button at the sheet's first cell (row 1, col 1) with the height and width being 1cm.
Set Button__ = ActiveSheet.Buttons.Add(1, 1, One_Unit, One_Unit)
'button count will be restricted to the number set by user.
With Button__
.Name = "Button " & ActiveSheet.Buttons.Count
With .Characters
.Text = UCase(BLabel)
.Font.Name = BFontName
.Font.Size = BFontSize
End With
End With
End Sub
Sub AddAButton()
Add_Button BLabel:="SAVE"
Debug.Print ActiveSheet.Buttons(ActiveSheet.Buttons.Count).Name
End Sub

Select linked cell, cell next to checkbox, or get the checkbox name

I have a document full of Checkboxes and I dont want to write specific VBA code for each checkbox because the file size needs to stay relatively small. What I am trying to do with my code is when the checkbox is checked, it automatically selects the cells next to it(not hard coded in using "Range") and then perform the rest of the programed VBA function.
How do I either get the name of the checkbox, select the linked cell, or select the cell next to the checkbox using some kind of "offset" property? I am completely stumped!
Thanks for your help in advance.
Use Form Controls instead of ActiveX Controls for Check Box.
Following code will not be work with check box from ActiveX Controls. Also, you need to assign macro to the checkbox, simply trying to run this code from VBEditor will give error.
Assuming all the checkboxes are on same sheet, select all your checkboxes and assign them same macro, something like this
Sub checkBoxHandler()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
MsgBox shp.Name 'Name
MsgBox shp.TopLeftCell.Offset(1).Address ' 1 Rows below checkbox
ActiveSheet.Range(shp.ControlFormat.LinkedCell).Select ' Select linked cell.
Set shp = Nothing
End Sub
here Application.Caller helps VBA to identify which checkbox is being clicked.

Create a macro that would hide and show columns based on selection from a dropdown

I am totally new to the world of macros but have done some VBScript on QTP before. I am looking to create a macro which would hide certain columns depending on the users selection from a drop down. I am unsure of the syntax and how to identify the columns I wish to hide and how to identify the cell with the drop-down.
Here's roughly how I would see it looking -
Sub HideColumns()
If cell(ViewType).Value = "Option 1" Then
Cells(Column_1, Column_2).EntireColumn.Hidden = True
ElseIf cell(ViewType).Value = "Option 2" Then
Cells(Column_2, Column_3).EntireColumn.Hidden = True
ElseIf cell(ViewType).Value = "Option 3" Then
Cells(Column_3, Column_4).EntireColumn.Hidden = True
End If
End Sub
I have named the cell with the drop-down ViewType in Excel - would the VBA recognize the object that way, or would I need to declare it?
How do I identify the columns I wish to hide?
The names you define in Excel are available in Excel as normal variables, so this should not be of any issue.
In your case, I would however suggest using a Switch Case statement. This would look as follows:
Select Case ActiveWorkbook.Names("ViewType").RefersToRange
Case "Option 1"
' Hide Column X
Case "Option 2"
' Hide Column Y
End Select
Also keep in mind that for the macro to be called once you change a cell, you would need to put this code into
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
This Sub has to be placed in the code part of the Sheet itself and will be executed every time a cell is changed in the Sheet.
Let me know if this is enough for you to go on or if you require more help.
I like to use Custom Views when hiding and unhiding columns. Custom views is on the View tab (>=2007) and under the View menu (<=2003). Here's an example:
Hide columns 1 and 2
View - Custom Views - Add - name it "Option1"
Unhide, then hide columns 2 and 3
View - Custom Views - Add - name it "Option2"
Unhide, then hide columns 3 and 4
View - Custom Views - Add - name it "Option3"
Unhide
Create Data Validation in a cell (that doesn't get hidden) and make is a List with "Option1, Option2, Option3"
Name that cell ViewType
Right click on the sheet tab and choose View Code and put the below code in that code pane
Private Sub Worksheet_Change(ByVal Target As Range)
'Only work on the cell named ViewType
'The Me keyword refers to the sheet whose code module you're in
If Target.Address = Me.Range("ViewType").Address Then
'Show the custom view that corresponds to the value selected
'in the dropdown
ActiveWorkbook.CustomViews(Target.Value).Show
End If
End Sub
When the user selection Option1 from the drop down, the Option1 view is shown that hides columns 1 and 2. It's a nice way to manage hiding and unhiding because you can just adjust the Custom View if you ever want to change it, rather than editing the code.