VBA get dynamically names of component? - vba

I am newbie in vb and I am trying to write simple programs in Excel 2010.
I have a multipage with customers(cutomer1, customer2, ..., customer20)
For each customer I have 10 users "labels" with their textboxes.
The textboxes are filled from a sheet page customer1 B9:B18 , customer2 D9:D18, etc ...
Is there any way to get dynamically the names and the values in order to avoid code like the following ?
CUSTOMER1_USER1_TEXTBOX.Value = Sheets("Data").Range("B9").Value
CUSTOMER1_USER2_TEXTBOX.Value = Sheets("Data").Range("B10").Value
CUSTOMER1_USER3_TEXTBOX.Value = Sheets("Data").Range("B11").Value
CUSTOMER1_USER4_TEXTBOX.Value = Sheets("Data").Range("B12").Value
CUSTOMER1_USER5_TEXTBOX.Value = Sheets("Data").Range("B13").Value
CUSTOMER1_USER6_TEXTBOX.Value = Sheets("Data").Range("B14").Value
CUSTOMER1_USER7_TEXTBOX.Value = Sheets("Data").Range("B15").Value
CUSTOMER1_USER8_TEXTBOX.Value = Sheets("Data").Range("B16").Value
CUSTOMER1_USER9_TEXTBOX.Value = Sheets("Data").Range("B17").Value
CUSTOMER1_USER10_TEXTBOX.Value = Sheets("Data").Range("B18").Value
CUSTOMER2_USER1_TEXTBOX.Value = Sheets("Data").Range("D9").Value
CUSTOMER2_USER2_TEXTBOX.Value = Sheets("Data").Range("D10").Value
CUSTOMER2_USER3_TEXTBOX.Value = Sheets("Data").Range("D11").Value
CUSTOMER2_USER4_TEXTBOX.Value = Sheets("Data").Range("D12").Value
CUSTOMER2_USER5_TEXTBOX.Value = Sheets("Data").Range("D13").Value
CUSTOMER2_USER6_TEXTBOX.Value = Sheets("Data").Range("D14").Value
CUSTOMER2_USER7_TEXTBOX.Value = Sheets("Data").Range("D15").Value
CUSTOMER2_USER8_TEXTBOX.Value = Sheets("Data").Range("D16").Value
CUSTOMER2_USER9_TEXTBOX.Value = Sheets("Data").Range("D17").Value
CUSTOMER2_USER10_TEXTBOX.Value = Sheets("Data").Range("D18").Value
......
Could you help me please do it ?
Thanks

Something like this should work:
Private Sub UserForm_Initialize()
Dim i As Long, j As Long
With Sheets("Data")
For i = 1 To 20
For j = 1 To 10
Me.Controls("CUSTOMER" & i & "_USER" & j & "_TEXTBOX").Value = .Cells(j+8, 2*i).Value
Next j
Next i
End With
End Sub

Related

Libreoffice Unable to fill ListBox because I could not declare the listbox righteously

I have a Listbox in my Calc Worksheet and I am trying to address it for filling it with data
DialogLibraries.LoadLibrary("Standard")
oDialog = CreateUnoDialog(DialogLibraries.Standard)
listBoxGruppe = oEvent.Source.Context.getControl("Drop_Down_5") 'oDialog.getControl("Drop_Down_5")
aItems = sheetb.getCellRangeByName(subStrRan).dataarray
Dim sItems(ubound(aItems))
For i = 0 To ubound(aItems)
sItems(i) = aItems(i)(0)
Next i
listBoxGruppe.addItems(sItems, 0)
oDialog.Execute()
However, it seems u need to call a certain Dialog, but idk what is the name of my Dialog for this scenario - this is how my code behind looks like
I think you don't need dialogs at this situation
dim RangeAddr as new com.sun.star.table.CellRangeAddress
RangeAddr.EndColumn = 1
RangeAddr.EndRow = Cell.CellAddress.Row
RangeAddr.Sheet = 2
RangeAddr.StartColumn = 1
RangeAddr.StartRow = 0
dim initParam(0) as new com.sun.star.beans.NamedValue
initParam(0).Name="CellRange"
initParam(0).Value = RangeAddr
CellRangeListSource = doc.createInstanceWithArguments("com.sun.star.table.CellRangeListSource", initParam )
listBoxGruppe.setListEntrySource CellRangeListSource

How can I use iteration to make this vbnet code more efficient?

Function set_bar_positions()
bar_x(0) = delocateX(bar1.Left)
bar_y(0) = delocateY(bar1.Top)
bar_x(1) = delocateX(bar2.Left)
bar_y(1) = delocateY(bar2.Top)
bar_x(2) = delocateX(bar3.Left)
bar_y(2) = delocateY(bar3.Top)
This snippet from one of my functions show what I'm trying to do. These lines repeat almost identical until the end of the function where this is called:
bar_x(29) = delocateX(bar30.Left)
bar_y(29) = delocateY(bar30.Top)
I have tried iterating this functions by doing stuff like this, but now I know I can't:
Dim num As Integer = 0
bar_x(num) = delocateX(bar(num)).Left)
I am trying to make this code more efficient and have less lines. Anyone have an idea I can implement?
You can use Controls.Find
For i = 0 To 29
Dim cs = Me.Controls.Find("bar" & i.ToString(), True)
If cs.Any() Then
Dim c = cs.First()
bar_x(i) = delocateX(c.Left)
bar_y(i) = delocateY(c.Top)
End If
Next

How can I write this VBA code more efficient?

