Excel VBA Workbook_open event handler required - excel

I have reference data in columns A and B on Sheet2 of my WorkBook. I have a blank sheet - Sheet1 - which i need to fill from my database.
I want to use Excel VBA to fill Sheet1 based on data in columns A and B in Sheet2.
I use the reference data by.
I populate values in column A in Sheet1 from DB.
I check if the values stored in sheet 1, column A exist in Sheet2 column A,
If so, I populate into sheet 1 column B, the corresponding data i've found in Sheet2 column B.

Open excel, do alt + F11 that will open the IDE.
In the right hand window should see vba project and in brackets the name of the excel book you have open.
At the bottom of the list of sheets is an object called ThisWorkbook, right click it, and select View Code.
At top of should see two combo boxes, right hand one should say General click on it and select Workbook from the list.
This should give you the following
Private Sub Workbook_Open()
End Sub
This is the event that is fired each time the work book is opened. Now you will need to add code to reference the sheets in the workbook.
Here is some methods for how to reference sheets in vba Reference sheets. Next step will be to refer to the cells in the sheets see - > excel ranges as an example.

Related

Filling cells of the correct workbook when multiple copies are open

I´m using VBA userforms to fill values from the textboxes into cells of a specific sheet. Lets says the Sheet is named "Sheet1", the textbox is called "TextBox1" and the cell is "C2". I´m currently using
ThisWorkbook.Sheet1.Range("c2").value = Userform1.TextBox1.Value
to fill the cell with the value that I have typed into the textbox. Now I have experienced that when I have multiple copies of the same document opened, it doesnt always put the value in the intended document, but into the copy (which also has a sheet called "Sheet1"). Is there a way around that problem?

Formula To Paste Table In Seperate Worksheet In Excel

I have a table located on Sheet1 called Table2, I just need a way to paste it onto Sheet2 in a certain cell to prepare it for a CSV file. Is there a formula I can use in a cell to display my table in a different sheet?
Table2 dynamically changes from week to week, so I need a formula that'll copy and paste the whole table onto Sheet2.
I've only found how to do a structured table reference, but have not found how to copy and paste the whole table.
In VBA you can use this one line of code in a new module:
Sub CopyTable1ToSheet2A1()
Sheet1.ListObjects("Table1").Range.Copy Destination:=Sheet2.Range("A1")
End Sub
tl;dr; You can insert the table from a connection.
Create your source table if not there already.
Change name if you want, mine is src_table:
Goto "Data -> Get External Data -> Existing Connections"
Goto "Tables" tab and select your table:
In the "Import data" dialog, select how as "Table" and where as wherever you want. In my case I put it in my existing Sheet2 cell A4.
Press Ok
Now if you update the original table in Sheet1, then come back to Sheet2, right click and "Refresh", it'll fetch teh latest data.

Getting data from other sheets with dropdown menu

I want to get the datas from other sheets to the main sheet by selecting the store name from the dropdown menu on main sheet.
So on main sheet, i want people to select the store name from dropdown list and retrieve the product stock datas (all sheets have same product names) and allow them to change the stock values and store them to corresponding store sheets that i will hide.
Here is an example sheet i prepared:
https://docs.google.com/spreadsheets/d/1BtrNKMSgurdft01P9Dw5MEh7rmYWPapwE18yhvDCdYE/edit?usp=sharing
Waiting for your help.
Thanks
You can probably achieve the first part with the INDIRECT function, which lets you convert a String into a Cell Reference or Named Range.
As an example, if you have Worksheets called Sheet1 and Other Sheet, and a dropdown with those names in cell $A$1 of another sheet, then you can retrieve the value from cell $B$2 of those sheets like so:
=INDIRECT("'" & $A$1 & "'!$B$2")
Which will evaluate to one of the following:
=INDIRECT("'Sheet1'!$B$2") > =Sheet1!$B$2
or
=INDIRECT("'Other Sheet!$B$2") > ='Other Sheet'!$B$2
This will not, however, let you send the data back to that sheet without delving into VBA. If you don't want to do that, you might be better off changing your drop-down to a series of Hyperlinks along the top of your worksheets to go directly to the sheets?

Linking Combo Boxes from different Worksheet in an Excel Workbook

I have a workbook with Combo Boxes on each Worksheet that are linked to the same data range. To help prevent bad data entry I would like to link Combo Boxes with the Same Data. For example each worksheet has a Country combo box. When the Country is selected on Sheet1 I want it to set the Country selection on all subsequent worksheets with a Country combo box.
I'm working with Excel 2007.
Any suggestions or assistance would be greatly appreciated. I can get them to update with the selected field but they lose all the other data ie: the other options pulled in from the range of Countries.
Cheers.
When your question is how can I select a country from a combobox in sheet1 and use the same value in all the other sheets in the same workbook, the answer is to reference the cells in the other sheets to the cell in sheet1 where the selection was made. In other words remove the country selection comboboxes from all sheets EXCEPT the first one and let the selected country on the first sheet drive the data on the other sheets (e.g. sheet1$A$1 or even better use a defined name!).
Alternatively, you could create a VBA script (on_change) for every sheet which is executed when the country code cell is changed on that sheet. The script reads the latest selected country, then puts the selected country value in e.g. sheet1$A$1. You would still reference all other sheets from this single controlling cell ($A$1) though.

Insert a Row to a Specific Set of Columns

I'm working on a spreadsheet to act as a master copy for some important information that will be kept track of for a project. On the main sheet I have a table for notes and a table for contacts set up separated by several columns and some VB code to insert new notes or new contacts. However, when I insert a new note I want it to appear at the top of the table and to move all existing notes down a row. Is there any way to add a row to a specific set of columns so that any other data that is kept on cells with the same row, but in a different table, are not moved?
A basic example:
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A2:D2")
rng.Insert Shift:=xlDown
...assuming that you want to insert 1 row of cells above the cells currently located in A2:D2.
You'll have to code a VBA macro that selects all cells in the notes table as a Range object, and then moves them down one row.
I wouldn't know the exact code, but I'd advise you use the "Record Macro" function and do it by hand: select two cells in an Excel sheet, press the right mouse button and click "Insert". Then it'll ask you if you want to move the rows down. You can see in the newly recorded macro what the code is, and adjust it for your specific purpose. Good luck!

Resources