Copy data from another workbook without hardcoding the file path - excel

I am trying to copy and transpose data from one workbook to another. Each week the file to copy information from is updated to a new workbook. In my macro I callout "Week of 06-17-19 WGN WB A-line.xlsm".
I would like to manually open the sheet to copy the data without having to hard-code the new date.
Could I use the ActiveWorkbook function to call the new open sheet?
Sub copytranspose()
Application.ScreenUpdating = False
Dim i As Integer
Dim Column As Integer
Dim Row As Integer
Row = 5
Column = 8
For i = 1 To 6
Workbooks("Week of 06-17-19 WGN WB A-line.xlsm").Worksheets("WEEKLY").Cells(10, Column).Copy
Column = Column + 2
Workbooks("copy transpose.xlsm").Worksheets("sheet1").Cells(Row,3).PasteSpecial Paste:=xlPasteValues
Row = Row + 1
Next i
End Sub

Solution 1
You can avoid hard-coding in the date into the code by storing the date of the file you want to open in a cell. Say you have a sheet called "config" and in range "A1" you have the date "06-24-19". Assuming you have both files in the same directory you could write something like this
Dim i As Integer
Dim Column As Integer
Dim Row As Integer
Row = 5
Column = 8
Dim currFileDate As String
currFileDate = Format(ThisWorkbook.Worksheets("Config").Range("A1").Value, "mm-dd-yy") '' Get the date typed in
Dim srcDataWB As Workbook
'' Open the workbook automatically with the file date of A1
Set srcDataWB = Workbooks.Open(ThisWorkbook.Path & "\Week of " & currFileDate & " WGN WB A-Line.xlsm")
For i = 1 To 6
srcDataWB.Worksheets("WEEKLY").Cells(10, Column).Copy
Column = Column + 2
'' If this is the same workbook that the code is stored
'' I suggest switching out Workbooks("copy transpose.xlsm") for ThisWorkbook
Workbooks("copy transpose.xlsm").Worksheets("sheet1").Cells(Row, 3).PasteSpecial Paste:=xlPasteValues
Row = Row + 1
Next i
Solution 2
Alternatively, if what you really want is to just match an already opened workbook that matches the pattern of "Week of * WGN WB A-line.xlsm" then the following will work. HOWEVER it's not bullet-proof since you could potentially have two workbooks open that match that pattern.
Sub DoStuff()
Dim i As Integer
Dim Column As Integer
Dim Row As Integer
Row = 5
Column = 8
Dim srcDataWB As Workbook
'' Get the already opened workbook that matches the pattern 'Week of * WGN WB A-line.xlsm'
Set srcDataWB = GetSrcDataWB
For i = 1 To 6
srcDataWB.Worksheets("WEEKLY").Cells(10, Column).Copy
Column = Column + 2
'' If this is the same workbook that the code is stored
'' I suggest switching out Workbooks("copy transpose.xlsm") for ThisWorkbook
Workbooks("copy transpose.xlsm").Worksheets("sheet1").Cells(Row, 3).PasteSpecial Paste:=xlPasteValues
Row = Row + 1
Next i
End Sub
Function GetSrcDataWB() As Workbook
Dim wbName As String
Dim currWB As Workbook
For Each currWB In Application.Workbooks
If currWB.name Like "Week of * WGN WB A-line.xlsm" Then
Set GetSrcDataWB = currWB
Exit For '' No more need to loop
End If
Next
End Function

Related

Excel Marco - How to combine and name datasets

