+ Reply to Thread
Results 1 to 6 of 6

editing a spreadsheet using a userform

Hybrid View

  1. #1
    Registered User
    Join Date
    11-06-2014
    Location
    Manila, Philippines
    MS-Off Ver
    Office 2010
    Posts
    2

    editing a spreadsheet using a userform

    Hello.
    Im just trying to learn VBA via forums.

    I need your help in completing this userform.

    I have created a userform where you can add data into a worksheet.

    What I'm trying to do now is to edit the data (previously added) by autopopulating it and editing it in the userform.

    The code I found on other sites are just for userforms with textboxes, however my userform has comboxes and it won't work.





    Can someone help me?

    Thank you in advance.
    Attached Files Attached Files
    Last edited by m4r4; 12-29-2014 at 04:08 AM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: need help please

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title go to your first post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: editing a spreadsheet using a userform

    Here try this:

    Sub EditAdd()
    
    Dim emptyRow As Long
    On Error Resume Next ' This command will skip error if it run into it
    If Punchlist.TextBox1.Value <> "" Then
        flag = False
        i = 0
        id = Punchlist.TextBox1.Value
        emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 ' this fill find first empty row
        emptyRow = WorksheetFunction.Match(id, Sheets("Punchlist").Range("A1:A65536"), 0) 
            ' This will find matching value. If doesn't exist MATCH will produce error.
            ' That's why I put above On Error Resume Next to skip that error
            ' And since it's skiped a previous value will remain and data will be written in last row
            ' And if value does exist new emptyRow will be matching row

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: editing a spreadsheet using a userform

    But you also need to use sheet where you writting data..

    So instead of, for example:
    Cells(emptyRow, j).Value
    You should have:
    Sheets("Punchlist").Cells(emptyRow, j).Value
    or you need to declare it on start and use it like:
    Dim wrksht as Worksheet
    Set wrksht  = Sheets("Punchlist")
    
    
    wrksht.Cells(emptyRow, j).Value

  5. #5
    Registered User
    Join Date
    11-06-2014
    Location
    Manila, Philippines
    MS-Off Ver
    Office 2010
    Posts
    2

    Re: editing a spreadsheet using a userform

    zbor,

    Thanks for your help.

    Im still getting an error.

    Sub EditAdd()

    Dim emptyRow As Long

    If Punchlist.TextBox1.Value <> "" Then
    flag = False
    i = 0
    id = Punchlist.TextBox1.Value
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

    Do While Cells(i + 1, 1).Value <> ""

    If Cells(i + 1, 1).Value = id Then
    flag = True
    For j = 2 To 14
    Cells(i + 1, j).Value = Punchlist.Controls("TextBox" & j).Value
    Next j
    End If

    i = i + 1

    Loop

    If flag = False Then
    For j = 1 To 14
    Sheets("Punchlist").Cells(emptyRow, j).Value = Punchlist.Controls("textbox" & j).Value Next j
    End If

    End If

    If Punchlist.TextBox1.Value <> "" Then
    flag = False
    i = 0
    id = Punchlist.TextBox1.Value
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

    Do While Cells(i + 1, 1).Value <> ""

    If Cells(i + 1, 1).Value = id Then
    flag = True
    For j = 2 To 14
    Cells(i + 1, j).Value = Punchlist.Controls("textBox" & j).Value
    Next j
    End If

    i = i + 1

    Loop

    If flag = False Then
    For j = 1 To 14
    Sheets("Punchlist").Cells(emptyRow, j).Value = Punchlist.Controls("textBox" & j).Value
    Next j
    End If

    End If

    End Sub

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: editing a spreadsheet using a userform

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1