Quantcast
Channel: MicroStation Programming Forum - Recent Threads
Viewing all articles
Browse latest Browse all 7260

RE: [V8i VBA] Copying listbox items to 2D array but getting wrong number of columns

$
0
0

Morning Jon

I did spend some time last week speaking with other VBA experts (more familiar to office products) and they found the code gives the correct results though I have read that Word VBA differs from Excel VBA which differs from Access VBA so I guess some how, MicroStation VBA must again be slightly different even if if does share the same methods and properties as the others. Oddly though, one person on another site did get the same incorrect array results as myself even though I did not specify which application the VBA project was hosted in.

I've now managed to fix my problematic array dimensions by specifying the listbox listcount and columncount values as the array row/column dimensions. I do also use Option Base 0 and LBound/UBound for the start and end of arrays as can be seen in the now completed class:

Option Explicit
Option Base 0

Private FrmParent                                 As MSForms.UserForm
Public WithEvents SwapLB                          As MSForms.CommandButton
Private SwapList                                  As MSForms.ListBox

Public Property Set Parent_Form(Frm As MSForms.UserForm)
Set FrmParent = Frm
End Property

Public Property Set Active_ListBox(lb As MSForms.ListBox)
Set SwapList = lb
End Property

Private Sub SwapLB_click()
Dim strText                                       As String                              '<---| Variable which stores the value in Col 0 on the selected row
Dim TempArray()                                   As Variant                             '<---| Temporary Array to store moving listbox items
Dim R                                             As Long                                '<---| Loop Row counter variable
Dim C                                             As Long                                '<---| Loop Column counter variable

With SwapList
    If Len(.RowSource) > 0 Then Exit Sub

    strText = .List(2)                                                                   '<---| copy value from last row in the worksheets listbox to string variable
    .RemoveItem 2                                                                        '<---| delete the last row in the worksheets listbox
    .AddItem strText, 0                                                                  '<---| Insert the value from the string variable to the 1st row of in the worksheets listbox

'As a result of the above, the previous middle listbox value is now at the bottom so we use the following to move it back to its original position.

    strText = .List(2)                                                                   '<---| copy value from last row in the worksheets listbox to string variable
    .RemoveItem 2                                                                        '<---| delete the last row in the worksheets listbox
    .AddItem strText, 1                                                                  '<---| Insert the value from the string variable to the 2nd row of in the worksheets listbox

    ReDim TempArray(FrmMain.LB_Sheet1.ListCount - 1, FrmMain.LB_Sheet1.ColumnCount - 1)  '<---| Set the array dimensions to match the top listbox rows/columns
    
For R = 0 To FrmMain.LB_Sheet1.ListCount - 1 '<---| Loop though the rows of the top listbox For C = 0 To FrmMain.LB_Sheet1.ColumnCount - 1 '<---| Loop through the columns of the listbox TempArray(R, C) = FrmMain.LB_Sheet1.List(R, C) '<---| Copy the listbox items to the temporary array Next C '<---| Move to next column Next R '<---| Move to next row FrmMain.LB_Sheet1.Clear '<---| Empty the top listbox For R = 0 To FrmMain.LB_Sheet3.ListCount - 1 '<---| Loop though the rows of the bottom listbox FrmMain.LB_Sheet1.AddItem FrmMain.LB_Sheet3.List(R, 0), R '<---| Copy the column 0 item in top listbox to equivalent position in bottom listbox For C = 1 To FrmMain.LB_Sheet3.ColumnCount - 1 '<---| Loop the column 1 to last column in the bottom listbox FrmMain.LB_Sheet1.List(R, C) = FrmMain.LB_Sheet3.List(R, C) '<---| Copy column item in bottom listbox to equivalent position in top listbox Next C '<---| Move to next column Next R '<---| Move to next row FrmMain.LB_Sheet3.Clear '<---| Empty the bottom listbox For R = LBound(TempArray, 1) To UBound(TempArray, 1) '<---| Loop though the lower to upper dimension of the temporary array rows For C = 0 To LBound(TempArray, 2) '<---| Loop though the lower to upper dimension of the temporary array columns FrmMain.LB_Sheet3.AddItem TempArray(R, C), R '<---| Copy the column 0 item in each row to the bottom listbox Next C '<---| Move to next column For C = 1 To UBound(TempArray, 2) '<---| Loop from 1 to upper dimension of the temporary array columns FrmMain.LB_Sheet3.List(R, C) = TempArray(R, C) '<---| Copy remaining temporary array column values to bottom listbox Next C '<---| Move to next column Next R '<---| Move to next row End With End Sub


Viewing all articles
Browse latest Browse all 7260

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>