Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 27

Thread: Using VBA Set for workbook and worksheet

  1. #1

    Using VBA Set for workbook and worksheet

    I have a workbook with two worksheets: Sheet1 = named Find and Sheet2 =named MasterData. On Sheet2 I have a created a table named Table2
    I am have created a Userform to interact with the MasterData table2 but continue to get errors when I try to create SET statement in the module.
    Here are the Dim statements in procedure:

    Dim wb As Workbook
    Dim ws1 As Worksheet
    Dim tbl1 As ListObject
    The module I am having trouble with should process like this (after clicking a record in the forms listbox it should select that records info from sheet2 data and fill individual text boxes)

    Here is the code section (with lines identified as showing errors)

    Private Sub lbo1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Me.lbo1.ListIndex = -1 Then Exit Sub
    If Me.lbo1.ListIndex > -1 Then sc = Me.lbo1.ListIndex + 2
    ClearForm
    Set ws1 = wb.Sheets("Sheet2")               'errors out
    Set tbl1 = ws1.ListObjects("Table2")         'errors out
    With tbl1
    For i = 1 To 6
    Controls("txtbox" & i).Value = .Range(sc, i)
    Next i
    End With
    End Sub
    How should these SET statements be modified? Thank you.
    Last edited by Aussiebear; 11-18-2022 at 03:42 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    You haven't Set the wb object or was that done in another procedure? Where are those Dim statements located - not in the procedure you posted.

    Should add Option Explicit to the top of all modules.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3

    All the code for the problem

    Thank you for your reply.
    I do have a named range named 'Table2'.

    With your questions I thought I should provide you the code I am working with.

    Option Explicit
    Dim wb As Workbook
    Dim ctl As Control
    Dim ary1 As Variant
    Dim ws As Worksheet
    Dim tbl1 As ListObject
    
    Sub ClearForm()
    For Each ctl In uf1.Controls
    Select Case TypeName(ctl)
    Case "TextBox"
    ctl.Text = ""
    Case "ListBox"
    ctl.ListIndex = -1
    End Select
    Next ctl
    End Sub
    
    Sub LoadListBox()
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("MasterData")
    Set tbl1 = ws.ListObjects("Table2")  'this is the offending line - Set ws line above does not error out
    With tbl1
    If .Range(2, 1) = "" Then Exit Sub
    ary1 = .DataBodyRange
    End With
    With uf1.lbo1
    .List = ary1
    .ColumnCount = 6
    .ColumnWidths = "50,50,30,20,20,20"
    End With
    End Sub
    Again, your help is really appreciated.
    Last edited by Aussiebear; 11-18-2022 at 05:24 PM. Reason: Added code tags to supplied code

  4. #4
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    Now, I have to ask - why use Excel if what you need is a database?

    And where is ws1 variable declared? In one procedure you use ws and another ws1.

    Not interested in trying to build workbook to replicate issue.

    You could provide the workbook for analysis.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5

    Here is the Workbook being referenced in the thread

    Here is a copy with data data for your review.
    The Find page works as designed. The error occurs when you click the button at the bottom of the page.

    I just noticed that the version I sent you did not have the command button at the bottom of the Find page.
    The code is included in VB editor. The name of the button is AddEditMod

    If you edit the buttons code (below) into the Sheet1(FIND) module it will all work correctly.

    Private Sub AddEditMod_Click()
    uf1.Show
    End Sub
    Attached Files Attached Files
    Last edited by Aussiebear; 11-19-2022 at 12:39 AM. Reason: Added code tags to supplied code

  6. #6
    I am a database nerd and would have done it using Access but I have a close friend (and my wife) want nothing to do with DB's so am using Excel. I will be using this for me also as a password manager from a flashdrive so I can get away from online PW mgr. I may work om putting together a runtime version in db later on. Thanks, I really appreciated your help.

  7. #7
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    Are you saying I need to create a button on Sheet1 and attach the code to it?
    I did that and click button. Errors "subscript out of range" on uf1.Show.

    Why does combobox list have only one item?
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    If you highlight the combobox list item and type in a w, it should show the list of db records that contain that letter. Selecting on of the displayed items will populate the rest of the boxes.

    The Error you mentioned just started in the wb I sent you. I can't figure out why. I have saved versions after making changes and never had that problem. I am sorry.

    I just spotted that the command module does not contain the _click in the private procedure. That may not fix the problem.
    Last edited by StudentJacks; 11-18-2022 at 09:01 PM.

  9. #9
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    I am confused. I am not missing _Click anywhere as far as I can tell unless you mean the form procedures.

    Why is there a Sheet11 module that duplicates code in Sheet1?

    Combobox works as you instructed but it is annoying that the list doesn't populate with dropdown click.

    Maybe you should replace the posted file.

    Code would be easier to follow with proper indentation.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    I will try to fix what I can and try to post an updated wb maybe tomorrow evening. Thanks very much.

  11. #11
    Quote Originally Posted by StudentJacks View Post
    I will try to fix what I can and try to post an updated wb maybe tomorrow evening. Thanks very much.
    I have done some work in an effort to help figure out the problem.
    First I created a completely new form (Userform1) created all the objects contained on the uf1 form and ran it. It opened as it should the first time but from then got an error on this procedure

    Private Sub OpenAddModDelForm_Click()
    uf1.Show
    End Sub
    So I tried changing the .Show to .Open, ran it and with the error changed the .Open back to .Show and it ran fine but the next time trying to run the code, it errors again.

    The next thing I did was go into VBA, opened Userform1 and clicking the run command. The two procedures
    Private Sub UserForm_Initialize()
        ClearForm
        LoadListBox
    End Sub
    work and run. You will get an error in the Sub LoadListBox() procedure.

    At least you will be able to see the code in the Set tblList = ws.ListObjects("Table1") line of the code.

    Hope this all makes sense.
    Attached Files Attached Files
    Last edited by Aussiebear; 11-19-2022 at 02:14 PM. Reason: Added code tags to supplied code

  12. #12
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    Why is there still no button on the worksheet to open form? Looks exactly like the first file you posted.

    Okay, I can get the form to run by commenting out call to LoadListBox procedure in form's code (once I figured out how to view form code), as this is cause of error.

    Where is this "Table2" referenced in listbox load procedure? It is not listed in Name Manager. What data should load into listbox?

    I created a Table2 and now code runs without error.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13

    The list should be displaying

    Quote Originally Posted by June7 View Post
    Why is there still no button on the worksheet to open form? Looks exactly like the first file you posted.

    Okay, I can get the form to run by commenting out call to LoadListBox procedure in form's code (once I figured out how to view form code), as this is cause of error.

    Where is this "Table2" referenced in listbox load procedure? It is not listed in Name Manager. What data should load into listbox?

    I created a Table2 and now code runs without error.
    Since the Table 2 now works, the ufi form the lbo1 (lable: Search Site Mane) control should be populated with the data in Table2

  14. #14
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    Yes, it works with the Table2 I created. I just don't know if I based the table on correct range. So you just need to set up Table2 in your file.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    Quote Originally Posted by June7 View Post
    Yes, it works with the Table2 I created. I just don't know if I based the table on correct range. So you just need to set up Table2 in your file.
    Thank you. Is there any chance you can provide the db that you worked on so I can view and test it? I still get compile errors and am going crazy?

    Secondly, can you please tell me how many records were in the MasterData worksheet you last downloaded?
    It is an important question and I would appreciate a response. Thank you

  16. #16
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    I see 5 records.

    All I did was create Table2. Didn't change any code. Table2 is what code referenced, however, I see your post shows Table1. Regardless, neither were in the Name Manager list.

    If you want to see what I have, just download from your post.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  17. #17
    Quote Originally Posted by StudentJacks View Post
    Thank you. Is there any chance you can provide the db that you worked on so I can view and test it? I still get compile errors and am going crazy?

    Secondly, can you please tell me how many records were in the MasterData worksheet you last downloaded?
    It is an important question and I would appreciate a response. Thank you
    Well, surprise, I got the form working and it loads up with the MasterData records. I created a new Named range (TableX), updated the Table1 and/or Table2 to this new reference. Clicking the command button on the Find page (and it is there) will open the form without errors. But, if you click on one of the records in the forms list, you will see an error on the Set tbl1 = ws.ListObjects.Name = "TableX". This is the same error I was first trying to correct. Is it a syntax error?

    If you are still willing to look at this ---- thank you is not enough but that's the best I can do.

    This is the wrong file. I will provide the latest update in another post
    Attached Files Attached Files

  18. #18

    The Correct Workbook

    Use this one. I couldn't figure out how to remove the wrong one from the previous post
    Attached Files Attached Files

  19. #19

    Image of the Find Page with Command Button

    Just in case the newest file I sent does not show the command button on the Find page, I am attaching an image of that page.
    Attached Images Attached Images

  20. #20
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    Yes, the button is there and works.

    This line:

    Set tbl1 = ws.ListObjects.Name = "TableX"

    should be

    Set tbl1 = ws.ListObjects("TableX")


    TableX is pulling only 4 columns. Sub txtbox1_AfterUpdate() expects 6. Resize TableX to include columns E and F. Include G if you want Notes and then modify code because it is loading email in the Notes textbox. Better add a textbox for Email. Modify code for 7 columns.


    Still annoying that the combobox list won't load on click and it doesn't stay loaded.
    URL textbox on form won't work as a clickable hyperlink. A textbox in Access will.
    The hyperlinks also lose clickability in the VLookup() formula.


    Why do you have combobox on worksheet instead of just using the form as interface?
    Last edited by June7; 11-20-2022 at 01:40 AM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Tags for this Thread

Posting Permissions

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