is there a possible way to write this piece of VBA code more efficient?
With the use of for loops or so?
And make it more general, not with the fixed cells?
Private Sub CommandButton1_Click()
Range("A31").Formula = "=index(Optional_Processes,1)"
Range("A32").Formula = "=index(Optional_Processes,2)"
Range("A33").Formula = "=index(Optional_Processes,3)"
Range("A34").Formula = "=index(Optional_Processes,4)"
Range("A35").Formula = "=index(Optional_Processes,5)"
Range("A36").Formula = "=index(Optional_Processes,6)"
Range("A37").Formula = "=index(Optional_Processes,7)"
Range("A38").Formula = "=index(Optional_Processes,8)"
Range("A39").Formula = "=index(Optional_Processes,9)"
Range("A40").Formula = "=index(Optional_Processes,10)"
Range("A41").Formula = "=index(Optional_Processes,11)"
Range("A42").Formula = "=index(Optional_Processes,12)"
Thanks!
Another way to do it in one hit would be:
Private Sub CommandButton1_Click()
Range("A31:A42").Formula = "=index(optional_processes,row()-30)"
End Sub
This wouldn't put 1,2,3 etc as the last argument but when placed on row 31 it will return 1 (row()-30) and so on.
Using a For loop, like this:
Dim i As Long
For i = 2 To 12
Range("A" & 30 + i).Formula = "=index(Optional_Processes," & i & ")"
Next i
2nd Option:
Dim CellStart As Range
Set CellStart = Range("A30") ' set the Start Cell anchor
For i = 2 To 12
CellStart.Offset(i).Formula = "=index(Optional_Processes," & i & ")"
Next i

Submitchanges after a loop - only last record saved

The following code works, but it only saves the last record in the loop, and I can't figure out why. I think the Submitchanges() is in the right place, at the end of the loop. Can someone please show me what's wrong? Thanks.
Sub POPULATE_CHAIN()
Dim newChain As New CHAIN
Dim dpSTRIKE As Integer
'get list of Options Contracts
Dim lstOPT = From Z In DATA.OPTIONs, X In DATA.UDLies
Where X.UDLY_SYM = Z.UDLY_SYM
Select Z.CONTRACT, Z.STRIKE_GAP, X.UDLY_LAST
Dim dctOPT = lstOPT.ToDictionary(Function(Z) Z.CONTRACT)
For Each key In dctOPT.Keys
For COUNT = 1 To 5
dpSTRIKE = 1850 + 5 * COUNT
Dim lkup = From Z In DATA.CHAINs
Select Z
Dim RCD_EXISTS As Boolean = lkup.Any(Function(Z) Z.CONTRACT = dctOPT(key).CONTRACT And Z.P_C = "C" And Z.STRIKE = dpSTRIKE)
If RCD_EXISTS = False Then
newChain.CONTRACT = dctOPT(key).CONTRACT
newChain.P_C = "C"
newChain.STRIKE = dpSTRIKE
DATA.CHAINs.InsertOnSubmit(newChain)
Else
newChain.CONTRACT = dctOPT(key).CONTRACT
newChain.P_C = "C"
newChain.STRIKE = dpSTRIKE
End If
Next
Next
DATA.SubmitChanges()
End Sub
Dim newChain As New CHAIN
should be inside Fore Each, exactly inside second for.
Since it is declared outside the loop, it will be detached from table and attached again to table. So it will be inserted only in last row.

Scope of Cell Object in For Loop - VBA

I'm working on a bit of VBA that's intended to loop over a Schedule Builder for students made in Excel. I keep getting an Error 424 during the assignments c = c.Offset(X, 0), but only in the nested For loops. Is there a limited scope, and if so, how do I overcome it?
Below is the code:
Public Sub generateRosters()
Worksheets("Course Rosters").Cells.ClearContents
Worksheets("Course Rosters").Range("A1") = "Course"
Worksheets("Course Rosters").Range("B1") = "Room"
Dim classTitleRange As Range
Set classTitleRange = Worksheets("Master School Schedule").Range("D1:BN1")
Dim rowCount As Integer
rowCount = 2
Dim periodArr(1 To 8) As String
periodArr(1) = "A"
periodArr(2) = "B"
periodArr(3) = "C"
periodArr(4) = "D"
periodArr(5) = "E"
periodArr(6) = "F"
periodArr(7) = "G"
periodArr(8) = "Z"
For Each c In classTitleRange.Cells
Dim courseTitle As String
courseTitle = c
c = c.Offset(2, 0)
Dim room As String
room = c
For Each p In periodArr()
Dim offsetCount As Integer
offsetCount = 0
For i = 1 To 340
c = c.Offset(1, 0) '424 Error One
If c = p Then
End If
offsetCount = offsetCount + 1
Next
c = c.Offset(-offsetCount, 0) '424 Error Two
Next
Worksheets("Course Rosters").Range("A" & rowCount) = "'" & courseTitle
Worksheets("Course Rosters").Range("B" & rowCount) = room
rowCount = rowCount + 1
Next
End Sub
Thanks, for your help.
Edit: Side question, is there a way for me to create a variable that I can manipulate like c, but not be c. Basically a Dim d As (Something) followed by d = c. I can't seem to find the right object to assign to d, so that I can make it c. Thanks again.
I don't get logic and goal of your code therefore there are only some tips for you:
c=c.offset(2,0)
changes initial Range type c variable into empty or any other value.
Next you try to use, in the line with error, the same c variable as range object which is not allowed.
What you possibly need is the Set instruction in the following lines:
Set c= c.offset(2,0)
'....
Set c= c.offset(1,0)
But as I said, I don't know the complete logic therefore this is solution for the error you have but not sure if it solve all your problems.