Locate and display data using listbox in vba - vba

I have tried this code to locate specific data from excel using List Box in VBA, It populated a list of names from sheet3 range(E7), then everytime I click an item/name on it the program should located the name from sheet3 and display the data on that row into their corresponding textboxes in my userform.But this doesn't work pecisely.Thanks.
Private Sub ListBox1_Click()
Dim isRow As Long
If Me.ListBox1.ListIndex > -1 Then
isRow = Me.ListBox1.ListIndex + 1
End If
Me.Label1 = Cells(sRow, 5)
Me.txt_Mon_in.Text = Cells(sRow,6)
End Sub
Populating data from Sheet3.
Private Sub Userform_Initialize()
Dim vCol As Variant
Dim Lrow As Long
Lrow = Sheets("Sheet3").UsedRange.Rows(Sheets("Sheet3").UsedRange.Rows.Count).Row
vCol = Sheets("Sheet3").Range("E7:E" & Lrow).Value
Me.ListBox1.List = vCol
End Sub

Im not quite sure what you are doing but try the below code
Private Sub Userform_Initialize()
Dim vCol As Variant
Dim Lrow As Long
Lrow = Sheets("Sheet3").UsedRange.Rows(Sheets("Sheet3").UsedRange.Rows.Count).Row
vCol = Sheets("Sheet3").Range("E7:E" & Lrow).Value
Me.ListBox1.List = vCol
End Sub
Private Sub ListBox1_Click()
Dim selectedName As String
Dim i As Long
With ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
selectedName = .List(i)
End If
Next i
End With
Dim c As Range
For Each c In Sheets(3).Range("E7:E" & Sheets(3).Range("E" & Rows.Count).End(xlUp).Row)
If c = selectedName Then
Label1 = Sheets("Sheet3").Cells(c.Row, 5)
txt_Mon_in.Text = Sheets("Sheet3").Cells(c.Row, 6)
End If
Next c
End Sub
the Listbox1_Click() sub will iterate over the column E in sheet 3 and put the name in the Label1 control and will put offset of (0,1) of the found cell into the txt_Mon_in contol.
Sheet3
Userform
Result

Related

recursive tree parsing with vba

