Create copies of original workbook inputting new data from list - excel

Trying to copy my original worksheet but with different data for as many items in a list/array.
My original worksheet is formatted in such a way it references the cells in the data array worksheet.
For example
TEST TIME Output1 Output2
A 3 5 9
. . . .
. . . .
. . . .
Z 2 9 4
Above would be something like my data array worksheet and I would reference the cells to the formatted worksheet. The end result would be 26 worksheets named A test, B test, ..., and Z test.
Is there a way to copy a worksheet over and over creating new reference cells going down an array?
What I have:
Sub AddSheets()
Dim cell As Excel.Range
Dim SCHPipe As Excel.Worksheet
Dim MacroTBF1 As Excel.Workbook
Set SCHPipe = ActiveSheet
Set MacroTBF1 = ActiveWorkbook
For Each cell In SCHPipe.Range("B12:B15")
With MacroTBF1
.Sheets("OriginalTBF").Copy after:=.Sheets(.Sheets.Count)
End If
Next cell
End Sub
I do not know how to reference the data to each sheet being created. This keeps getting an invalid error message.

Something like this, you will need to adjust for the layout of your data and template sheet:
Sub AddSheets()
Dim cell As Range
Dim SCHPipe As Worksheet, shtOrig As Worksheet
Dim MacroTBF1 As Workbook, shtNew As Worksheet
Set SCHPipe = ActiveSheet
Set MacroTBF1 = ActiveWorkbook
Set shtOrig = MacroTBF1.Sheets("OriginalTBF")
For Each cell In SCHPipe.Range("B12:B15")
If cell.Value <> "" Then
shtOrig.Copy after:=MacroTBF1.Sheets(MacroTBF1.Sheets.Count)
Set shtNew = MacroTBF1.Sheets(MacroTBF1.Sheets.Count)
With shtNew
.Name = cell.Value & " Test"
.Range("A1").Value = cell.Value
.Range("A2").Value = cell.Offset(0, 1).Value
.Range("D49").Value = IIf(cell.Offset(0, 1).Value = "-", _
cell.Offset(0, 5).Value, "")
End With
End If
Next cell
End Sub

Related

Pull data from certain WS and paste to another WS

Need help with my code please. I want to search all worksheets in a workbook that contain a specific string in its sheet name to copy cell data of a range and paste as values into a different ws. I keep getting Run-Time error '9' subscript out of range. It highlights Set wsSumm = ThisWorkbook.Sheets("Summary") as the reasoning. I have a Summary tab so I am unsure why it is giving this error.
What I ultimately need to do is take data from A2 of all BL ws and paste into Column A of Summary ws. Then take A1 of all SL ws and paste into Column B of Summary ws. I would need to paste as values. My sheets are named 1-15 as BL, SL (BL1, SL1, BL2, SL2, BL3, SL3, ect) and a Summary ws. Below is what my Workbook looks like and the Code I am using.
[enter image description here][1]
Option Explicit
Sub Macro1()
Dim wsSumm As Worksheet, ws As Worksheet
Dim strCol As String
Dim lngRow As Long
Application.ScreenUpdating = False
Set wsSumm = ThisWorkbook.Sheets("Summary") '<-Sheet name for the data to be concolidated. Change to suit.
For Each ws In ThisWorkbook.Sheets
If ws.Name <> wsSumm.Name Then
strCol = IIf(StrConv(Left(ws.Name, 2), vbUpperCase) = "BL", "A", "B")
lngRow = IIf(StrConv(Left(ws.Name, 2), vbUpperCase) = "BL", 2, 1)
wsSumm.Range(strCol & Rows.Count).End(xlUp).Offset(1, 0).Value = ws.Range("A" & lngRow)
End If
Next ws
Application.ScreenUpdating = True
End Sub

Copy range of values between workbooks

