Choosing a listbox from multiple list boxes - excel

I am somewhat new to excel vba and I am in search of an answer on how to choose a list box by using a variable.
For instance the code I found is as follows:
Me.ListBox2.AddItem Me.LB_JobList.List(iCtr)
Instead of ListBox2 I would like the 2 to be another number selected by the user from a combo box.
Current code is:
FrameNumber = CMB_FrameNumber.Value 'number selected by user
lb = ("ListBox" & FrameNumber) 'this would = ListBox#
Therefore I would like something similar to
Me.lb.AddItem Me.LB_JobList.List(iCtr)

The comment above is good, but if you want to do something a little safer (in case you have more numbers available than controls, say), you could loop through the available controls and check against their name.
For Each contr In UserForm1.Controls
If TypeName(contr) = "ListBox" And contr.Name = ("ListBox" & FrameNumber) Then
lb = contr
End If
Next

Related

Cycle through a loop in VBA

I have a form in VBA where I want a combo box (cboIncluded) to change its dropdown values according to what's chosen in a different combo box(cboSelectParty).
It provides choices correctly the first time I choose something from cboSelectParty, and if I choose again from either my If or elseif.
My problem is, if I have chosen from either the if or elseif (Fun climb party or climbing party), if won't go back to giving me correct options if I choose anything else.
Private Sub cboSelectParty_AfterUpdate()
cboIncluded.Value = ""
If cboSelectParty.Value = "Fun Climb Party" Then
cboIncluded.List = Sheets("PartyImport").Range("N3:N4").Value
ElseIf cboSelectParty.Value = "Climbing Party" Then
cboIncluded.List = Sheets("PartyImport").Range("O3:O6").Value
Else
cboIncluded.Value = Application.WorksheetFunction.VLookup(cboSelectParty.Value,
Sheets("PartyImport").Range("E2:H37"), 4, False)
End If
End Sub

Update an Excel style in VBA

My Excel macro reads the answers to a survey from a set of Excel files. The answers of a survey contain a score (from 1 to 4) and a description. The goal is to generate a a matrix. Each cell of the matrix has a color that represents the score. I would like the user to be able to modify the layout of these cell. To make it easy to the user, I created a template matrix and a button. The user should be able to modify the layout of the cells and on a click of a button, a set of styles (Score 1, Score 2,...) should be generated. Once the matrix is created, the Workbook should be to function without the survey files.
I have tried a couple of things:
Try 1
ThisWorkbook.Styles.Add "Score 1", BasedOn:=cell1
This gives errors. I don't fully understand when they occur, but one of the causes is when the user modifies the cell layout by selecting another style.
Try 2
ThisWorkbook.Styles("Score 1").Delete
ThisWorkbook.Styles.Add "Score 1", BasedOn:=cell1
This is not a good idea: all cells loose their styling when it is executed a second time.
Try 3: Current
Copy the most frequently used properties of the cells layout and copy them to the style. If this style is deleted by the user, it is recreated. This procedures is not ideal, since most style properties won't be covered.
Is there a way to update a cell style that is more general? I would like there to be as little room as possible to make the workbook in an inconsistent and non-functional state.
I sticked with try 3. Because it required a lot of code for all properties that seemed possible to be edited, and because of copying borders is tricky, I post the result.
'xR1_Template: the cell to base the style on
'nm_Style: the name of the style
Public Function Upsert_Style(xR1_Template As Excel.Range, nm_Style As String) As Excel.Style
Dim xStyle As Excel.Style
Set xStyle = Fn.TryGet(ThisWorkbook.Styles, nm_Style)
If Fn.IsNothing(xStyle) Then
Set xStyle = ThisWorkbook.Styles.Add(nm_Style)
End If
xStyle.Font.Color = xR1_Template.Font.Color
xStyle.Font.Bold = xR1_Template.Font.Bold
xStyle.Font.Name = xR1_Template.Font.Name
xStyle.Font.Italic = xR1_Template.Font.Italic
xStyle.Font.Size = xR1_Template.Font.Size
xStyle.Font.Strikethrough = xR1_Template.Font.Strikethrough
xStyle.Font.Subscript = xR1_Template.Font.Subscript
xStyle.Font.Superscript = xR1_Template.Font.Superscript
xStyle.Font.Underline = xR1_Template.Font.Underline
xStyle.Interior.Color = xR1_Template.Interior.Color
xStyle.Interior.Pattern = xR1_Template.Interior.Pattern
xStyle.Interior.PatternColor = xR1_Template.Interior.PatternColor
'NOTE: necessary to delete all borders first. There's no way to delete them one by one.
xStyle.Borders.LineStyle = xlNone
Dim iBorder As Long
For iBorder = 1 To xR1_Template.Borders.Count
Dim xBorder As Excel.Border
'NOTE: The Borders property claims to work with xlBordersIndex argument, but this is not true.
' Normal indexing is used.
Set xBorder = xR1_Template.Borders(iBorder)
'NOTE: "none-style" borders (=no border), should be skipped.
' Once they are retrieved using the Borders property, they are always visible.
' Setting them with xlLineStyle.xlLineStyleNone does not hide them.
If xBorder.LineStyle <> XlLineStyle.xlLineStyleNone Then
Dim xBorder_Style As Excel.Border
Set xBorder_Style = xStyle.Borders(iBorder)
xBorder_Style.Color = xBorder.Color
xBorder_Style.LineStyle = xBorder.LineStyle
xBorder_Style.Weight = xBorder.Weight
End If
Next iBorder
xStyle.AddIndent = xR1_Template.AddIndent
xStyle.FormulaHidden = xR1_Template.FormulaHidden
xStyle.HorizontalAlignment = xR1_Template.HorizontalAlignment
xStyle.IndentLevel = xR1_Template.IndentLevel
xStyle.NumberFormat = xR1_Template.NumberFormat
xStyle.NumberFormatLocal = xR1_Template.NumberFormatLocal
xStyle.Orientation = xR1_Template.Orientation
xStyle.ShrinkToFit = xR1_Template.ShrinkToFit
xStyle.VerticalAlignment = xR1_Template.VerticalAlignment
xStyle.WrapText = xR1_Template.WrapText
xStyle.IndentLevel = xR1_Template.IndentLevel
Set Upsert_Style = xStyle
End Function