Given the following spreadsheet of data: https://ethercalc.org/q7n9zwbzym5y
I have the following code that will parse this and will derive a tree from the parent-child relationships in the sheet. Note that fact that every column occurs twice is because the first instance of the columns is for another type of data, I am only concerned with the populated columns. This is the desired output from the sheet above:
Code:
Sub performanceSheet(someParams)
' Write to "Performance" sheet
Dim w1 As Worksheet, w2 As Worksheet, wsSearch As Worksheet, wsData As Worksheet
Dim num_rows
Dim parent As Range, parentName As String
Dim parentRange As Range, childrenRange As Range
Dim childCount As Long
Dim p As Variant
Dim f1 As Range, f2 As Range
currRow = 8
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Set w1 = wbk.Sheets("PositionsDB")
Set w2 = wbk.Sheets("Performance")
num_rows = w1.Cells(Rows.Count, 1).End(xlUp).row
'If there's no parentName column, we can't continue.
If w1.Rows(1).Find("portfolioName") Is Nothing Then Exit Sub
'find first instance
Set f1 = w1.Rows(1).Find("portfolioName", lookat:=xlWhole)
If Not f1 Is Nothing Then
'find second instance
Set f2 = f1.Offset(0, 1).Resize(1, w1.Columns.Count - f1.Column).Find("portfolioName", lookat:=xlWhole)
If Not f2 Is Nothing Then
'set range based on f2
Set parentRange = w1.Range(f2.Offset(1, 0), _
w1.Cells(Rows.Count, f2.Column).End(xlUp))
End If
End If
'If there's no Root level, how do we know where to start?
If parentRange.Find("Main") Is Nothing Then Exit Sub
For Each parent In parentRange
If Not dict.Exists(parent.Value) Then
childCount = Application.WorksheetFunction.CountIf(parentRange, parent.Value)
Set childrenRange = parent.Offset(, 2).Resize(childCount, 1)
dict.Add parent.Value, Application.Transpose(Application.Transpose(childrenRange.Value))
End If
Next
' Recursive method to traverse our dictionary, beginning at Root element.
Call PerformanceProcessItem("", "Main", dict, w2, 9)
wbk.Sheets("Performance").Columns("A:F").AutoFit
End Sub
Private Sub PerformanceProcessItem(parentName As String, name As String, dict As Object, ws As Worksheet, row_num As Long, Optional indent As Long = 0)
Dim output As String, v
Dim w2 As Worksheet
'Debug.Print WorksheetFunction.Rept(" ", indent) & name
'Debug.Print parentName & name
'write to sheet
ws.Cells(row_num, 3).Value = name
row_num = row_num + 1
If Not dict.Exists(name) Then
'we're at a terminal element, a child with no children.
Exit Sub
Else
For Each v In dict(name)
' ## RECURSION ##
Call PerformanceProcessItem(name, CStr(v), dict, ws, row_num, indent + 2)
Next
End If
End Sub
However, when creating this tree, it gets stuck on an infinite loop of India's, where after recognizing "Cash" as the terminal element of India, rather than exiting that subtree it will create another India and continue until overflow. Is there a logic error in my code? Hours of debugging hasn't worked for me and any input would be appreciated on where I have a flaw in my logic.
I am assuming that "Main" and "Cash" will always be there. If not then we will have to tweak the code little bit. I have commented the code so you may not have a problem understanding it. But if you do, simply ask. I quickly wrote this code so I am sure it can be optimized :)
Option Explicit
Dim sB As String
Dim tmpAr As Variant
Sub Sample()
Dim col As New Collection
Dim s As String
Dim ws As Worksheet
Dim lRow As Long, i As Long, j As Long
Dim itm As Variant, vTemp As Variant
Set ws = Sheet1 '<~~ Change this to the relevant sheet
With ws
'~~> Get Last Row of Col AA
lRow = .Range("AA" & .Rows.Count).End(xlUp).Row
'~~> Store Range AA:AC in an array
tmpAr = .Range("AA2:AC" & lRow).Value
End With
'~~> Create a unique collection of portfolioName
For i = LBound(tmpAr) To UBound(tmpAr)
If tmpAr(i, 1) = "Main" Then
On Error Resume Next
col.Add tmpAr(i, 3), CStr(tmpAr(i, 3))
On Error GoTo 0
End If
Next i
'~~> Sort the collection
For i = 1 To col.Count - 1
For j = i + 1 To col.Count
If col(i) > col(j) Then
vTemp = col(j)
col.Remove j
col.Add vTemp, vTemp, i
End If
Next j
Next i
s = "Main"
For Each itm In col
sB = vbTab & itm
s = s & vbNewLine & sB
sB = ""
GetParentChild itm, 2
If Trim(sB) <> "" Then _
s = s & vbNewLine & sB
Next itm
s = s & vbNewLine & vbTab & "Cash"
Debug.Print s
End Sub
Private Sub GetParentChild(strg As Variant, n As Integer)
Dim sTabs As String
Dim j As Long, k As Long
For k = 1 To n
sTabs = sTabs & vbTab
Next k
For j = LBound(tmpAr) To UBound(tmpAr)
If Trim(tmpAr(j, 1)) = Trim(strg) And Trim(tmpAr(j, 1)) <> "Cash" Then
sB = sB & sTabs & tmpAr(j, 3) & vbNewLine
GetParentChild tmpAr(j, 3), n + 1
End If
Next j
End Sub
This is what I got when I ran it on the data that you provided.

Looping through a 2D array of text boxes