I am trying to consolidate data from a list of file paths into one worksheet then add the names per dataset. I have a list of names and paths set up like this in Excel:
Name1, Path1
Name2, Path2
Name3, Path3
The macro I have written so far loops through the paths, copy and paste into the master spreadsheet starting in the first empty in column B. What I want the macro to also do is also fill in column A with Name1, Name2, and Name3 next to the respective dataset. I got the macro do to the first part but now I can't get it to do the naming part. Here is my code so far:
Sub Data()
Dim ws As Worksheet, dataws As Worksheet
Dim wkb As Workbook, wkbFrom As Workbook
Dim wkblist As Range
Dim fromtab As String
Dim Name As String
For Each wkblist In Sheets("Ref").Range("d4:d18")
If wkblist.Value = "" Then
Exit For
Else
Set wkb = ThisWorkbook
Set wkbFrom = Workbooks.Open(wkblist)
Set ws = wkb.Sheets("Ref")
Set dataws = wkb.Sheets("Data")
fromtab = ws.Range("b22")
wkbFrom.Worksheets(fromtab).Range("b2:z200").Copy
dataws.Cells(Rows.Count, 5).End(xlUp).Offset(1).PasteSpecial xlValues
Application.CutCopyMode = False
wkbFrom.Close
End If
Next wkblist
End Sub
Ta,
I'm not too sure what worksheet you want the names to be pasted; I assume the data WS in the code below. Also, I would set the wkb, ws, and dataws outside of the loop since they are or belong to "ThisWorkbook". Not that it hurts to be inside of the loop, but you're just resetting them each time the loop runs.
The code below should find the range of rows that you just pasted into dataws. Then, it copies the names that are related to the wkblist in Col C into Col A of dataws.
Dim colARow, colBRow As Long
' Place code below after you paste the paths into Col B of the worksheet
' Find first blank in Col A
colARow = dataws.Cells(Rows.Count, 1).End(xlUp).Row + 1
' Find last filled cell in Col B
colBRow = dataws.Cells(Rows.Count, 2).End(xlUp).Row
dataws.Range("A" & colARow & ":A" & colBRow).Value = wkblist.Offset(0,-1).Value

Copy a row of only data then move on to the next worksheet if no values left

I don't know how to create this section of the code. This is a template code that I've used to copy specific values from a cell from each worksheet into one master worksheet that compiles that data usually into one row.
Sub distribute()
Dim sh As Worksheet
Dim destsh As Worksheet
Dim i As Integer
Set destsh = ActiveWorkbook.Worksheets.Add
destsh.Name = "Master"
i = 1
For Each sh In ActiveWorkbook.Worksheets
***destsh.Cells(i, 1).Value = sh.Range("B7:B90").SpecialCells(xlCellTypeConstants).Select***
i = i + 1
Next
ActiveWorkbook.Worksheets("Master").Cells.EntireColumn.ColumnWidth = 30
ActiveWorkbook.Worksheets("Master").Cells.EntireRow.AutoFit
ActiveWorkbook.Worksheets("Master").UsedRange.UnMerge
ActiveWorkbook.Worksheets("Master").UsedRange.WrapText = False
End Sub
I want my code to go through each worksheet in my workbook and copy the column from the range B7:B90 and stop where there are no more values within each row of each worksheet, then moves on to the next worksheet. Some worksheets have 10 cells in one row, others have 60; in this scenario my master file would show 70 cells in column A from both worksheets. The code creates a master worksheet that compiles the worksheets row B in one column.
The section I need help with has been asterisked
Your code creates the Master sheet every time which will cause the program to fail when it tries to name the sheet in the next run. Also, in your iteration through all sheets, you are reading data from Master sheet as well which might give incorrect results. I can quickly think of below code. I have used the numerical notation for rows and cells. Also since the range is constant, i looped through the range that you specified
Sub distribute()
Dim sh As Worksheet
Dim destsh As Worksheet
Dim i As Integer: i = 1
Dim sheetName As String: sheetName = ""
Set destsh = ActiveWorkbook.Worksheets.Add
'Taking sheet name as input from user
sheetName = InputBox("Enter sheetname to aggregate data")
'Checking if sheetname was entered properly
If (sheetName <> "") Then
destsh.Name = sheetName
ActiveWorkbook.Worksheets("Master").Cells.EntireColumn.ColumnWidth = 30
ActiveWorkbook.Worksheets("Master").Cells.EntireRow.AutoFit
ActiveWorkbook.Worksheets("Master").UsedRange.UnMerge
ActiveWorkbook.Worksheets("Master").UsedRange.WrapText = False
masterSheetRow = 1
For Each sh In ActiveWorkbook.Worksheets
'Making sure that the sheet is not the master sheet while getting rows
If (sh.Name <> sheetName) Then
For i = 7 To 90
If (sh.Cells(i, 2).Value <> "") Then
destsh.Cells(masterSheetRow, 1).Value = sh.Cells(i, 2).Value
masterSheetRow = masterSheetRow + 1
End If
Next
End If
Next
Else
MsgBox ("Enter valid sheetname")
End If
End Sub

Copy values to Workbook 1 (Main Workbook) from Workbook 2 when a cell value in Workbook 1 matches to column value in Workbook 2

