Excel - When saving sheet1, write certain cells to sheet2 - vba

I am very new to vba and need your assistance in writing a code that will do the following
1) I have a workbook titles "workbook.xlms" which I have saved in my documents
2) The workbook has got 2 sheets, namely sheet1 and sheet2
3) Sheet1 has predefined fields such
as "name", "surname", "address", "contact", "social security", "race" and so on
4) After filling in the predefined fields with required data and clicking save, some (or all) of the fields must be written to empty row on sheet 2 sheet2.
5) If new information is filled in on sheet1 and the social security number entered is already registered in sheet2, then new data must not be written to sheet2 (To avoid duplicates).
I do not have any code to share with you and am hoping you can help

Start by recording the copy/paste portion of the macro and view the code. See if you can modify that to suit your needs. Post back here with what you tried if you still can't make it work.

Related

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.

Update master workbook with another workbook

There is lots of questions about copying the content of one workbook to another that I'm aware of.
I would like to make a code that updates the workbook by importing the content from the other workbook without rewriting cells, just adding values. I want to make it that way in case the other workbook is corrupted or have some data deleted.
Some rows can be partially filled but the macro will fit the correct information in the right row.
Have you considered trying to copy over the workbook as a different name?
Imagine you start with
MasterWB -> sheet1
ChildWB -> sheet1
You copy over such that you have this worksheets.
MasterWB -> sheet1 , sheet2(childbook)
From there it is pretty easy to do a concat on a third page. First create a third sheet. Next, concat the values on sheet3 with values from sheet2 and 1
MasterWB.sheet3.cellA1.value = concat(sheet2.cellA1.value, sheet1.cellA1.value)
Sorry for the bad syntax, hope this helps!

I need to generate new workbooks using existing sheets that I will autopop with data

I have two sheets on my template. Sheet one contains a SKU in column A and a descr. in column B
On sheet 2, I have a pre-written template that has generic SKU and Description. That means under the item sku, it says D999 and under the description it says xxx.
I want to replace "999" with the value in A2 on my first sheet, but the 999 is in multiple columns through the sheet. Next, I want to replace the "xxx" with the data in B2 from sheet 1. I can't seem to find a VBA code that will do this specifically.
Lastly, once the find and replaces are done, I need to have it save me a new workbook using sheet2, in the same format, and name it as the value in A4 on sheet 2. I think you should be able to find the workbook here My Workbook
Basically, I want to be able to enter a bulk amount of new SKUs and Description, and be able to generate a new workbook for each SKU I enter. Right now I have 78 waiting to have sheets made.
In the cell to the right of the cells where you want to type in the SKU, add the following formula: =Match(A2, LookupsheetName!B:B,0). This will return the row the SKU was found on. In the next cell to the right use the Index function: =Index(LookupSheetName!A1:B1000, B2, 0). This will bring the label in column B to the current page. The Match function locates the row the SKU was found on.
Try these for looking up the information, because they are more efficient than VLookups.
The code for copying a worksheet to another workbook can be created using the Macro recorder on the Developer tab. Turn the recorder on then do the steps to manually move/copy to another workbook. When done, open the VBA editor and figure out how to save the workbook to a new name each time.
Good luck.

Hyperlink after Vlookup

This simple thing is eating my brain out. Here is the challenge:
I have 2 sheets in the same workbook. Say Sheet 1 and Sheet 2. I want to Vlookup for the value present in Sheet 1 Column A and Sheet 2 column B. If the value is found i want to put a hyperlink in Sheet 1 Column A to point it to Sheet 2 Column D. In simple words, there are program names present in both the sheets and associated risk in sheet 2. If the program is present in both the sheets then i want to navigate directly to the risk present in the Sheet 2 upon clicking the program name in Sheet1.
I am attaching the sample file with this thread. Please somebody help me. I have tried with vba and normal formulas. Nothing is being worked out.
SAmple file can be downloaded from here
Here's how you can do that with a formula. You'll need to change the workbook name in the HYPERLINK formula to match yours.
=IF(ISERROR(VLOOKUP($A1,Sheet2!$B:$B,1,0)),"",HYPERLINK("[StackTest.xlsx]'Sheet2'!D"&TEXT(MATCH($A1,Sheet2!$B:$B,0),"#"),"Click Here"))