I have a user form having a 100 text boxes. They are arranged 10 x 10 arrays. The text boxes have names
C1_A1 to C1_A10 (first row)
C2_A1 to C2_A10 (second row)
.....
C10_A1 to C10_A10 (tenth row)
How can I loop through the textboxes row by row. The code for the textbox_change() is given below. I successfully implemented this for a form containing 10 boxes. But now I have to scale to a form having 100 boxes and it is no longer practical.
Private Sub C1_A1_Change()
Dim wt As Double
C1_A1.SetFocus
If IsNumeric(C1_A1.Value) Then
wt = CDbl(C1_A1.Value)
If wt >= 0 And wt <= 1 Then
'do nothing
Else
MsgBox "Enter a number between 0 and 1"
C1_A1.Value = vbNullString
End If
Else
wt = 0
End If
End Sub
'an action button to read all values
Private Sub ReadDataTT1_Click()
Me.C1_A1.Value = Range("Wt!E9").Value
............
Me.C10_A10.Value = Range("Wt!N18").Value
End Sub
'an action button to save all values
Private Sub SaveDataTT1_Click()
If C1A1.Value <> "" Then
Range("Wt!E9").Value = C1_A1.Value
............
Range("Wt!N18").Value = C10_A10.Value
End If
End Sub
To use only one single event handler (TextBox_Change Event) for all text boxes you can use a class module.
Add a class module called clsTextBox with the following content:
Option Explicit
Public WithEvents pTbx As MSForms.TextBox
Private Sub pTbx_Change()
Dim wt As Double
If IsNumeric(pTbx.Value) Then
wt = CDbl(pTbx.Value)
If wt >= 0 And wt <= 1 Then
'do nothing
Else
MsgBox "Enter a number between 0 and 1"
pTbx.Value = vbNullString
End If
Else
wt = 0
End If
End Sub
Note that this is the code you used in your TextBox_Change event which we want to apply to all text boxes.
Add the following to your user form to apply the class to your text boxes
Option Explicit
Private mArrClsTbx(1 To 9) As clsTextBox 'change 9 to number of textboxes
Const TbxRows As Long = 3 'change 3 to number of text box rows
Const TbxCols As Long = 3 'change 3 to number of text box columns
Private Sub UserForm_Initialize()
Dim i As Long
Dim iRow As Long, iCol As Long
For iRow = 1 To TbxRows
For iCol = 1 To TbxCols
i = i + 1
Set mArrClsTbx(i) = New clsTextBox
Set mArrClsTbx(i).pTbx = Controls("C" & iRow & "_A" & iCol)
Next iCol
Next iRow
End Sub
To read/save the values you can use a loop similar to the one above writing/reading cells instead of the two Set lines.
Public Sub WriteDataToWorksheet()
Dim iRow As Long, iCol As Long
For iRow = 1 To TbxRows
For iCol = 1 To TbxCols
Worksheets("Wt").Range("E9").Offset(iRow - 1, iCol - 1).Value = Controls("C" & iRow & "_A" & iCol).Value
Next iCol
Next iRow
End Sub
Public Sub ReadDataFromWorksheet()
Dim iRow As Long, iCol As Long
For iRow = 1 To TbxRows
For iCol = 1 To TbxCols
Controls("C" & iRow & "_A" & iCol).Value = Worksheets("Wt").Range("E9").Offset(iRow - 1, iCol - 1).Value
Next iCol
Next iRow
End Sub

How can I modify this code to count the same object in a normal Microsoft word table

