how to import a sheet from a specific workbook? - vba

I am working on analyzing some data that is basically currency exchange rates and so forth. This data fluctuates daily.
I want to create a workbook where one can select the date. Also, i will have a folder full of data from each date.
Based on the date selected, I would want to import the first two sheets from the file with the name matching the date.
Any help on how to do this in VBA would be very much appreciated.
Thank you very much

You can use simple worksheet copy like this
Sub Importsheet()
Dim anotherWorksheet As Workbook
Set anotherWorksheet = Workbooks.Open("some medium funny sheet path and name")
anotherWorksheet.Sheets("Sheet1").Copy before:=ThisWorkbook.Sheets(1)
anotherWorksheet.Close (False)
End Sub
this is just example, you need to specify your workbooknames, and sheetnames/numbers and also where you wana to place them...

Related

How to use VBA to VLOOKUP across workbooks, sheet by sheet

my apologies in advance - I am relatively novice with VBA
I have a report generated daily, with several sheets (Let's say Sheet1, Sheet2, Sheet3). These sheets have tables where I track notes for each order line, and I would like to vlookup the notes from day to day.
The issue I am facing: I need to be able to repeat this for Sheet1, Sheet2, and Sheet3, pulling from the previous day's respective Sheet1, 2, 3, and repeat this day by day.
Is there a way for the vlookup to recognize my active sheet name, e.g. Sheet3, and look back at the prior day's workbook and pull from that Sheet3?
Is this something that can be done?
Thank you for any help
Before you get where you are trying to go you need a consistent starting point. I would create a new workbook and hold your logic there. You don't need your macro to be in the workbook that your data is.
Like this:
Using named ranges is best in case you want to change the look of your control workbook, and you can create formulas for the file names so that they default to your current day and prior workday.
The code would launch both workbooks. Hopefully this is a valid starting point for you and gives you an idea how to proceed.
Sub mergeThese()
path_New = Range("path_New")
path_Old = Range("path_Old")
file_New = Range("file_New")
file_Old = Range("file_Old")
Workbooks.Open path_Old & file_Old
Workbooks.Open path_New & file_New
End Sub

Create a table using existing data via VBA, using the CURRENTREGION function

So I'm 90% of the way into creating my first big macro project. Thanks for all your help so far everyone! I've come up against an issue however - which I think should be the last issue I should face before putting the macro into use.
I've written a macro which copies the data presented in a master spreadsheet, creates a new workbook in the target location, then creates a table using the information provided.
What I want to do is use CurrentRegion to allow the data to alter month-to-month. I think the code should go something like this
`GeneratePivotTables Macro
'Converts information stored on sheet "Data" to a table
Dim TABLE As Range
Set TABLE = Sheets("Data").A1.CurrentRegion
Sheets("Data").Listobjects.Add(x1SrcRange, Range("TABLE"), ,1Xyes).Name="Data"
I know this as presented is incorrect, but I'd like some help just parsing this correctly! My aim is to then be able to use the table "Data" to create pivot tables.
The following code converts the CurrentRegion around cell A1 into a table. See if you can modify it for your needs.
Sub ConvertRangetoTable()
Dim rngTable As Range
Set rngTable = Sheets("Data").Range("A1").CurrentRegion
Sheets("Data").ListObjects.Add(xlSrcRange, rngTable, , xlYes).Name = "All_Data"
End Sub
This seems to be sufficient
Sub M_snb()
Sheet1.ListObjects.Add(1, [A1].CurrentRegion, , 1).Name = "snb_001"
End Sub

VBA - Copy from Closed File paste to next available row on Summary Sheet

This is my first post. Forgive me if i am doing something wrong here. I will be glad to correct any mistakes. I have found the web site to be very valuable as i am a baby in the field of vba. Please have patience with me.
I am a super rookie in VBA. I am learning as i go but have spent a lot of time on this. I find bits and pieces of information on the web but have trouble in putting them all together. I have learned how to make a vba macro that i can select a file and then run other macros.
I am using Excel 2013.
I complete a time sheet every week(sometimes more at end of month) of the hours i work and the projects i work on. I also include on that sheet when i am out and a code for the reason. I would like to copy three sections to a summary sheet.
Cell D1. This cell always has the date beside it. I would like to copy this to the cell in my first row.
Cells F3-L3 are cells where a code is put. I would like to copy this to the second cell in my first row.
The next range of cells aret the last cells with data in columns F-L. These vary as we have different numbers of rows for work orders each time but are always in columns F-L. I would like to copy this to a second row below the corresponding cells in the first.
For the next file I would like to copy to the next available row in summary.
I would like to copy this data so i can figure vacation days, sick days, etc.
I know i'm asking alot but would be extremely grateful for any help.
i'm giving you this as Example, you will still need to modify...
Option Explicit 'forces Programmer to declare variables
Sub Button_To_Copy () 'link this to a button or other action that launchs the sub
Dim Range_to_Copy as Range
Dim Range_Destination as Range
Dim Sheet_Data as worksheet 'sheet from where we pull the data
Dim Sheet_Destination as Worksheet' Summary Sheet
set Sheet_Data = Thisworkbook.Sheets("Sheet1") 'you might have to rename the sheetname accordingly to its name.
set Sheet_Destination = Thisworkbook.sheets("Summary") ' wild guess, correct to your summary sheet name
Set Range_to_Copy = Sheet_Data.Range("D1")
Set Range_Destination = Sheet_Destination.range("A1")
Range_to_Copy.Copy Range_Destination 'this copies from range A to B (basically A.copy B), but i changed variable names to make it easier...
'more code, copies
'you can simplify without variables like this:
'Sheets("Sheet1").Range("D1").Copy Sheets("Summary).Range("A1") <===== does the same as the above coding
End Sub
Note that i never used activate or select, wich macro recorder will badly use all the time, making bad habits to starters.
Also, by referencing hard cell location like "D1", the code is not dynamic, if you add more data, the sub will have to be changed, so use this just as a start maybe

Moving data based on information in one column and then sorting by data in another

Ok so best way to explain this, I have a spreadsheet that is produced monthly. It needs to be automated. From the report that gets shipped into excel I need to take all the data from the project number column and move it to another workbook. In this workbook are worksheets for 2012 and 2013. I need all of the projects starting 300, then 400, then 500 etc to move into the new workbook and then the 2012 projects to go into one worksheet and 2013 into another. How would I go about automating such a thing?
Thanks for any help
300873 Company Name 10/09/2012
300874 Company Name 10/09/2012
412774 Company Name 30-01-12
412972 Company Name 23-05-12
412986 Company Name 17-07-12
413001 Company Name 08/06/13
413048 Company Name 14-08-12
413049 Company Name 14-08-12
413062 Company Name 20-08-13
413068 Company Name 23-08-12
413169 Company Name 21-09-13
510003 Company Name 27-12-12
600161 Company Name 28-02-12
So with these I would want all of the projects with 2012 to go into one sheet and all the others to go into a 2013 sheet in a sperate workbook, I would also need the 300--- projects to go into the spreadsheet and fill rows underneath the number 300 so this might include adding rows.
you can design a macro for this pretty easily. I'm going to give you some tools to use, give it a try, when and if there is something you're running into come back, post your code, and ask more questions on this question you've already made and I'll help you along with the code so you can learn.
I need to take all the data from the project number column and move it to another workbook.
First, code for Opening a file from your macro:
ChDir "[pathway to the folder]" 'loads file
Workbooks.Open Filename:= "[pathwayToFolder including filename]"
Workbooks("[workbook name]").Activate
Something else that is useful is the ability to specifically refer to different sheets and ranges without actually have to select or go to those sheets. This code, for example, writes something specific in a specific workbook, sheet, and range without going to them
Workbooks("Book1").Sheets("Sheet1").Range("A5") = "Test"
Second, Copying and Pasting:
This code copies all cells and pastes them all on another sheet, you can obviously copy and paste more specific ranges (google it if you need)
Cells.Copy
Sheets("[sheetname]").Select
ActiveSheet.Range("A1").Select
ActiveSheet.Paste
Third, Determining the Year:
If the date is in Column "C" I would just do this
Dim i As Integer, rowNumber As Integer
i = 1 'whatever row your data begins on
rowNumber = ActiveSheet.UsedRange.Rows.Count 'gets used number of rows
Do 'Loops through all data on the sheet
If Right(Range("C" & i), 2) = "12" Then 'checks the last 2 characters in date column
'copy data from that row and paste it wherever applicable
ElseIf Right(Range("C" & i), 2) = "13" Then 'checks for 2013
'move 2013 data wherever applicable
End If
i = i + 1
Loop Until i = rowNumber
Try to start putting this code together into something and get back to us on this question.
Also, in the future try to do some of the research and work on this before asking, the question is actually multiple questions and very broad, making it hard to answer. That is why someone downvoted it.
Hope this helps!

How do I dynamically update the name of a worksheet in Excel?

I have an Excel spreadsheet which is being used to specify filesystem build information for our Unix team.
This worksheet would have different information depending on the system to be built. For example, a production system would have many filesystems, whereas a development system only one or two.
So based on user input, we would populate one or other of the two input worksheet formats ("dev","prod") but the destination worksheet will be always be the same. Only one of the dev or prod worksheets would be visible and active for users to input data at any given time.
How do we tell Excel to use a particular input worksheet to calculate another output worksheet?
My initial idea was that, for simplicity's sake, the "output" worksheet would take its data from an "input" worksheet, and that we would dynamically make either the "dev" or "prod" template become the "input" worksheet. Is this the right way to do this, or is there some better method?
There's a few ways depending on the complexity of the sheet:
If you only have a few worksheets, and users would just select "Production" or "Test", you could set up the conditional logic in the formulas. For example: =if(A1="Test",TestSheet!A1 + TestSheet!A2, ProdSheet!A1 + ProdSheet!A2)
You could utilize the same type of idea as above, but add more complexity using vlookup tables
Your VBA code could create the entire Excel spreadsheet dynamically based on the user input
if you can use VBA code as LuckyLindy suggests, you can do pretty much anything. What I used to do is record macros for various operations, for example adding or renaming sheets and take the generated code and adapt it to do exactly what was required.
An example of simple macro code:
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "My Sheet"
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet5").Select
Sheets("Sheet5").Name = "My Sheet 2"
End Sub
You can record very complex operations and manipulate the generated code and place it behind specific events. So if you wanted the code to fire on worksheet load or activation of a sheet, you can insert it in the relevant block: i.e.
'in Sheet1 - code behind (alt+F11 shortcut)
Private Sub Worksheet_Activate()
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "My Sheet"
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet5").Select
Sheets("Sheet5").Name = "My Sheet 2"
End Sub
HTH
Another way would be to use the INDIRECT formula.
Say you have in cell A1 the name of the sheet you want to look up input from, then the following will look up the value of cell B7 from that sheet:
=INDIRECT($A$1,"!B7")