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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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:
Upvote 0
Thanks for letting us know and you're welcome :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

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