Excel macro for automatic data entry - vba

I need a macro to help me with data entry in Excel. Basically I want sheet 1 for data entry, and sheet 2 for the data that are entered. Sheet 1 will only have one row for data entry, and once that row is filled you hit enter. The row is automatically added to the table in sheet 2, and the row is cleared on sheet 1. Now you are ready to enter another entry in sheet 2.
So to summarize sheet 2 will have multiple rows for the data entered, and sheet 1 will only have 1 row because it automatically clears it's row after each entry.
Is something like this possible?? If you guys can post some code for me it would really help, and keep in mind I have never programmed in VBA before. Thanks in advance!!

Place a button on your sheet 1, then put this macro into a regular code module and attach it to your button. Assuming you are entering values across row2 (row1 being titles or such), then this would transfer row2 data to the next empty row on sheet2:
Sub Transfer()
Rows(2).Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
Rows(2).ClearContents
End Sub

I'm not totally sure of the use-case, but have you considered using Excel UserForms instead.
The input would be a form that actually appears in front of the user which would collect input. When they hit the enter button, you could have underlying code to update the main sheet, Sheet 2.
http://www.excel-vba-easy.com/vba-userform-excel-vba.html

You could also use the database form. set up your database sheet then Data > Form (in 2003)

In order not to have to "click" on a button, I would suggest to look into this function:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
This macro will run every time something in your sheet has changed.
It might be that you have to check if there is data in cell A1, as this VBA might be triggered both after hitting enter and after the data has been transported to the other sheet.

Related

VBA: Mirror a cell to another sheet, then keep the data after deleting the original

Apologies if this has been answered, but I couldn't find any details of this being asked before. I know it's a vague question without code, but just looking for ideas of how to approach this. Writing VBA code on Excel.
I have a sheet, which contains 20+ columns and 8,000+ rows of data. The sheet is password protected / read only as I do not want users editing the data (the sheet is linked to several other macros). Called 'Master Data Sheet'.
I have created a macro that will filter the Master Data table and then paste the filtered results on another sheet (called 'Filtered Results') and combines data with another separate sheet. The user can view and edit this sheet.
However there are two columns that I want the user to put data in that needs to be duplicated/mirrored in the Master Data Sheet (in those specific columns) automatically. Issue I'm having is that the 'Filtered Results' sheet gets deleted after a period of time, but I still need the user input data for those specific columns to be in the Master Data Sheet.
I'm unsure how to get around the issue of the sheet being deleted but data being retained.
An alternative workaround I had would be a copy/paste button the user could click to copy the unformatted data from the Filtered Sheet to Master Data Sheet. However, I was hoping to find a solution that would just automatically update as the user types (in case they forget to click the button etc.)
Thanks for all the help!
EDIT: Code I'm working with right now. Still stuck on linking filtered cells though.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents=False
If Not Intersect (Target, Range("D1:D1000")) Is Nothing Then
Target.Copy Destination:=Worksheets("Master Data
File").Range(Target.Address)
End If
Application.EnableEvents=True
End Sub
You can use the Worksheet_Change event to check if a specific range changed and then copy/update the other sheet. This way you have the copy/paste automatically when the original data changed.
Example:
If you want to copy range A1:A5 into another sheet whenever its values changed:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A5")) Is Nothing Then 'range A1:A5 was changed
Target.Copy Destination:=Worksheets("OtherSheet").Range(Target.Address)
End If
End Sub

Transferring information from input sheet to master sheet

I am trying to move data from one workbook 'input worksheet' to another workbook 'master workbook'. Both sheets are in the same file and if possible, it would be great if both files didn't have to be open at the same time in order to transfer the data but the master workbook would autosave once the data was transferred across. Links to images of the files below to make it easier to understand what I am trying to do.
The data in the input worksheet is in row 6, columns A-J with each user inputting details of the tasks they get asked to complete. I would like when a button is clicked, the data from the input worksheet is transferred into row 2, columns B-K in the master workbook so that each time a new task is entered and transferred across, it appears in the row below (so that it can be pivoted later, etc.).
http://i.stack.imgur.com/b2cyI.jpg - input sheet
http://i.stack.imgur.com/JZr0a.jpg - master sheet
Use the macros here to get the last row in the master sheet.
Then simply write the values from the input sheet to the corresponding cell in the master sheet.
That is all. This is how you refer cells:
tbl_master.cells(1,3) = tbl_input.cells(3,5).value
Make sure that the row in the tbl_input is a variable, coming from the function, calculating the last row. Give it a try!
Edit:
This is what I use for last row:
Public Function last_row_with_data(ByVal lng_column_number As Long, shCurrent As Variant) As Long
last_row_with_data = shCurrent.Cells(Rows.Count, lng_column_number).End(xlUp).Row
End Function
If you want to find the last row of column B of sheet "tbl_main" you call it like this:
last_row_with_data(2,tbl_main)
Edit2:
Change the names of your sheets here, and reference them by their names.
In order to get this window, select the sheet on the left and press F4.

Pasting same cell on different rows as value is replaced

This sort of follows up from my previous few questions on the same workbook.
I have two sheets, the first being Car Search, which contains a form (NOT a VBA form, just a normal table that appears like a form) to fill in. The second sheet is Raw Data, and will contain all the information entered in the Car Search sheet. It will be displayed row by row (see 2nd image).
In the Raw Data sheet, I am using the formula =""&'Car Search'!B3 to copy the contents of cell B3 in the Car Search spreadsheet.
My question is: If I had a new Car ID value, how can that automatically be entered into the row below?
Essentially, I am trying to use the form to capture all data for new cars coming in, and then I would like all that data to appear in the second sheet in their respective rows/columns.
Any help much appreciated! :)
EDIT:
Good news!
You need to use VBA:
Sub Range_Copy_Examples()
Worksheets("Car Search").Range("B3").Copy Worksheets("Raw Data").Range("=OFFSET(Sheet3!B1,0,0,COUNTA(B1:B300)+1,1)")
End Sub
However there is a small bug where it keeps pasting across past values, so I have suggested an alternative macro below
Re-edit:
A more mechanical way to make macro work (hopefully someone can improve on it) looks like this:
Sub Macro1()
Sheets("Car Search").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Raw Data").Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Worksheets("Car Search").Activate
End Sub
This works as follows:
in Car Search, have cell B3 selected the whole time
in Raw Data, select the cell with the last value in the column where you want you Car Search!B3 data pasted (you only have to do this once)
press Run on the macro (easy enough to just record one yourself)
go back to Car Search and change the value of B3, press Ctrl-Enter to keep the same cell selected after changing value, then press the same macro button without changing a thing.
If someone could add so that when pressing Ctrl-Enter on Car Search!B3 the Raw Data gets automatically added without having to manually run the macro, it would be fully automated!

