Basic data entry [VBA]
In this small tutorial, I am going to show you how to create basic data entry with a small amount of VBA code.
Cell B3 and C3 are input cells. When you press with left mouse button on button "Add", the data in cell B3 and C3 are copied to the first empty row in the list.
The last step in the code removes the values in cell B3 and C3, see the animated image above.
Table of Contents
1. Data entry - VBA Macro
Sub AddText() Dim Lrow As Single With Worksheets("Sheet1") Lrow = .Range("B" & Rows.Count).End(xlUp).Row + 1 .Range("B" & Lrow & ":C" & Lrow) = .Range("B3:C3").Value .Range("B3:C3").Value = "" End With End Sub
2. How to insert the VBA macro to your workbook
- Press Alt+F11 to open the VB Editor.
- Press with right mouse button on on your workbook name in the Project Explorer window.
- Press with left mouse button on "Insert".
- Press with left mouse button on "Module" to insert a code module to your workbook.
- Copy macro and paste to code module, see image above.
3. Explaining data entry VBA macro
Step 1 - Name macro
Sub AddText()
Step 2 - Declare variables and data types
Dim Lrow As Single
Step 3 - Find last non-empty cell in column B
This line returns the row number of the last non-empty cell in column B:
We want the first empty cell so we add one to the code.
This line of code saves the value to variable Lrow. In the example given above, the code saves the value 8 to Lrow.
Step 4 - Copy values
Let's use the value in Lrow and copy values from cell B3:C3 to the first empty row.
Step 5 - Remove values
The following line deletes the values in cell range B3:C3.
Step 6 - End macro
4. Simplify VBA code further
Worksheets("Sheet1") is repeated many times. The WITH and WITH END statement allows you to simplify the code.
Sub AddText() Dim Lrow As Single With Worksheets("Sheet1") Lrow = .Range("B" & Rows.Count).End(xlUp).Row + 1 .Range("B" & Lrow & ":C" & Lrow) = .Range("B3:C3").Value .Range("B3:C3").Value = "" End With End Sub
5. How to insert a button
- Go to the Developer tab.
- Press with left mouse button on "Insert" button on the ribbon.
- Press with left mouse button on "Button" (Form Control).
- Press and hold with left mouse button on the worksheet.
- Drag with mouse to create the button then release the mouse button.
6. How to assign a macro to a button
- Press with right mouse button on on your new button.
- Assign a macro.
- Select "AddText" macro.
Final note
There is a data entry form in Excel but it is not on the ribbon.
Macro category
This article demonstrates how to add or remove a value in a regular drop down list based on a list […]
In this tutorial, I am going to show you how to add values to a drop down list programmatically in […]
This article demonstrates how to place values automatically to a table based on two conditions using a short macro. Cell […]
Excel categories
6 Responses to “Basic data entry [VBA]”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Could you please show me the code to place the copied data into a different tab instead of below the input cells. Its annoying me and I'm rubbish at this. thanks.
phil,
see this post:
Add values to different sheets (vba)
[...] a previous post I described how to simplifiy data entry. Now it is time to put values in separate [...]
Hi,
First of all thanks its a very great Macro.
But i would like to do a little modification.
What should i change, to copy the values, to the first row, and pushing down the other datas, and not after the last one?
i guess somewhere here:
.Range("B" & Lrow & ":C" & Lrow) = .Range("B3:C3").Value
Thanks:
Márton
you need to for i loop
Hi Oscar,
first of all thank you for all the awesome and detailed tutorials.
I would like to add data to a table, using the method you describe. How can I, using vba, add an empty row at the end of the table and copy the new data there?
Thank you