I am trying to copy either single cells values or rows of cells from a source workbook to a target workbook.
The user will have three workbooks open:
Dashboard workbook
Source workbook
Target workbook
The sub reads the user input in Dashboard workbook, which will look like the following:
Source cells Target cells Cell/Row
G28 H30 Cell
G29 H31 Row
The sub is then supposed to look up cell G28 in Source workbook and copy that into H30 in Target workbook. Likewise the sub is supposed to look up cell G29 in Source workbook and copy that cell and everything to the right into H31 in Target workbook.
I managed to copy single cell values. I have not been able to implement the functionality for the row type input.
I indicated below where the error is.
Sub transferSub()
Dim wbMain As Workbook: Set wbMain = ThisWorkbook
Dim wbMainDashboard As Worksheet: Set wbMainDashboard = wbMain.Worksheets("Dashboard")
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
'Definition of file path for source and target workbooks
sourceModel = wbMainDashboard.Range("FILE_SOURCE") 'Pull from dashboard input
targetModel = wbMainDashboard.Range("FILE_TARGET") 'Pull from dashboard input
'Source and target workbooks
Dim wbSource As Workbook: Set wbSource = Workbooks(sourceModel) 'Workbook already open
Dim wbTarget As Workbook: Set wbTarget = Workbooks(targetModel) 'Workbook already open
'Source and target worksheet
Dim wskpInput_source As Worksheet: Set wskpInput_source = wbSource.Worksheets("INPUT (kp)")
Dim wsSCEInput_source As Worksheet: Set wsSCEInput_source = wbSource.Worksheets("INPUT (SCE)")
'Source and target worksheet
Dim wskpInput_target As Worksheet: Set wskpInput_target = wbTarget.Worksheets("INPUT (kp)")
Dim wsSCEInput_target As Worksheet: Set wsSCEInput_target = wbTarget.Worksheets("INPUT (SCE)")
'Procedures
Dim rng As Range: Set rng = wbMainDashboard.Range("Dashboard!E9:E15")
Dim i As Integer
For i = 1 To rng.Rows.Count
cell_source = rng.Cells(i, 1)
cell_target = rng.Cells(i, 1).Offset(0, 1)
cell_cellrow = rng.Cells(i, 1).Offset(0, 3)
If cell_cellrow = "Cell" Then 'If cell then copy paste value in that cell
wskpInput_target.Range(cell_target) = wskpInput_source.Range(cell_source).Value
ElseIf cell_cellrow = "Row" Then 'If row then copy and paste the row of cells
wskpInput_source.Range(cell_source, cell_source.End(xlToRight)).Copy _
wskpInput_target.Range(cell_target) '---NEED HELP WITH THIS PART---
End If
Next
End Sub
Well, the Range object can either get Cells as arguments or a String (details here).
Hard-coding the range with a string argument would look like this:
wskpInput_source.Range("G28:L28").Copy _
destination:=wskpInput_target.Range(cell_target)
but since you already have a variable containing the first cell ("G28") in the row, we only need to find the last cell, you can get it with a Function like the following:
Function GetLastCellInRow(sheetName As String, firstCell As String) As String
Sheets(sheetName).Range(firstCell).End(xlToRight).Select
GetLastCellInRow = ActiveCell.Address
End Function
and this is how you call it
'MySheet is the source sheet, so you need to modify that
cell_source_last = GetLastCellInRow(MySheet.Name, cell_source)
And putting all together:
cell_source = rng.Cells(i, 1)
cell_target = rng.Cells(i, 1).Offset(0, 1)
cell_cellrow = rng.Cells(i, 1).Offset(0, 3)
'MySheet is the source sheet, so you need to modify that
cell_source_last = GetLastCellInRow(MySheet.Name, cell_source)
If cell_cellrow = "Cell" Then 'If cell then copy paste value in that cell
wskpInput_target.Range(cell_target) = wskpInput_source.Range(cell_source).Value
ElseIf cell_cellrow = "Row" Then 'If row then copy and paste the row of cells
wskpInput_source.Range(cell_source & ":" & cell_source_last).Copy _
Destination:=wskpInput_target.Range(cell_target)
End If

How to copy an entire row to another sheet if a cell = true

