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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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