I've got a spreadsheet with around 90 identically formatted workbooks. I need to copy and paste around 336 independant formulas located in one row from a completed worksheet to all the other worksheets. Unfortunatley, this ends up being around 30k indvidual cells that need to be copied and pasted at once. Is there a workaround other than splitting it up and doing it manually?
Any help is apperciated. Thanks.
You can try using VBA. Something like the procedure below should get you started. The procedure below takes the information from. This procedure assumes that your formulas begin in Row A1 and end in C1, change as you need. Inside the array, list the name of the sheets you want.
Sub copyFormulas()
Dim rng As Range
Dim WS As Worksheet
With Sheets("The Sheet Name with the Formulas To Copy")
Set rng = .Range(.Range("A1:C1"), Range("A" & Rows.Count).End(xlUp))
End With
For Each WS In Sheets(Array("Your Destination Sheet Name Here", "And Here")) 'Add more sheets if you need to
WS.Rows(2).Resize(rng.Count).Copy
rng.Copy Destination:=WS.Range("A1")
Next WS
End Sub
Related
I am new to using VBA. I have an Excel document that has 500 sheets worth of data each sheet only has at most 45 rows and up to column W. I am trying to have a macro that copies the data from each of the 500 sheets and pastes it into a sheet named "Master". I am able to use the following code to perform this successfully.
Sub CopyPaste()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
If Not wks.Name = "Master" Then
wks.Range("A1:W" & wks.Cells(Rows.Count, "A").End(xlUp).Row).Copy _
Destination:=Worksheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1)
End If
Next
End Sub
The issue I am running into is that some of the sheets have a row that is blank below the header before the data begins and as my code is written currently it seems like the macro thinks this is the end of the page and moves to the next sheet and is missing data that should be copied and pasted. I am looking for some assistance/guidance on how to account for these blank cells/rows so that the copy and pasting continue for the sheet through the end. Any help would be much appreciated, thank you.
I want to copy cells A103 to Y148 from first sheet to every sheet in my excel workbook. What code I should use?
Here is a four-step approach.
1. Design the action in plain words, like,
' loop through rows 103 to 148 in Sheet1
' copy the row
' loop through all other sheets in the workbook
' paste the row in the first empty row at the bottom of each
Research code for each action. For example, google for "VBA Excel loop through rows"
Be amazed at the multitude of ready-to-use code you will find, and at how quickly you will learn to do impossible things.
Come back here with any problems you can't solve on your own.
Depending on what you mean by 'every sheet in my excel workbook', this may be faster than looping a paste operation through all of the remaining worksheets.
Sub galumph()
Dim w As Long, wss As String, wsa as Variant
For w = 2 To Worksheets.Count
wss = wss & IIf(CBool(Len(wss)), ";", vbNullString) & Worksheets(w).Name
Next w
wsa = Split(wss, ";") 'make array of worksheet names
Worksheets(1).Range("A103:Y148").Copy 'copy from A103:Y148 on Sheet1
Worksheets(wsa).Select 'select all of the remaining worksheets
Worksheets(wsa(0)).Range("A1").Activate 'activate the destination on one of the worksheets
Worksheets(wsa(0)).Paste 'paste the data into all worksheets
End Sub
I have a workbook with over 100 separate sheets and
Each sheet is the same form with cells having different data. I need to be able to go through each sheet and copy the cells in the form with data and input it in a
Select sheet of rolls. Each roll would represent the data
From one sheet.
Basicly. Say for example. The form on each of the 100
Worksheets was an address book with the typical data for a simple address book. And each worksheet was exactly the same. How would be able to cycle through each of the 100 sheets and copy the information in the
Cells into one sheet with each row have the data from
The worksheets sepeatery. This way I could transfer the
Info into a database.
Thanks for you time
You can use a macro to do this:
Public Sub CopyToOneSheet()
Dim DestSht As Worksheet
Dim sht As Worksheet
Set DestSht = ThisWorkbook.Worksheets("DestinationSheetName") 'Change to your sheet name where everything will be copied.
'Add a header to your distination sheet
DestSht.Cells(1, 1).Value = "Everything will be copied here."
'Loop through each sheet.
For Each sht In ThisWorkbook.Worksheets
'Not going to copy the destination sheet.
If sht.Name <> DestSht.Name Then
'Copy used range to first available row on destination sheet.
sht.Range("A2:Z" & sht.Cells(Rows.Count, 1).End(xlUp).Row).Copy Destination:=DestSht.Range("A" & DestSht.Cells(Rows.Count, 1).End(xlUp).Row + 1)
End If
Next sht
Set DestSht = Nothing
Set sht = Nothing
End Sub
I have a spreadsheet I'm using to compile text that changes all the time.
In column AD, Row 4(AD4) I put the contents of text, and it can have data going 1000 to 4000 rows down. It changes every time, so there is no static range name. I need a macro that
finds the final piece of data in that column,
then automatically "drags a box" from that spot two columns to the left (AB4)
and copies it... (A 3000 row piece of text would be AB4:AD3004) (Macro stops there, with text to be copied highlighted)
The current version finds the bottom cell correctly, but if I run the macro a 2nd time, with new data, it keeps trying to copy the same range. (I used the Formula Define.Name method, to name the cell, and then selected AB4:LastRow) but it is ALWAYS 3160 whether data goes to row 4000 or not.....
Sub Last_row()
Cells(Application.Rows.Count, 30).End(xlUp).Select
' following lines of code are useless
Range("AB4:AD3160").Select
Range("AD3160").Activate
Selection.Copy
End Sub
To answer your question directly:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy
End With
Copy to specific location WITHOUT using clipboard:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy Sheet2.[A1]
End With
Copy and exclude formatting:
With Sheet1
With .Range("AB4", .Cells(Rows.Count, "AD").End(xlUp))
Sheet2.Cells(1, "A").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End With
Note: Replace all sheet codenames (sheet1, Sheet2) above with your actual sheet codenames.
Your current code hard-codes the range of interest with
Range("AB4:AD3160").Select
This code will define a dynamic range starting from AB4 to the last non-empty cell in column AD
You can then use this range (without selecting) for changing values elsewhere (note that you may not need to actually copy rng1, it is possible to dump these values to a separate range directly without a copy and paste.
Sub Last_row()
Dim rng1 As Range
Set rng1 = Range([ab4], Cells(Rows.Count, 30).End(xlUp))
rng1.Copy
End Sub
Update: Example of how to copy a dynamic sized range from one sheet to another without a copy and paste:
Sub Last_row2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Set ws1 = Sheets(1)
Set ws2 = Sheets(2)
Set rng1 = ws1.Range(ws1.[ab4], ws1.Cells(Rows.Count, 30).End(xlUp))
ws2.[a1].Resize(rng1.Rows.Count, rng1.Columns.Count).Value = rng1.Value
End Sub
Currently I have spreadsheets coming in that are formatted incorrectly. Our client sent out to his suppliers an old spreadsheet where columns are laid out differently than what they are currently setup as. Normally we would tell them to correct it, but some of these spreadsheets have over 220k rows and 33 columns. They're updating it for the future, but asking them to have their clients redo their tables is a no-go. I've written a script that will copy a column, and place it into the corresponding static column in another workbook. This works okay but I feel there is more that could be done.
Name of open workbook copying from varies.
Name of workbook copied to: C:\User\(Name)\UCOR\Catalogs\PSX-Toolset v1.503-EN.xls
What I would like is help writing a macro that will do the following from open workbook:
1.) Select an entire column minus Row 1 to the first blank row. - This goes from B to AH
2.) Paste that column into PSX-Toolset workbook, worksheet name "Item Data" - Static Assigned Columns
3.) Perform a Save As on PSX-Toolset as (Catalog-PSX-<Workbook Copied From>.xls)
Lastly, I'd like to know if it's possible to do the above, but mapping heading cells. Unfortunately the cell names are not identical.
Untested:
Sub MapAndCopyColumns()
Dim i As Integer, rng As Range
Dim shtSrc As Worksheet, wbDest As Workbook
Dim shtDest As Worksheet
Dim iNew
Set shtSrc = ActiveSheet
Set wbDest = Workbooks.Open("C:\User\(Name)\UCOR\Catalogs\PSX-Toolset v1.503-EN.xls")
Set shtDest = wbDest.Sheets("Item Data")
For i = 2 To 34
Set rng = shtSrc.Cells(2, i)
If rng.Value <> "" Then
If rng.Offset(1, 0).Value <> "" Then
Set rng = Range(rng, rng.End(xlDown))
End If
'map old position >> new position
' mapping table has 2 columns of numbers: "old" and "new"
iNew = Application.VLookup(i, _
ThisWorkbook.Sheets("Mapping").Range("A2:B40"), 2, False)
If Not IsError(iNew) Then
'copy if the column has an entry in the mapping table
rng.Copy shtDest.Cells(2, iNew)
End If
End If
Next i
wbDest.SaveAs "C:\wheretosaveto\Catalog-PSX-" & shtSrc.Parent.Name
End Sub
How I learned most of my vba is through 'record macro'. You start recording, do what you want to do yourself, stop recording and then look at the generated code.
Usually you can improve the code by eliminating a lot of redundant lines, but it should at least expose all the commands you need to complete your goal.