I have 2 sheets, 'Initial' & 'Report1'. I'm trying to copy specific rows from 'Inital' to 'Report1' when the cell in column 'H' is = "On going".
I have the function as a button in excel but cant workout how to copy and paste the line and move onto the next cell.
Also, Column D is formula and needs to be pasted special to copy over.
I have attached the current code I have tried but it errors. Any help would be greatly appreciated.
Sub GenRep1_Click()
Dim Inti As Worksheet
Dim rep1 As Worksheet
Set Inti = ThisWorkbook.Worksheets("Inital")
Set rep1 = ThisWorkbook.Worksheets("Report1")
Dim rngA As Range
Dim cell As Range
Set rngA = Sheets("Inti").Range("H5:H9999")
For Each cell In rngA
If cell.Value = "On going" Then
cell.EntireRow.Copy
Sheets("Inti").Range("").End(xlDown).Select
ActiveSheet.Paste
End If
Next cell
End Sub
I expect the all rows in column 'H' that = "On Going" to be copied to "Report1".
I think this does what you want. You might want to improve the range you're looping through in case you only have, e.g. 100 cells of data.
A quicker approach than looping would be AutoFilter.
Sub GenRep1_Click()
Dim Inti As Worksheet
Dim rep1 As Worksheet
Set Inti = ThisWorkbook.Worksheets("Inital") 'check name - typo?
Set rep1 = ThisWorkbook.Worksheets("Report1")
Dim rngA As Range
Dim cell As Range
Set rngA = Inti.Range("H5:H9999") 'already defined worksheet so just use variable
'Set rngA = Inti.Range("H5",inti.range("H" & rows.count).end(xlup)) 'would be more efficient
For Each cell In rngA
If cell.Value = "On going" Then
cell.EntireRow.Copy
repl.Range("A" & Rows.Count).End(xlUp)(2).PasteSpecial xlValues 'copy to the other sheet
End If
Next cell
End Sub

Using VBA in Excel, how do I loop through a column and create new sheets based on cell values?

I have a column in sheet 1 containing different values. I'd like to loop thru the column and create new sheets with the sheet name corresponding to the values. Each time I create a sheet, I like to set the new sheet active and do some task on that sheet.
Sub test()
i = 4 'starting row in sheet 1
While Not IsEmpty(Cells(i, 1))
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add
ws.Name = Cells(i, 1).Value
'do something to new sheet
i = i + 1
Wend
End Sub
This does not work and the examples I've found online are way too complex for my need. I hope for an easy solution pointing out what I did wrong. thanks
This adds worksheets and names them as per the values in A4 downwards:
Dim wks As Worksheet
Dim wksNew As Worksheet
Dim rngCell As Range
Set wks = Sheets("Sheet1")
Set rngCell = wks.Range("A4")
While Not IsEmpty(rngCell)
Set wksNew = ActiveWorkbook.Worksheets.Add(After:=Sheets(Sheets.Count))
wksNew.Name = rngCell.Value
'do stuff with wksNew
Set rngCell = rngCell.Offset(1)
Wend

Identify the range the values in a worksheet and paste them in a different sheet

I have a Pivot table whose row number could change depending on the data. I want to copy the pivot table and paste it as values starting from column B in another existing sheet.
To do this, i first tried getting the name of the pivot and stored it in a named range using the following code:
Sub ListPivotsInfor()
Dim St As Worksheet
Dim NewSt As Worksheet
Dim pt As PivotTable
Dim I, K As Long
Application.ScreenUpdating = False
Set NewSt = Worksheets.Add
ActiveSheet.Name = "ListOfPivotTables"
I = 1: K = 2
With NewSt
.Cells(I, 1) = "Name"
.Cells(I, 2) = "Sheet"
.Cells(I, 3) = "Location"
For Each St In ActiveWorkbook.Worksheets
For Each pt In St.PivotTables
I = I + 1
.Cells(I, 1).Value = pt.Name
.Cells(I, 2).Value = St.Name
.Cells(I, 3).Value = pt.TableRange1.Address
Next
Next
.Activate
End With
Application.ScreenUpdating = True
Dim cell As Range
Dim rng As Range
Dim RangeName As String
Dim CellName As String
'Single Cell Reference (Workbook Scope)
RangeName = "PivotName"
CellName = "A2"
Set cell = Worksheets("ListOfPivotTables").Range(CellName)
ThisWorkbook.Names.Add Name:=RangeName, RefersTo:=cell
I later tried to copy the pivot table and paste it in another sheet using the following code:
Worksheets.Add
ActiveSheet.Name = "GEP"
Worksheets("GEP Write").PivotTables(PivotName).TableRange2.Copy Destination:=Worksheets("GEP").Range("B1")
The last line is throwing a "Run time error 1004: Unable to get the PivtoTables property of the worksheet class".
Appreciate any help.
Regards

Resources