Help Needed

kwavic

New Member
Joined
Sep 13, 2024
Messages
9
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have created a userform to submit data to the respective cells based on vba code I found on the forum that will help me and i modifies it. But it was not working as intended.
I want to match a date in textbox1 from the userform to column G, if the date is not found, add a new row and populate data into the respective columns based the values from the textboxes at different times.
Attached image. Thank you
VBA Code:
[/
Private Sub CommandButton1_Click()
    Dim targetsheet As String
    targetsheet = ComboBox1.Value
    
    ' Check if targetsheet is empty, exit the sub if true
    If targetsheet = "" Then
        Exit Sub
    End If

    Dim ws As Worksheet
    Set ws = Worksheets(targetsheet)
    
    Dim Lookup As String
    Dim lastRow As Long
    Dim SalesClm As Integer
    Dim IvfClm As Integer
    Dim MomoClm As Integer

    ' Disable error handling temporarily
    On Error Resume Next
    Lookup = Me.TextBox1.Value
    
    ' Use Match function to find the lookup value in column G
    lastRow = Application.Match(Lookup, ws.Range("G5:G"), 0)
    
    SalesClm = 2
    IvfClm = 3
    MomoClm = 4

    ' Check if the lookup value was found
    If Not IsError(lastRow) Then
        ' Insert a new row below the last used row in column G
        ws.Cells(ws.Rows.Count, "G").End(xlUp).Offset(1, 0).EntireRow.Insert
        lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row

        ' Populate data into respective columns based on TextBox values
        If Me.TextBox2 <> "" Then ws.Cells(lastRow, SalesClm) = Me.TextBox2.Value
        If Me.TextBox3 <> "" Then ws.Cells(lastRow, IvfClm) = Me.TextBox3.Value
        If Me.TextBox4 <> "" Then ws.Cells(lastRow, MomoClm) = Me.TextBox4.Value

        If Me.TextBox5 <> "" Then ws.Cells(lastRow, SalesClm + 3) = Me.TextBox5.Value
        If Me.TextBox6 <> "" Then ws.Cells(lastRow, IvfClm + 3) = Me.TextBox6.Value
        If Me.TextBox7 <> "" Then ws.Cells(lastRow, MomoClm + 3) = Me.TextBox7.Value

        If Me.TextBox8 <> "" Then ws.Cells(lastRow, SalesClm + 6) = Me.TextBox8.Value
        If Me.TextBox9 <> "" Then ws.Cells(lastRow, IvfClm + 6) = Me.TextBox9.Value
        If Me.TextBox10 <> "" Then ws.Cells(lastRow, MomoClm + 6) = Me.TextBox10.Value
        
        MsgBox "Data was added successfully"
        
        ' Clear TextBox values after data insertion
        With Me
            .TextBox2.Value = ""
            .TextBox3.Value = ""
            .TextBox4.Value = ""
            .TextBox5.Value = ""
            .TextBox6.Value = ""
            .TextBox7.Value = ""
            .TextBox8.Value = ""
            .TextBox9.Value = ""
            .TextBox10.Value = ""
        End With
    End If
End Sub

Private Sub CommandButton2_Click()
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Dim sh As Worksheet
    ComboBox1.Clear
    
    ' Fill ComboBox1 with worksheet names from the workbook
    For Each sh In ThisWorkbook.Worksheets
        ComboBox1.AddItem sh.Name
    Next sh
End Sub

]
 

Attachments

  • Screenshot (14).png
    Screenshot (14).png
    131.6 KB · Views: 15
Okay will let you know
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
All my columns G for the worksheets are formatted with date so even if you submit let say 5 it will change it to some date. I try to submit 5 as the date from the userform to January worksheet, it returns nothing
Here is what i got :

bukti-ezgif.com-optimize.gif
 
Upvote 0
I got it, My purpose is that when the morning **** ended you submit data, same to afternoon and evening. Not to wait say the following day to enter everything. Okay wanted to send you a copy of the workbook but after downloading the XI2bb, the mini sheet copy to the clipboard, can't find it
 
Upvote 0
When I save the data it doesn't go to the formatted cells, however if you saved it at different times,it does not match it with the date in columns G to save the data on the same row. So please check it for me again. Thank you attached a copy of the submitted data.
 

Attachments

  • Screenshot (1).png
    Screenshot (1).png
    119.4 KB · Views: 10
Upvote 0

Forum statistics

Threads
1,226,460
Messages
6,191,164
Members
453,643
Latest member
adamb83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top