Active sheet, text box copy, check if cell empty

mrmrf

New Member
Joined
Jun 3, 2019
Messages
34
Hi.


I use the blow code to paste data from a textbox in a user from to a specific cell on sheet 1.


I would like to refine this to:


Paste to the active worksheet, instead of just Sheet1


and


Paste to Cell N15 (of active sheet), only if empty. If not empty check if N25 is empty and then paste. If both Cells contain data, then error message box.


Thanks


Code:
Private Sub AddButton2_Click()


Sheets("Sheet1").Select
Range("N15").Value = txtSampleNo.Text


End Sub
 

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,473
Hi mrmrf,

Here's one way:

Code:
Option Explicit
Private Sub AddButton2_Click()

    If Len(Range("N15")) = 0 Then
        Range("N15").Value = Me.txtSampleNo.Text
    ElseIf Len(Range("N25")) = 0 Then
        Range("N25").Value = Me.txtSampleNo.Text
    Else
        MsgBox "There is data in cells N15 and N25.", vbExclamation
    End If

End Sub
Regards,

Robert
 
Last edited:

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,473
Thanks for letting us know and you're welcome :)
 
Last edited:

Forum statistics

Threads
1,085,010
Messages
5,381,209
Members
401,718
Latest member
Ereese20

Some videos you may like

This Week's Hot Topics

Top