Find row based cell value and add data to next empty column in that row...

Chac96

New Member
Joined
Oct 31, 2018
Messages
3
I'm new to VBA and have really been struggling to get this to work!

I have a sheet that has a list of customer contract names in "A" and a user form that has a drop down with all those names in. I've managed to write a VBA to find the row corresponding to the contract name but then want to copy the data from a textbox into the next blank column in that row.

Example

Contract Name: Test (Row 1)
Next Blank Cell in row 1: D
Data in text box : 23/11/96

So when i click the save button on my user form i want it to match the dropdown selection with the corresponding contract in column A, then find the next blank cell in that row (D) and put the date (23/11/96) into that cell.

I'm not great at explaining this but would appreciate someones help.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

kanadaaa

Active Member
Joined
Dec 29, 2019
Messages
348
Office Version
  1. 365
Platform
  1. Windows
Try this in the form module:
VBA Code:
Private Sub CommandButton1_Click()
    Dim fnd As Range, cell As Range
    Application.ScreenUpdating = False 'Make the code faster
    
    Set fnd = Range("A:A").Find(ComboBox1, , xlValues, xlWhole) 'Check if customer name exits
    
    If fnd Is Nothing Then 'When name in ComboBox isn't found
        MsgBox "No match found", vbExclamation, "Error" 'Show error message
        Exit Sub
    Else 'When name in ComboBox IS found
        For Each cell In Rows(fnd.Row).Cells 'Get first blank cell in the row
            If cell = "" Then
                cell = Format(TextBox1, "dd/mm/yy") 'Output date in TextBox1 into the first blank cell
                Exit For
            End If
        Next cell
    End If
    
    Unload Me 'Close Userform
    Application.ScreenUpdating = True
End Sub
 

Chac96

New Member
Joined
Oct 31, 2018
Messages
3
Try this in the form module:
VBA Code:
Private Sub CommandButton1_Click()
    Dim fnd As Range, cell As Range
    Application.ScreenUpdating = False 'Make the code faster
   
    Set fnd = Range("A:A").Find(ComboBox1, , xlValues, xlWhole) 'Check if customer name exits
   
    If fnd Is Nothing Then 'When name in ComboBox isn't found
        MsgBox "No match found", vbExclamation, "Error" 'Show error message
        Exit Sub
    Else 'When name in ComboBox IS found
        For Each cell In Rows(fnd.Row).Cells 'Get first blank cell in the row
            If cell = "" Then
                cell = Format(TextBox1, "dd/mm/yy") 'Output date in TextBox1 into the first blank cell
                Exit For
            End If
        Next cell
    End If
   
    Unload Me 'Close Userform
    Application.ScreenUpdating = True
End Sub
Works like a treat, absolute legend!!! Really appreciate the help
 

Forum statistics

Threads
1,144,342
Messages
5,723,813
Members
422,518
Latest member
quack_quack

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
Top