Add textbox value from userform in cell if cell is empty

MrThor

New Member
Joined
Aug 13, 2018
Messages
36
Hi, I want to add a textbox value in a cell if its empty. If the cell is not empty I want to add to the next cell under instead and so on. I have a code which I can not get so it works correctly. Can anybody help me with this?

Code:
Private Sub TextBox1_Change()

Dim rg As Worksheet
Set rg = Worksheets("Blad2")




If rg.Range("B2").Value = "" Then
rg.Range("B2").Value = Jan1.TextBox1.Value


End If




End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Maybe a loop

Code:
Option Explicit


Private Sub TextBox1_Change()


    Dim rg As Worksheet
    Set rg = Worksheets("Blad2")
    Dim i As Long, lr As Long
    lr = Range("B" & Rows.Count).End(xlUp).Row
    For i = 2 To lr
        If rg.Range("B" & i).Value = "" Then
            rg.Range("B" & i).Value = Jan1.TextBox1.Value
        Next i
    End If
End Sub
 
Upvote 0
Hi MrThor,

Welcome to MrExcel!

Change event wouldn't easily help in this case, because every time you type something in the text box, the cell value will be populated, and it won't be empty string even if you have something more to type in the text box.

If you don't have to use Change event for some specific reason, then I suggest using Textbox1_Exit event which will be triggered when you exit the textbox on the form (clicking on another box or button), or even using a command button to update the cell value on click.

Following uses the Exit event:

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim sht As Worksheet
Dim cll As Range


Set sht = Worksheets("Blad2")
Set cll = sht.Range("B2")


Do Until cll.Value = ""
    Set cll = cll.Offset(1)
Loop


cll.Value = TextBox1.Value
End Sub

It is simply starting from B2 cell, and using a loop to check the cell value, and set the cell to be updated as the next row cell if the previous cell is not empty.
 
Upvote 0
You're welcome.

Following is an alternative, more optimized way instead testing all cells from B2.

This one is simply going to the last cell of the B column (Column index is 2), and jumping to the last used cell, then setting then next cell as the subject.

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim sht As Worksheet
Dim cll As Range
    Set sht = Worksheets("Blad2")
    Set cll = sht.Cells(sht.Rows.Count, 2).End(xlUp).Offset(1)
    cll.Value = TextBox1.Value
End Sub

There are really more than one way to make things work in VBA and object oriented programming. We don't even have to use variables, instead use a single line code, but it wouldn't be a good practice of coding.

Glad to hear it helps.
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

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