VBA Active X button to take user to first cell of updated row

I have a spread sheet which has a button that transfers all the entered data into another sheet. I have then made a second button that takes you to this log should you want to view it, however it takes you too the last cell filled. The log has a lot of columns so i want to make the 'go to log' button to take you to the first cell in the last row that was filled.
all i have at the moment is a simple formula to take you there.
Private Sub CommandButton2_Click()
ThisWorkbook.Sheets("Log").Activate
End Sub
If you are sure that your code is taking you to last Cell filled, and you are expecting the control to go to the first cell of the row then below piece will do.
Cells(ActiveCell.Row, 1).Select

Is it possible to have a cell that has a formula and accepts entry at same time in excel?

Example:
A B
1 =vlookup(XX)
2
3
in cell A1 there is a Vlookup formula, Is it possible to enable user entry in this cell and override the formula then later restore the formula automatically when sheet is open again?
Even through VBA
Short, boring answer: nope.
A cell only ever has a keyed-in value, or a calculated formula. Can't have both.
Longer answer: maybe.
Shift everything 1 row down, and use row 1 to store your "original" formula - then hide that row (and pray the user isn't going to mess with it).
When the sheet is opened again sounds like you're confusing "workbook" and "worksheet" - you need to handle Workbook_Open if you want to run code when a workbook opens. Workbooks contain worksheets - it's the workbook that opens, not the sheets (sheets activate, but I doubt you would want to put that logic in there).
So, in the handler for Workbook_Open, write code that takes the formula in the hidden row and overwrites whatever is under it.
Another solution can be to hard-code the formula in the VBA code.
One possibility would be to store your Workbook as a template. Normally when a user opens the workbook by double-clicking, it will open whole new workbook based on the template, and they can modify it to their heart's content, save it, mail it to Grandma, etc.
The next person who comes along will double-click the template file and get the formula again, just as you designed it.
Short answer: Kind of, sort of
Long answer:
Save your workbook as a template. Every time someone will use it you'll see the orignal with formula, then if someone write over the formula, when using save your original will be kept intact.
What You need to do is:
press Alt + F11
select ThisWorkbook and paste this code:
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("A11").Value = "asdf"
End Sub
Every time the workbook is opened, this script will run.
Instead of "Sheet1" you can write the name of the sheet you want to apply the script.
Inside the Range, You can define the cells you want to modify, You can use even multiple cells. Check this for more information about this.
After Value You can write what You want to be written inside the cell. You can write "=vlookup(XX)" and it will work.