Following code is for counting the number of checked checkboxes in a formfield. How can I modify it to count the same object in a normal table, and not a form field?
Private Sub CommandButton2_Click()
Dim i As Long, j As Long, k As Long
k = 0
With ActiveDocument
With .Tables(1)
j = 3
For i = 1 To .Rows.Count
If .Cell(i, j).Range.FormFields(1).CheckBox.Value = True Then
k = k + 1
End If
Next i
i = .Rows.Count
End With
End With
MsgBox k & " instances were found"
End Sub
use the ContentControls property of the Range object to return a ContentControls object and exploit it
here's some examples of how to count checkboxes or checked checkboxes in a table or a in a single column of it:
Option Explicit
Sub main()
With ActiveDocument
MsgBox CountCheckBoxes(.Tables(1)) & " CheckBox instances were found"
MsgBox CountCheckedCheckBoxes(.Tables(1)) & " checked CheckBox instances were found"
MsgBox CountCheckBoxesInColumn(.Tables(1), 1) & " CheckBox instances were found in column 1"
MsgBox CountCheckedCheckBoxesInColumn(.Tables(1), 1) & " checked CheckBox instances were found in column 1"
End With
End Sub
Private Function CountCheckBoxes(table As table, Optional col As Variant) As Long
Dim cc As ContentControl
With table
For Each cc In .Range.ContentControls
If cc.Type = wdContentControlCheckBox Then CountCheckBoxes = CountCheckBoxes + 1
Next cc
End With
End Function
Private Function CountCheckedCheckBoxes(table As table) As Long
Dim cc As ContentControl
With table
For Each cc In .Range.ContentControls
If cc.Type = wdContentControlCheckBox Then If cc.Checked Then CountCheckedCheckBoxes = CountCheckedCheckBoxes + 1
Next cc
End With
End Function
Private Function CountCheckBoxesInColumn(table As table, col As Long) As Long
Dim i As Long
With table
For i = 1 To .Rows.count
CountCheckBoxesInColumn = CountCheckBoxesInColumn + .Cell(i, col).Range.ContentControls.count
Next i
End With
End Function
Private Function CountCheckedCheckBoxesInColumn(table As table, col As Long) As Long
Dim i As Long
With table
For i = 1 To .Rows.count
CountCheckedCheckBoxesInColumn = CountCheckedCheckBoxesInColumn + CountCheckBoxesCheked(.Cell(i, col).Range)
Next i
End With
End Function
Function CountCheckBoxesCheked(rng As Range) As Long
Dim cc As ContentControl
With rng
For Each cc In .ContentControls
If cc.Type = wdContentControlCheckBox Then If cc.Checked Then CountCheckBoxesCheked = CountCheckBoxesCheked + 1
Next cc
End With
End Function

inputting multiple rows in a textbox and displaying each value in a different row of a single column

I am trying to input multiple rows in a textbox and display each value in a different row of a single column in excel, My code inputs all values in different rows of the column. Code is as follows
Private Sub CommandButton1_Click()
Dim i As Variant
For Each i In Split(TextBox1.Text, vbCrLf)
With Range("A1")
lastrow = ThisWorkbook.Worksheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1
ThisWorkbook.Worksheets("sheet1").Range("A" & lastrow).Value = TextBox1.Value
End With
Next
TextBox1.Text = ""
End Sub
Any suggestions please? your response will be highly appreciated
if I understand it correctly you want to have each line in the text box in its own row so this would work
Private Sub CommandButton1_Click()
Dim i As Variant
Dim rowCounter As Long
rowCounter = 1
For Each i In Split(TextBox1.Text, vbCrLf)
'start at row 1 column A
Cells(rowCounter, 1).Value = i
rowCounter = rowCounter + 1
Next
TextBox1.Text = ""
End Sub
There is no need to iterate. Split returns an array that you can use to fill all the rows at once.
Private Sub CommandButton1_Click()
Dim Target As Range
Dim Data As Variant
If TextBox1.Text = "" Then Exit Sub
Data = Split(TextBox1.Text, vbCrLf)
With Worksheets("sheet1")
Set Target = .Range("A" & .Rows.Count).End(xlUp)
If Target.Value <> "" Then Set Target = Target.Offset(1)
Target.Resize(UBound(Data) + 1).Value = Application.Transpose(Data)
End With
TextBox1.Text = ""
End Sub