I am working on a code to copy data to Workbook 1 (Main Workbook) from Workbook 2 based on a criteria.
The criteria is - If the value of cell C11 in Workbook 1 (Main Workbook) is equal to column A of Workbook 2, then copy all the data from Workbook 2 columns A to F to Workbook 1 (Main Workbook). Please note that there could be multiple matching values (in Workbook 2) that may need to be copied to Workbook 1.
I have tried the below code that pull all the data perfectly. Now I am trying to see if there is a code which can be applied to copy data based on criteria.
Private Sub CommandButton1_Click()
' Get Tiger calendar workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the Tiger calendar workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select the Tiger Calendar file"
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' assume range is A1 - M10000 in sheet1
' copy data from Tiger calendar to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets("Sheet1")
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
targetSheet.Range("B14", "G500").Value = sourceSheet.Range("A2", "G500").Value
' Close customer workbook
customerWorkbook.Close
End Sub
For example, if Cell C11 in Workbook 1 = 1232223 (Product ID), then the data copied should be all the sales details related to the product ID. The bulk data is available in Workbook 2.
Who Is Who, What Is What
In this workbook check cell C11 against the values in source workbook's A column. When found, copy the range of the row and 6 contiguous columns (A-F) to this workbook starting from B14 (B-G). Do all of this until the last row of data in source workbook is reached.
Private Sub CommandButton1_Click()
Const filter As String = "Text files (*.xls*),*.xls*"
Const caption As String = "Please Select the Tiger Calendar file"
Const wsTarget As Variant = "Sheet1" ' Target Worksheet Name/Index
Const cTgtFirst As String = "B14" ' Target First Cell Range
Const cTgtSearch As String = "C11" ' Target Search Value Cell Range
Const wsSource As Variant = 1 ' Source Worksheet Name/Index
Const cSrcFirst As Long = 2 ' Source First Row
Const cSrcFirstCol As Variant = "A" ' Source First Column Letter/Number
Const cColumns As Integer = 6 ' Number of Columns
Dim customerFilename As String
Dim sourceSheet As Worksheet
Dim i As Long
Dim rngTarget As Range
customerFilename = Application.GetOpenFilename(filter, , caption)
Set sourceSheet = Workbooks.Open(customerFilename).Worksheets(wsSource)
With sourceSheet
Set rngTarget = ThisWorkbook.Worksheets(wsTarget).Range(cTgtFirst)
For i = cSrcFirst To .Cells(.Rows.Count, cSrcFirstCol).End(xlUp).Row
If .Cells(i, cSrcFirstCol) = rngTarget.Parent.Range(cTgtSearch) Then
.Cells(i, cSrcFirstCol).Resize(, cColumns).Copy _
rngTarget.Resize(, cColumns)
Set rngTarget = rngTarget.Offset(1, 0)
End If
Next
End With
sourceSheet.Parent.Close False
End Sub
You're going to want to add an If statement at the end. I havent tested this, but it should give you an idea of how to get this working.
'Your need to change this to what you need
Dim CustomerSheet = Customerworkbook.Worksheets("Sheet1")
If Customersheet.range("C11").value = targetSheet.range("A1").value then
targetSheet.Range("B14", "G500").Value = sourceSheet.Range("A2", "G500").Value
Else
Exit Sub
End If

excel code to print multiple sheets based on formula

I have to print multiple sheets in excel based on whether that sheet is relevant to a user or not. In a sheet called "Sheet2" I have in column A from cell 14, the sheet names. There are 24 sheets with different names in Column A. In each adjacent cell (in Column B), I have either True or False. I wish to print the sheets which have a True in column B. I am able to print to the sheets one by one and not all the relevant sheets simultaneously as one file. The code I use is as follows:
Sub CommandButton1_Click()
Dim wb As Workbook
Dim SheetsToPrint As String
Set wb = ThisWorkbook
Dim sheetWithData As Worksheet
Set sheetWithData = wb.Sheets("Sheet2")
Dim startNameRange, endNameRange As Integer
startNameRange = 14
endNameRange = 39
For i = startNameRange To endNameRange Step 1
Dim nameSheetToPrint As String
SheetsToPrint = sheetWithData.Range("A" & i).Value
Dim wsToPrint As Worksheet
Set wsToPrint = wb.Sheets(SheetsToPrint)
If sheetWithData.Cells(i, 2) = "True" Then
wsToPrint.PrintOut From:=1, To:=1
End If
Next i
End Sub
Adapt the vars and try this :
Sub PrintSheet()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim sheetWithData As Worksheet
Set sheetWithData = wb.Sheets("Sheet2")
'Add the first row number and the last row number where start the sheet name
Dim startNameRange, endNameRange As Integer
startNameRange = 2
endNameRange = 4
For i = 2 To 4 Step 1
Dim nameSheetToPrint As String
nameSheetToPrint = sheetWithData.Range("A" & i).Value
Dim wsToPrint As Worksheet
Set wsToPrint = wb.Sheets(nameSheetToPrint)
If sheetWithData.Cells(i, 2) = "True" Then
wsToPrint.PrintOut
End If
Next i
End Sub

