Textbox to add a value to the next available cell in a column

shapz

New Member
Joined
Jan 19, 2018
Messages
1
I am totally new to macros and need help with a solution.

I want to have a button on a worksheet that when clicked prompts a text box asking for a number. That number should then be pasted to the next available cell in column C on two different sheets.

Any help is greatly appreciated.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Shapz

Paste this code into a new module and then create a button on the first worksheet you'd like the user's number to be inserted into.

Code:
Option Explicit

Sub AddUserNumberToColC()


Application.ScreenUpdating = False


Dim userNum As Variant


'Collect user input
userNum = InputBox("Please enter a number...", "Add a number")
    
    'Check that user has entered something into the InputBox
    If userNum = "" Then
        Exit Sub
    Else:
        'Check value is a number
        If Not IsNumeric(userNum) Then
            MsgBox ("You must enter a number")
            Application.ScreenUpdating = True
            Exit Sub
        End If
    End If
 
'Paste number to first worksheet
'Check if cell C2 is empty
    If Range("C2").Value = "" Then
        Range("C2").Value = userNum
    Else:
'Add user number to last cell in column C
    Range("C1").End(xlDown).Offset(1, 0).Value = userNum
    End If
    
'Paste number to second worksheet
    Worksheets("Second Worksheet").Activate
    
'Check if cell C2 is empty
    If Range("C2").Value = "" Then
        Range("C2").Value = userNum
    Else:
'Add user number to last cell in column C
    Range("C1").End(xlDown).Offset(1, 0).Value = userNum
    End If
    
Application.ScreenUpdating = True


End Sub

I've made an assumption that the button that the user will click to run this sub-routine is going to be on the first worksheet that you'd like the user's number to be appended to and that Column C has a header in cell C1. The code above will need to be amended to suit your specific needs where the second worksheet is concerned. Change the '
Worksheets("Second Worksheet").Activate' part of the code to the name of your 2nd worksheet.

There's some basic validation checking in place to ensure the user enters a number rather than anything else. If they don't enter a number the sub-routine exits with a message telling the user that they didn't enter a number.

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,025
Members
449,060
Latest member
LinusJE

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