Loop Through Userform & Paste to Offset Cells

It's me again!
I am trying to input data into a database with a userform by looping through each control and pasting it into a cell via an offset with a counter. I am getting an error on the line which actually inputs the data to the cell and cannot figure out how to do this via a loop. It would be easy to do it field by field but I do not want to write that many lines of code.
Here is my most recent attempt:
Option Explicit
Sub cbSubmit_Click()
' Set worksheet
Dim dbFood As Worksheet
Set dbFood = Sheets("dbFood")
'Set last row and column
Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim lCol As Long
lCol = Cells(1, Columns.Count).End(xlLeft).Row
'Define idCell as Range type
Dim idCell As Range
' If no records exit, add first record
If Cells(lRow, 1).Value = "ID" Then
Set idCell = dbFood.Range("A2")
idCell.Value = 1
' Add Data
Dim ufControl As Control
Dim Counter As Long
Counter = 1
For Each ufControl In Me.Controls
If TypeOf ufControl Is MSForms.ComboBox Or MSForms.TextBox Then
idCell.Offset(0, Counter).Value = ufField.Value
Counter = Counter + 1
End If
Next ufControl
MsgBox "Added to database!"
' Else add next record
ElseIf Cells(lRow, 1).Value >= 0.1 Then
Dim lastID As Long
lastID = Cells(lRow, 1).Value
Set idCell = dbFood.Cells(lRow + 1, 1)
idCell.Value = lastID + 1
' Add Data
' If none of the above display ERROR and exit sub
Else: MsgBox ("ERROR - Cannot Create Record")
Exit Sub
End If
End Sub
If anyone could help me figure out how to solve this one then great!
I managed to solve this by using the method Kathara suggested to me but edited it to avoid the 438 error. Below is the small adjustment I made to make it work:
For Each ufControl In Me.Controls
If TypeOf ufControl Is MSForms.TextBox Then
idCell.Offset(0, Counter).Value = ufControl.Text
Counter = Counter + 1
ElseIf TypeOf ufControl Is MSForms.ComboBox Then
idCell.Offset(0, Counter).Value = ufControl.Text
Counter = Counter + 1
End If
Next ufControl
Many thanks for your help :)
I saw some things that I have adapted down below. May I ask you to test that bit of code?
Option Explicit
Sub cbSubmit_Click()
Dim dbFood As Worksheet
Set dbFood = ActiveWorkbook.Sheets("dbFood")
Dim lRow As Long
lRow = dbFood.Cells(dbFood.Rows.Count, 1).End(xlUp).Row
Dim lCol As Long
lCol = dbFood.Cells(1, dbFood.Columns.Count).End(xlLeft).Row
Dim idCell As Range
If dbFood.Cells(lRow, 1).Value = "ID" Then
Set idCell = dbFood.Range("A2")
idCell.Value = 1
Dim ufControl As Control
Dim Counter As Long
Counter = 1
For Each ufControl In Me.Controls
If TypeOf ufControl Is MSForms.TextBox Then
idCell.Offset(0, Counter).Value = ufControl.Result
Counter = Counter + 1
ElseIF TypeOf ufControl Is MSForms.ComboBox
idCell.Offset(0, Counter).Value = ufControl.SeletedItem.Value
End If
Next ufControl
MsgBox "Added to database!"
ElseIf dbFood.Cells(lRow, 1).Value >= 0.1 Then
Dim lastID As Long
lastID = dbFood.Cells(lRow, 1).Value
Set idCell = dbFood.Cells(lRow + 1, 1)
idCell.Value = lastID + 1
Else
MsgBox ("ERROR - Cannot Create Record")
Exit Sub
End If
End Sub
As you can see I have divided the types of the ufcontrol as I am not sure that with a combobox you can directly say .Value so you'll have to add .SelectedItem. You can at least try it once :)