1
votes

I have a plethora of Excel workbooks containing 25+ worksheets each containing 20 columns of data from range 1:500 (or 1:1000 in some cases). Frequently I am tasked with updating the "template" onto which new data is entered for new calculations. I want to be able to easily paste extant data from old worksheets into sheets with new formatting while retaining any new formatting/formulas in the new templates.

I am using VBA to open the sheet I want to copy and paste it onto the new template sheet. So far my code will copy everything from the first sheet (S1) of the to-be-copied workbook and paste it onto the first sheet (S1) of the target workbook.

I want to extend this process to go through all active sheets (do whatever it is doing now for each sheet in the workbooks). I previously was able to do this with different code but it removed the formulas in rows 503 and 506 that I need when it pasted in. Can I do a pastespecial and skip empty cells? I am new to this.

Here is my current code:

Sub CopyWS1()
Dim x As Workbook
Dim y As Workbook

Set x = Workbooks("Ch00 Avoid.xlsx")
Set y = Workbooks("Ch00 Avoid1.xlsx")
Dim LastRow As Long
Dim NextRow As Long

x.Worksheets("S1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Select
LastRow = ActiveCell.Row

Range("A2:T" & LastRow).Copy y.Worksheets("s1").Range("A1:A500")

Application.CutCopyMode = False

Range("A1").Select
End Sub

I believe that I need to use something like the following code in order to extend this across the worksheets, but I'm not sure how to iterate through the sheets since I'm specifically referencing two sheets in my above code.

     Sub WorksheetLoop2()

     ' Declare Current as a worksheet object variable.
     Dim Current As Worksheet

     ' Loop through all of the worksheets in the active workbook.
     For Each Current In Worksheets

        ' Insert your code here.
        ' This line displays the worksheet name in a message box.
        MsgBox Current.Name
     Next

     End Sub

I imagine that I might be able to solve this as a for loop across an index of worksheets (make a new variable and run a for loop until my index is 25 or something) as an alternative, but again, I'm not sure how to point my copy/paste from a particular sheet to another sheet. I am very new to this with semi-limited experience with Python/Java only. These VBA skills would greatly benefit me on the day to day.

The two files in question: Ch00 Avoid

Ch00 Avoid1

2
"I'm not sure how to point my copy/paste from a particular sheet to another sheet" --- Why don't you try Sheets(i).Range("A2:T" & LastRow).Copy Sheets(j).Range("A1") where i and j are the indices of the sheets you wish to use.Cody G
Also, it may help to avoid using .Select, at the very least it'll help you understand a little better how to work with the data. You could also look up something like "VBA loop through worksheets"BruceWayne
I am completely lost. Every time I modify my code away from what is above I am losing the functionality of what I have. I should probably specify that any functionality I have gotten so far is due to dumb luck and hodge-podgeing the code of others' together. If I add Sheets(i).Range("A2:T" & LastRow).Copy Sheets(j).Range("A1") and specify the ranges I want (index 1 through index 25), nothing happens. I want to activate each sheet of my first workbook in succession, copying data from rows 1-500 and columns A-T and copy that data into its corresponding worksheet in the new workbook.RigStackhorse

2 Answers

0
votes

This should do it. You should be able to drop this in a blank workbook just to see how it works (put some values in column A on a couple of sheets). Obviously you will replace your wbCopy and wbPaste variables, and remove the wbPaste.worksheets.add from the code (my excel was only adding 1 sheet in the new workbook). LastRow is determined per your code, looking up from column A to find the last cell. wsNameCode is used to determine the first part of your worksheets you are looking for, so you will change it to "s".

This will loop through all sheets in your copy workbook. For each of those sheets, it's going to loop 1 through 20 to see if the name equals "s" + loop number. Your wbPaste has the same sheet names, so when it finds s# on wbCopy, it is going to paste into wbPaste with the same sheet name: s1 into s1, s20 into s20, etc. I didn't put in any error handling, so if you have an s21 on your copy workbook, s21 needs to be on your paste workbook, and NumberToCopy changed to 21 (or just set it to a higher number if you plan on adding more).

You could have it just loop through the first 20 sheets, but if someone moves one it will throw it all off. This way sheet placement in the workbook is irrelevant as long as it exists in the paste workbook.

You can also turn screenupdating off if you don't want to have a seizure.

Option Explicit

Sub CopyAll()

'Define variables
Dim wbCopy As Workbook
Dim wsCopy As Worksheet
Dim wbPaste As Workbook
Dim LastRow As Long
Dim i As Integer
Dim wsNameCode As String
Dim NumberToCopy As Integer

'Set variables
i = 1
NumberToCopy = 20
wsNameCode = "Sheet"

'Set these to your workbooks
Set wbCopy = ThisWorkbook
Set wbPaste = Workbooks.Add
'These are just an example, delete when you run in your workbooks
wbPaste.Worksheets.Add
wbPaste.Worksheets.Add

'Loop through all worksheets in copy workbook
For Each wsCopy In wbCopy.Worksheets
    'Reset the last row to the worksheet, reset the sheet number search to 1
    LastRow = wsCopy.Cells(65536, 1).End(xlUp).Row
    i = 1
    'Test worksheet name to match template code (s + number)
    Do Until i > NumberToCopy
        If wsCopy.Name = (wsNameCode & i) Then
            wsCopy.Range("A2:T" & LastRow).Copy
            wbPaste.Sheets(wsNameCode & i).Paste
        End If
    i = i + 1
    Loop
Next wsCopy

End Sub
0
votes

Thank you for all of your help, everyone. I went back yesterday afternoon from scratch and ended up with the following code which, at least to my eyes, has solved what I was trying to do. The next step will be to try to make this less tedious as I have a gajillion workbooks to update. If I can find a less obnoxious way to open/update/save/close new workbooks, I will be very happy. As it stands now, however, I have to open both the example workbook and the target workbook, save both, and close...but it works.

'This VBA macro copies a range of cells from specified worksheets within one workbook to a range of cells
'on another workbook; the names of the sheets in both workbooks should be identical although can be edited to fit

Sub CopyToNewTemplate()

Dim x As Workbook
Dim y As Workbook
Dim ws As Worksheet
Dim tbc As Range
Dim targ As Range
Dim InxW As Long
Dim WshtNames As Variant
Dim WshtNameCrnt As Variant

'Specify the Workbook to copy from (x) and the workbook to copy to (y)
Set x = Workbooks("Ch00 Avoid.xlsx")
Set y = Workbooks("Ch00 Avoid1.xlsx")

'Can change the worksheet names according to what is in your workbook; both worksheets must be identical
WshtNames = Array("S1", "S2", "S3", "S4", "S5", "S6", "S7", "s8", "s9", "S10", "S11", "S12", "S13", "S14", "S15", _
                "S16", "S17", "S18", "S19", "S20", "Ext1", "Ext2", "Ext3", "EFS BigAverage")

'will iterate through each worksheet in the array, copying the tbc range and pasting to the targ range
For Each WshtNameCrnt In WshtNames
    With Worksheets(WshtNameCrnt)
        'tbc is tobecopied, specify the range of cells to copy; targ is the target workbook range
        Set tbc = x.Worksheets(WshtNameCrnt).Range("A1:T500")
        Set targ = y.Worksheets(WshtNameCrnt).Range("A1:T500")

        Dim LastRow As Long
        Dim NextRow As Long

        tbc.Copy targ
        Application.CutCopyMode = False
        
    End With
Next WshtNameCrnt


End Sub