Archive for March 31, 2020

Excel : Form to Save Data P-1

March 31, 2020 Leave a comment

Learning : VBA Codes to Save Data
Subject: Create Form to collect Data

In a very fast and as simple as we can, we will design and write VBA codes to transform three fields of Data from an Excel sheet “Form” to another sheet “Data”.

To keep thing as simple as we can, we will not use any Validations on user inputs in this example.

First, we will re-name a sheet to “Form” and another one to “Data”. In sheet [Form] we will create a simple Form to collect Names, Company Name and Emails of our customers, also we add a button and call it Save. As in Image-1


For companies Name, We will create a list then we will link it to Cell”D9″ so we can select a “Company Name” from a drop-down list. So to do this First in the Data sheet I will write a list of companies Name as (Comp Name 1,Comp Name 2,Comp Name 3, …. to Comp Name 10). I start my list from Cell “H8” [As in Image-2] you may start from any Cell you want.


Now to link a Dropdown list of our companies Name to Cell “D9″ Do this:
1. Goto Cell”D9”.
2. From the menu we will select “Data” then “Data Validation” and select Data Validation. [Image-3]


3. In the Allow [Select “List”], then in the Source we select the range of Companies we write [Range Cells H8:H17] or just write this: =$H$8:$H$17. Then click OK. See Image-4


Now if we try to click on Cell “D9” we will have a list as in Image-5.


In the “Data” Sheet we will just create the Table Header as in Image-6, and will go-back to “Form” Sheet.


Now we will write the VBA codes and link it to to a “Save” Button we create. To open the Visual-Basic window we select “Developer” From the Top menu, then Press Visual Basic.

Then we write this code and link it to the Save Button. ..

 # VBA Macro code to Save Data to Data sheet 

Sub Save_data()
' Save_data Macro
' Macro recorded 2020-03-29 by HP

    ' Set Variables
    Name = Range("D7").Value
    comp_name = Range("D9").Value
    Email = Range("D11").Value

    ' Goto data sheet
    ' This line will get the Next empty Row in the Data sheet.
    emp_row = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    Range("A" & emp_row).Value = Name
    Range("B" & emp_row).Value = comp_name
    Range("C" & emp_row).Value = Email
    'Go to Form Sheet.
    ' Clear data cells.
    Range("D7").Value = ""
    Range("D9").Value = ""
    Range("D11").Value = ""
End Sub

Now if we enter some data [as we said: No Validation on the data] and press the Save button, the data will be coped to next empty row in the Data Sheet.

Enhancement: In some cases as in our coming project, it’s better to create a sheet and call it “Setting”, then we can have our Lists (such as Company-Name), Colors, Filters all to be in the Setting sheet. [We will see this in the Next Project.]

Follow me on Twitter..

By: Ali Radwani