Results 1 to 4 of 4

Thread: [RESOLVED] Help with arrays - Almost there!

  1. #1

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Resolved [RESOLVED] Help with arrays - Almost there!

    Back again with cap in hand...

    Below is code kindly provided by killazzz. Attached is the document that contains the code.

    Within the document is a userform that contains a series of comboboxes, some of which are disabled. Each combobox represents an item that has a unique price.

    When the code is executed, any comboboxes that are enabled, but false will be added to the document in the form:

    checkbox.caption <TAB SPACE> £ <TAB SPACE> Price


    VB Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. 'you define an array of all your prices and string variables
    4. Dim A As Variant
    5. Dim x As Integer
    6. Dim chkMyCheck As Control
    7. Dim MainPriceControlArray(5, 2) As String
    8.  
    9.     MainPriceControlArray(1, 1) = "150.00"       'Chain Shorteners
    10.     MainPriceControlArray(2, 1) = "100.00"        'Protection Covers
    11.     MainPriceControlArray(3, 1) = "2000.00" '     'Load Cells
    12.     MainPriceControlArray(4, 1) = "400.00" '    strCheckBox4= "400.00"
    13.     MainPriceControlArray(5, 1) = "500.00" '    strCheckBox5 = "500.00"
    14.    
    15.     MainPriceControlArray(1, 2) = "chkChainShorteners"
    16.     MainPriceControlArray(2, 2) = "chkProtectionCovers"
    17.     MainPriceControlArray(3, 2) = "chkLoadCells"
    18.     MainPriceControlArray(4, 2) = "strCheckBox4" '    strFireExtingExt
    19.     MainPriceControlArray(5, 2) = "chktest124578986532" '    strFireExtingInt
    20.    
    21. 'then you loop through your array and controls in the document
    22. ' you check each  control for it's anabled and value property and take the appropriate action
    23.  
    24.     For x = 1 To UBound(MainPriceControlArray)
    25.         For Each chkMyCheck In Me.Controls
    26.             If LCase(Left(chkMyCheck.Name, 3)) = "chk" And InStr(1, MainPriceControlArray(x, 2), chkMyCheck.Name, vbTextCompare) <> 0 Then
    27.                 If chkMyCheck.Enabled = True And chkMyCheck.Value = False Then
    28.                     ActiveDocument.Bookmarks("Test").Range.Text = vbCr & chkMyCheck.Caption & vbTab & "£" & vbTab & MainPriceControlArray(x, 1)
    29.                 Exit For
    30.                 End If
    31.             End If
    32.         Next chkMyCheck
    33.     Next
    34.    
    35. End Sub
    I am now attempting to alter the code slightly so that the items to be added to the document are done so using a table.

    What I have attempted to do is create another array that has 3 columns and the number of rows is equal to the number of items to be added. I than want to create a table of equivalent dimensions and add that to the document.

    For example, if there were 2 items to be added to the document (in reality the list is much larger), I would get a 2 row, 3 column table, which would contain the following items.

    (1,1) = checkbox1.caption
    (1, 2) = "£"
    (1, 3) = checkbox1 price

    (2,1) = checkbox2.caption
    (2,2) = "£"
    (2, 3) = checkbox2 price

    I also want to set column sizes and word alignment, but can probably work that out later. I have made numerous attempts at the above, but am having real difficulty. I have referenced arrays in the help files in vba, but can't seem to apply the information correctly (obviously it is the help files at fault )

    Any help appreciated.
    Attached Files Attached Files
    Last edited by New2vba; Mar 26th, 2006 at 12:30 PM.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  2. #2
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: Help with arrays

    Here you go
    Attached Files Attached Files

  3. #3

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: Help with arrays

    Thanks again killazzz, but I am afraid this is not exactly what I meant.

    I want to add my values to a table, which I will then insert at a bookmark. I have some code from DKenny (seems all the decent code I have was provided by one of the esteemed members of vbforums ) for creating a table on the fly, but am having trouble adapting it.

    I was guesing that first an additional array would be needed to store the values for entry into the table, but maybe this isn't the case?

    I know that each cell in column 2 will always contain "£" and the table will always have 3 columns.

    To clarify, if say checkboxes 1, 3, and 5 are enabled and false, then the table would be 3 rows by 3 columns and would contain:

    (1,1) = Item 1 caption
    (1,2) = "£"
    (1,3) = Item 1 price

    (2,1) = Item 3 caption
    (2,2) = "£"
    (2,3) = Item 3 price

    (3,1) = Item 5 caption
    (3,2) = "£"
    (3,3) = Item 5 price

    I also wanted to right align the entries in column 2 and 3 and set particular column widths.

    Any help or even pointers in the right direction would be appreciated.
    Last edited by New2vba; Mar 25th, 2006 at 11:58 AM.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  4. #4

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: Help with arrays - Almost there :)

    Here's an "attempt" at achieving the above, but I get error 9 at Redim Preserve.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub CommandButton1_Click()
    4.  
    5. 'you define an array of all your prices and string variables
    6. Dim x As Integer
    7. Dim AddOption() As String
    8. Dim chkMyCheck As Control
    9. Dim MainPriceControlArray(1 To 5, 1 To 2) As String
    10.  
    11. Dim myRange As Range
    12. Dim tblMyTable As Table
    13. Dim nItem As Long
    14. Dim nRowCount As Long
    15.  
    16. ReDim AddOption(1 To 1, 1 To 3) As String
    17.  
    18.     MainPriceControlArray(1, 1) = "chkChainShorteners"
    19.     MainPriceControlArray(2, 1) = "chkProtectionCovers"
    20.     MainPriceControlArray(3, 1) = "chkLoadCells"
    21.     MainPriceControlArray(4, 1) = "strCheckBox4"
    22.     MainPriceControlArray(5, 1) = "chktest"
    23.    
    24.     MainPriceControlArray(1, 2) = "150.00" '        Chain  Shorteners
    25.     MainPriceControlArray(2, 2) = "100.00" '        Protection Covers
    26.     MainPriceControlArray(3, 2) = "2000.00" '       Load Cells
    27.     MainPriceControlArray(4, 2) = "400.00" '        CheckBox4
    28.     MainPriceControlArray(5, 2) = "500.00" '        chktest
    29.    
    30. 'then you loop through your array and controls in the document
    31. ' you check each  control for it's anabled and value property and take the appropriate action
    32.  
    33.     For x = LBound(MainPriceControlArray, 1) To UBound(MainPriceControlArray, 1)
    34.         For Each chkMyCheck In Me.Controls
    35.             If LCase(Left(chkMyCheck.Name, 3)) = "chk" And MainPriceControlArray(x, 1) = chkMyCheck.Name Then
    36.                 If chkMyCheck.Enabled = True And chkMyCheck.Value = False Then
    37.                         AddOption(UBound(AddOption), 1) = Me.Controls(MainPriceControlArray(x, 1)).Caption
    38.                         AddOption(UBound(AddOption), 2) = "£"
    39.                         AddOption(UBound(AddOption), 3) = MainPriceControlArray(x, 2)
    40.                         ReDim Preserve AddOption(UBound(AddOption, 1) + 1) As String
    41.                 End If
    42.             End If
    43.         Next chkMyCheck
    44.     Next
    45.    
    46.   nRowCount = UBound(AddOption, 1)
    47.    
    48.     Set myRange = ActiveDocument.Bookmarks("Test").Range
    49.         If nRowCount > 0 Then
    50.             Set tblMyTable = ActiveDocument.Tables.Add(Range:=myRange, NumRows:=nRowCount, NumColumns:=3)
    51.         End If
    52.    
    53. UserForm1.Hide
    54.    
    55. End Sub

    Can anyone please tell me what I'm doing wrong?

    EDIT:


    I've since discovered that you can only change the last dimension of an array when using Preserve, as I'm sure many people already know!

    I have now got some working code that uses 2 single dimension arrays (don't know if this is the best thing to do, but it works!

    If there are any critiques out there, please check the code in the attached document and let me know where I can improve.
    Attached Files Attached Files
    Last edited by New2vba; Mar 26th, 2006 at 12:27 PM.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width