Having trouble using For/Next with variables

I am using an excel Userform and on the form I have created several label fields that are named in sequential order. For example:
Label1, Label2...LabelX
I want to assign the .caption for them from an array that I have built separately. Using a For/Next loop makes the most sense in my head, but since it is a label, I am having trouble. I cannot even get the code to take in a way that makes sense. Below is what I was trying...
For i = 1 To 100
Labeli.Caption = Array(i)
Next i
If there is a way to keep it inside of the loop, it would make the code a lot less cumbersome. Thank you in advance for your assistance.
Try this:
i = 1
For Each itm In Me.Controls
If itm.Name = "Label" & i Then
itm.Caption = Array(i)
i = i + 1
End If
Next
You cannot form an Object Name like Labeli where i is a Variable. But you can loop through all the controls and check if that Label have a Name like Label1 and so on.
Another Method:
For i = 1 To 100
Me.Controls("Label" & i).Caption = Array(i)
Next
What you can to is "build" the name of the Labels like this:
Userform.Controls("Label" & i).Caption
Userform is a Reference to the Userform, if you write it the codebehind of the Userform you can use Me.
But if you have a hundred Labels, there may be better ways to create your Userform, this looks a bit like a XY-Problem

Using a counter in VBA

I'm working making a loop to get data out of a combo form.
Analysis_1 is the first variable
Analysis_1_ComboB is the first ComboBox from the screen
Analysis_1 = Me.Analysis_1_ComboB.Column(0)
Analysis_2 = Me.Analysis_2_ComboB.Column(0)
Analysis_3 = Me.Analysis_3_ComboB.Column(0)
etc etc
as single lines, it is working I do want to work with a loop
for counter = 1 to 9
Analysis_&Counter = Me.Analysis_&Counter&_ComboB.Column(0)
next counter
unfortunately, this is not working, who can help me out here?
Unfortunately, you cannot dynamically specify variable names. (You can usually find a way to dynamically access various objects, especially if they are accessible be a "Name" index.)
The best way to achieve what you want to do is make your variables an array, e.g.:
Dim Analysis(1 To 9) As String
For counter = 1 To 9
Analysis(counter) = Me.Controls("Analysis_" & counter & "_ComboB").Column(0)
Next counter
MsgBox "Value from Analysis_5_ComboB is " & Analysis(5)
(This code assumes that your ComboBoxes are on a UserForm and therefore dynamically accessible via the form's Controls collection.)

Automatically add to list

I'm having a small issue manipulating data.
I have a few check-boxes to select various items on the screen, which in turn inputs relevant data such as item name, price, etc. The issue is that these names have to go onto a invoice, and I cannot get the names to transfer onto the invoice nicely.
The invoice has to record the items, but it needs to check to see if a item has been already applied to the invoice and this is what I'm having problems with.
How can I get items to collectively display on a sheet without any additional interactions (I just click the relevant checkbox instead of manually typing it in), and display them nicely.
The items 'Underlay and Extended Warranty' need to be displayed one after another, and not 2-3 boxes away.
Example:
Underlay Extended Warranty
I don't want this:
Underlay
Extended Warranty
Is there a solution to this?
Update:
This happens if I do it the way I know:
I solved this by making a custom Excel VBA Macro that basically tells us the cell that is free which is closest to the top of the invoice:
Public Function GetApplicableCell() As String
Range("C20").Select
If (IsEmpty(Range("'Invoice Sheet'!C20").Value)) Then
GetApplicableCell = "C20"
End If
If (IsEmpty(Range("'Invoice Sheet'!C21").Value)) Then
GetApplicableCell = "C21"
End If
If (IsEmpty(Range("'Invoice Sheet'!C22").Value)) Then
GetApplicableCell = "C22"
End If
If (IsEmpty(Range("'Invoice Sheet'!C23").Value)) Then
GetApplicableCell = "C23"
End If
If (IsEmpty(Range("'Invoice Sheet'!C24").Value)) Then
GetApplicableCell = "C24"
End If
End Function
It can be called by a =GetApplicableCell() from within the worksheet, and we can work with this to produce a workable solution.

Resources