Excel VBA - Pull data from external books and paste into the next blank row in multiple tabs

I'm working on automating a workbook in Excel, and I'm running into a few issues with my VBA code. I'd paste it here, but I've been through so many iterations, it's pretty unusable.
The goal is to have the active book 'grab' the data out of several workbooks containing raw data when it is opened, and put the copied data into a few tabs that can be used to populate various charts on a dashboard tab. Each workbook containing raw data should go into its own separate tab within the active workbook. Broken into steps, I am thinking I need the below process to occur:
Open Active Book
Open hidden tab 'Sheet1'
Open raw data book 1(e.g. c:\Raw Data.xls)
Copy data from specified location (e.g. [Raw Data.xls]Sheet1!$A$3:$AE$64) in the raw data book 1
Paste copied data into Active Book, into specific worksheet, into first empty row (e.g. [Active Book.xls]Sheet1!first empty row)
Hide tab 'Sheet1' in Active Book
Close raw data book 1
Repeat process using raw data book 2 and sheet2 of Active Book
Repeat process using raw data book 3 and sheet3 of Active Book
Only after data is populated into the destination tabs (Sheet1, Sheet2, Sheet3 in the Active Book), can the user interact (click into cells, change tabs, etc) with the workbook
I know this is simple - I'm getting frustrated, as I'm a newbie and the syntax (and multiple variations of syntax) is really throwing me for a loop. Any help would be greatly appreciated. Many thanks in advance!!
Where are you getting stuck? Briefly:
Open Active Book
If its active, it's already open? Reference with object 'ThisWorkbook'
Open hidden tab 'Sheet1'
You don't need to unhide sheets to "VBA" them... If you're going to be creating new sheets for each datafile I'd recommend creating new via:
Set wsDestination = thisworkbook.sheets.add
wsDestination.visible = xlSheetHidden ' Might as well hide it now
If you're wanting to match specific raw data files with specific worksheets, maybe use a "select"
Open raw data book 1(e.g. c:\Raw Data.xls)
Set wbSource = Application.Workbooks.Open("c:\Raw Data.xls")
Set wsSource = wbSource.sheets(1)
'If need Select (see above):
Select case wbSource.name
Case "Raw Data A.xls" ' If line above changes to .Open(Workbooks[i]) or something
set wsDestination = sheet1
Case "Raw Data B.xls"
set wsDestination = sheet2
'...
End Select
Copy data from specified location (e.g. [Raw
Data.xls]Sheet1!$A$3:$AE$64) in the raw data book 1 Paste copied data
into Active Book, into specific worksheet, into first empty row (e.g.
[Active Book.xls]Sheet1!first empty row)
intBlankRow = Application.WorksheetFunction.Counta(wsDestination.columns(1)) + 1 ' Note this only works if there are no blanks in column 1 otherwise you'll need another method: there are some good tricks if you google "find last cell vba"
wsSource.cells.copy wsDestination.cells(intBlankRow,1) ' No need to copy and paste in seperate lines: you can just pass destination to the copy function
Hide tab 'Sheet1' in Active Book
You don't need to
Close raw data book 1
wbSource.close
Repeat process using raw data book 2 and sheet2 of Active Book Repeat
process using raw data book 3 and sheet3 of Active Book Only after
data is populated into the destination tabs (Sheet1, Sheet2, Sheet3 in
the Active Book), can the user interact (click into cells, change
tabs, etc) with the workbook
Wrap the whole thing in a big loop... If you wanna do it the easy way, create an array with your workbook names and iterate through it. Otherwise present the user with a FileDialogue so they can select the workbooks they want to import... this ofcourse depends on what I was saying above: do you want to create new worksheets each time or import specific raw data in to specific sheets...