I'm having trouble copying rows from multiple sheets to a new worksheet. The code I now have is as following:
Sub Samenvoegen()
Dim J As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add ' add a sheet in first place
Sheets(1).Name = "Index"
' work through sheets
For J = 2 To Sheets.Count ' from sheet 2 to last sheet
Sheets(J).Activate ' make the sheet active
Range("2:2").Select
Range(Selection, Cells(Rows.Count, "2:2").End(xlUp)).Copy Range("2:2") ' select all cells in this sheets
' select all lines except title
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
' copy cells selected in the new sheet on last line
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
End Sub
For example: I have 10 worksheets in my excel file and if I use this macro, it merges row 2 from all those excel worksheets into 1 worksheet named "Index" and pastes per worksheet under each other. Thats what I want. But the problem with this macro is that it copies the second row from the first worksheet correct. But after that first worksheet it copies from B2 from the second worksheet and C2 from the third worksheet and so on..
I would like to have a macro which copies all of the second row from the worksheets and pastes it into the new worksheet under each other. What am I doing wrong here?
Sub Samenvoegen()
Dim ws As Worksheet, wb As Workbook, i As Long
Set wb = ThisWorkbook
wb.Sheets.Add ' add a sheet in first place
wb.Sheets(1).Name = "Index"
' work through sheets
i = 1
For Each ws In wb.Sheets ' from sheet 2 to last sheet
If ws.Name <> "Index" Then
' copy row(2) into the new sheet
ws.Rows(2).Copy
wb.Sheets("Index").Range("A" & i).PasteSpecial xlPasteValues
i = i + 1
End If
Next
End Sub
Related
I have workbook (WB1) with a Sheet (Sheet1) with data (A2:W2).
I want to create a VBA where i can copy a specific row from WB1 and paste it into antoher workbook (WB2 / Sheet2) after the last row.
Can anyone help on that?
Unfortunately I just need a code on that and my tries were not going anywhere
If both workbook are open you can do it like this.
Sub CopyRow()
On Error GoTo Err
With Workbooks("Book1").Worksheets("Sheet1") ' like From ThisWorkbook
myrow = 4 ' Row to be copied
colCount = .UsedRange.Columns.Count ' Colums to be copied like From ThisWorkbook
DesRow = "A" & Trim(CStr(Workbooks("Book2").Worksheets("Sheet1-(inBok2)").UsedRange.Rows.Count + 1))
.Range(.Cells(myrow, 1), .Cells(myrow, colCount)).Copy Destination:=Workbooks("Book2").Worksheets("Sheet1-(inBok2)").Range(DesRow)
End With
Exit Sub
Err:
MsgBox ("# Error - Can`t perform the copy. Check sheet names and that bouth workbooks are open.")
End Sub
I want to simply copy the first row "test" into all following sheets (Sheetxx1, Sheetx23, Sheet231, etc. ) ... (like 100 following sheets with different names).
So I tried this by recording a macro (with relative reference) and then went on the sheet, where I want to have it pasted (like Sheetx231) and then did run the macro. But what it did is it pasted again row "test" into Sheetxx23.
How can I make the macro paste the row test of Sheetxx1 into the recent sheet (I am in and run the macro)?
Sub Macro1()
Rows("1:1").Select
Selection.Copy
Sheets("Sheetxx23").Select
ActiveCell.Rows("1:1").EntireRow.Select
ActiveSheet.Paste
End Sub
You need to loop throuh all worksheets and copy/paste for each worksheet.
Option Explicit
Public Sub CopyFirstRowIntoAllWorksheets()
Dim SourceWs As Worksheet
Set SourceWs = ActiveSheet
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets 'loop throuh all sheets
If Not ws.Name = SourceWs.Name Then 'makes sure source and destination is not the same sheet
SourceWs.Rows(1).Copy Destination:=ws.Rows(1) 'copy first row
End If
Next ws
Application.CutCopyMode = False
End Sub
You might benefit from reading
How to avoid using Select in Excel VBA.
Edit according comment:
If you need only the worksheets right of the active worksheet to be pasted, replace
If Not ws.Name = SourceWs.Name Then
with
If ws.Index > SourceWs.Index Then
I copy a worksheet "Costing Sheet" to a new worksheet.
I use this code:
Sub CopyPasteSheetAsValues()
Worksheets("Costing Sheet").Copy After:=Worksheets("Costing Sheet")
Application.CutCopyMode = False
End Sub
There are formulas in the copied sheet that I do not need in the new worksheet. I would like to copy the worksheet into a new sheet but paste as 'values only'.
I also have the code below that can copy and paste the worksheet but it overrides the current sheet. Instead of a new worksheet with values only, it's the same worksheet as values.
Sub CopyPasteSheet()
'Copy and Paste Summary Sheet as Values
Sheets("Costing Sheet").Cells.Copy Before:=Sheets("Comparison Job")
Sheets("Costing Sheet").Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
I tried to combine the codes but was unsuccessful.
You can exploit the fact that a sheet becomes the active one when it is copied from another sheet
Sub CopyPasteSheetAsValues()
Worksheets("Costing Sheet").Copy After:=Worksheets("Costing Sheet")
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
End Sub
Doing a copy with no destination will create a new workbook with the copied worksheet. Assigning the value of the range to the value of the range will get rid of the formulas.
Dim myNewWB as Workbook
Worksheets("Costing Sheet").Copy
Set myNewWB = ActiveWorkbook
With myNewWB.Worksheets(1)
.Range(.Cells(1,1),.Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Count)).Value= _
.Range(.Cells(1,1),.Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Count)).Value
End With
I'm starting to learn my journey in VBA Excel
I have a project to do: to loop through the different sheets in the worksheets
and copy the value in a specific range in sheets and copy those value to one sheets. How i can achieve this?
Sub LoopAndCopy
Dim ws as Worksheet
dim target as worksheet
set target = worksheets("target sheet name goes here") 'sheet you're copying to
For each ws in ThisWorkbook.worksheets 'loop through worksheets
if ws.name <> target.name then 'if not the target sheet then...
'copy your range into the next blank row in column A
ws.range("your range here").copy target.range("A" & rows.count).end(xlup).offset(1,0)
end if
next ws
Unfortunately for my employer, none of my network engineering courses included advanced Excel formula programming. Needless to say, I know nothing about Excel save for basic SUM and COUNT formula commands.
My employer has an Excel workbook with multiple worksheets within it representing each month of the calendar year. We want to be able to have a "total" worksheet in the workbook that reflects all data across the entire workbook in each column/row.
An example for the sake of clarity:
In the worksheet "May_2013", column A is labeled "DATE". Cell A2 contains the data "MAY-1".
In the worksheet "June_2013", column A is labeled "DATE". Cell A2 contains the data "JUNE-1".
In the worksheet "Total", column A is labeled "DATE". We want cells A2 to reflect "MAY-1" and A3 to reflect "JUNE-1".
We want to do this for all worksheets, columns A-Q, rows 2-33 and populate a master sheet at the very end containing all data in all worksheets in their corresponding columns.
Is this possible?
Here are two VBA solutions. The first does this:
Check if a sheet "totals" exists. Create it if it does not
Copy the first row (A to Q) of first sheet to "totals"
Copy block A2:Q33 to "totals" sheet starting at row 2
Repeat for all other sheets, appending 32 rows lower each time
The second shows how to do some manipulation of the column data before copying: for each column it applies the WorksheetFunction.Sum(), but you could replace that with any other aggregating function that you would like to use. It then copies the result (one row per sheet) to the "totals" sheet.
Both solutions are in the workbook you can download from this site. Run the macros with , and pick the appropriate one from the list of options that shows up. You can edit the code by invoking the VBA editor with .
Sub aggregateRaw()
Dim thisSheet, newSheet As Worksheet
Dim sheetCount As Integer
Dim targetRange As Range
sheetCount = ActiveWorkbook.Sheets.Count
' add a new sheet at the end:
If Not worksheetExists("totals") Then
Set newSheet = ActiveWorkbook.Sheets.Add(after:=Sheets(sheetCount))
newSheet.Name = "totals"
Else
Set newSheet = ActiveWorkbook.Sheets("totals")
End If
Set targetRange = newSheet.[A1]
' if you want to clear the sheet before copying data, uncomment this line:
' newSheet.UsedRange.Delete
' assuming you want to copy the headers, and that they are the same
' on all sheets, you can copy them to the "totals" sheet like this:
ActiveWorkbook.Sheets(1).Range("1:1").Copy targetRange
Set targetRange = targetRange.Offset(1, 0) ' down a row
' copy blocks of data from A2 to Q33 into the "totals" sheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> newSheet.Name Then
ws.Range("A2", "Q33").Copy targetRange
Set targetRange = targetRange.Offset(32, 0) ' down 32 rows
End If
Next ws
End Sub
Sub aggregateTotal()
Dim thisSheet, newSheet As Worksheet
Dim sheetCount As Integer
Dim targetRange As Range
Dim columnToSum As Range
sheetCount = ActiveWorkbook.Sheets.Count
' add a new sheet at the end:
If Not worksheetExists("totals") Then
Set newSheet = ActiveWorkbook.Sheets.Add(after:=Sheets(sheetCount))
newSheet.Name = "totals"
Else
Set newSheet = Sheets("totals")
End If
' assuming you want to copy the headers, and that they are the same
' on all sheets, you can copy them to the "totals" sheet like this:
Set targetRange = newSheet.[A1]
ActiveWorkbook.Sheets(1).Range("A1:Q1").Copy targetRange
Set targetRange = targetRange.Offset(1, 0) ' down a row
For Each ws In ActiveWorkbook.Worksheets
' don't copy data from "total" sheet to "total" sheet...
If ws.Name <> newSheet.Name Then
' copy the month label
ws.[A2].Copy targetRange
' get the sum of the coluns:
Set columnToSum = ws.[B2:B33]
For colNum = 2 To 17 ' B to Q
targetRange.Offset(0, colNum - 1).Value = WorksheetFunction.Sum(columnToSum.Offset(0, colNum - 2))
Next colNum
Set targetRange = targetRange.Offset(1, 0) ' next row in output
End If
Next ws
End Sub
Function worksheetExists(wsName)
' adapted from http://www.mrexcel.com/forum/excel-questions/3228-visual-basic-applications-check-if-worksheet-exists.html
worksheetExists = False
On Error Resume Next
worksheetExists = (Sheets(wsName).Name <> "")
On Error GoTo 0
End Function
Final(?) edit:
If you want this script to run automatically every time someone makes a change to the workbook, you can capture the SheetChange event by adding code to the workbook. You do this as follows:
open the Visual Basic editor ()
In the project explorer (left hand side of the screen), expand the VBAProject
Right-click on "ThisWorkbook", and select "View Code"
In the window that opens, copy/paste the following lines of code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
' handle errors gracefully:
On Error GoTo errorHandler
' turn off screen updating - no annoying "flashing"
Application.ScreenUpdating = False
' don't respond to events while we are updating:
Application.EnableEvents = False
' run the same sub as before:
aggregateRaw
' turn screen updating on again:
Application.ScreenUpdating = True
' turn event handling on again:
Application.EnableEvents = True
Exit Sub ' if we encountered no errors, we are now done.
errorHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
' you could add other code here... for example by uncommenting the next two lines
' MsgBox "Something is wrong ... " & Err.Description
' Err.Clear
End Sub
Kindly use RDBMerge add-in which will combine the data from different worksheet and create a master sheet for you. Please see the below link for more details.
http://duggisjobstechnicalstuff.blogspot.in/2013/03/how-to-merge-all-excel-worksheets-with.html
Download RDBMerge
You can use the indirect function to reference the sheet name. In the image below this function takes the header name (B37) and uses it as the sheet reference. All you have to do is choose the correct "total cell" which I made "A1" in "MAY_2013". I put an image below to show you my reference name as well as tab name