Inserting a copied range (from closed workbook) into named range of open workbook

I want to be able to insert data from a closed workbook (x) into a Named Range ("YearlyData") of my active workbook (y). This range is in a worksheet called "Destination".
However, the named range "YearlyData" has a Title Row (namely row 1 of the range) and the actual 'raw' data starts from Row 2.
What I want to do is to shift down the contents from Row 2 onwards by the exact amount of rows that are to pasted from the source workboox (x).
Here is what I have so far:
Option Explicit
Sub DataFromClosedFile()
On Error GoTo ErrHandler
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim x As Workbook
Dim y As Workbook
Dim CA_TotalRows As Integer
Dim CA_Count As Integer
x is the closed, source workbook y is the current active workbook into which I will paste the data
Set y = ThisWorkbook 'ActiveWorkbook
Set x = Workbooks.Open("PATH", True, True)
This is the named range, into which I want to copy the data
Dim YearlyData As Range
Set YearlyData = y.Worksheets("Destination").Range("YearlyData")
Next, count the number of rows that need to be copied:
CA_TotalRows = x.Worksheets("August_2015_CA").UsedRange.Rows.Count
And HERE is where I need to change the code.
I need to insert the right number of rows between row 1 and 2 of named range "YearlyData" and then I need to paste the data into those rows from the closed workbook.
In addition to that, I only want to copy columns A:B and E:H from the source databook.
I have read posts where users suggest using '.Insert Shift:=xlDown' but I didn't manage to make it work.
So below is my old code using "Sheet3", instead of "Destination" which works - but obviously doesn't copy it into the named range "YearlyData" which is what I actually want to do.
Please note that I start copying data from row 2 of the source workbook (x) because I don't want to copy the titles, only the raw data.
y.Worksheets("Sheet3").Range("A1:B" & CA_TotalRows - 1).Formula = x.Worksheets("August_2015_CA").Range("A2:B" & CA_TotalRows).Formula
y.Worksheets("Sheet3").Range("C1:F" & CA_TotalRows - 1).Formula = x.Worksheets("August_2015_CA").Range("E2:H" & CA_TotalRows).Formula
Any suggestions will be much appreciated!
x.Close False
Set x = Nothing
Application.Calculation = xlCalculationAutomatic
ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
You don't really say how big Range("YearlyData") is so this could potentially insert extra rows you don't need. Also, not sure why you did .formula. I changed it to .value.
Sub DataFromClosedFile()
Dim x As Workbook
Dim y As Workbook
Dim yws As Worksheet
Dim xws As Worksheet
Dim CA_TotalRows As Integer
Dim CA_Count As Integer
Set y = ThisWorkbook 'ActiveWorkbook
Set x = Workbooks.Open("PATH", True, True)
Set yws = y.WorkSheets("Destination")
Set xws = x.WorkSheets("August_2015_CA")
Dim r As Long
r = yws.Range("YearlyData").Cells(1, 1).Row
CA_TotalRows = xws.UsedRange.Rows.Count
yws.Rows(r + 1).EntireRow.Resize(CA_TotalRows).Insert
yws.Range(Cells(r + 1, 1), Cells(r + CA_TotalRows - 1, 2)).Value = xws.Range("A2:B" & CA_TotalRows).Value
yws.Range(Cells(r + 1, 3), Cells(r + CA_TotalRows - 1, 6)).Value = xws.Range("E2:H" & CA_TotalRows).Value
x.Close False
Set x = Nothing
End Sub

Resources