I have data in Sheet1 in range A2:D17.
I want to copy this data & paste in various multiple sheets (count of sheets are 12).
Sub CopyData()
Dim ws As Worksheet
Dim wsStart As Worksheet
Set wsStart = Worksheets("Sheet1")
For Each ws In ThisWorkbook.Sheets
If Not ws.Name = wsStart.Name Then
wsStart.Range("A2:D17").Copy
ws.Range("A2").PasteSpecial (xlPasteAll)
End If
Next ws
End Sub
Related
I have a small macro that is supposed to copy/paste data from sheet 1 in Book1 to a fresh workbook (Book2). After that, I want it to loop through the rest of the worksheets from Book1 and copy/paste into Book2 but without the headers.
The macro below completes the first step but then continues to copy/pastes the records in sheet 1 every time instead of switching worksheets to copy/paste new data.
Sub CopyData()
' Copy A:D from all sheets to template
Dim ws As Worksheet
Dim sheetIndex As Integer
sheetIndex = 1
'First Sheet pulls in headers and data
Windows("Book1.xlsx").Activate
Sheets(1).Select
Range("A1:D" & Cells(Rows.Count, "C").End(xlUp).Row).Copy
Windows("Book2.xlsm").Activate
ActiveSheet.Paste
Windows("Book1.xlsx").Activate
'Every other worksheet only copies over data
For Each ws In ActiveWorkbook.Worksheets
If ws.Index <> 1 Then
Windows("Book1.xlsx").Activate
Range("A2:D" & Cells(Rows.Count, "C").End(xlUp).Row).Copy
Windows("Book2.xlsm").Activate
Range("A1").End(xlDown).Offset(1).Select
ActiveSheet.Paste
End If
sheetIndex = sheetIndex + 1
Next ws
End Sub
I'm not too experienced so I apologize if the code above isn't optimized. Thanks in advance for your help!
Quick and very dirty solution:
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
'rest of the code
Next ws
It would be much better, if you assign the workbook to a variable and loop through the worksheets, without using Activate and Select.
To achieve something like this, it is important that you know how to initialize workbooks and sheets. Please find time to study how to initialize objects in vba because this will help you in the future.
Sub CopyData()
' Copy A:D from all sheets to template
Dim ws As Worksheet
Dim sheetIndex As Integer
Dim wbBook1 As Workbook, wbBook2 As Workbook
sheetIndex = 1
'First Sheet pulls in headers and data
Set wbBook1 = ThisWorkbook 'The Workbook where we will copy the data; This contains the macro
Windows("Book2.xlsx").Activate 'Because we don't know the book name we will just activate it to initialize
'the second workbook where we will copy our data from Book1
Set wbBook2 = ActiveWorkbook
'Every other worksheet only copies over data
'Now that we initialize our two workbooks we will now copy it in the corresponding sheets
For Each ws In wbBook1.Worksheets
With ws
If ws.Index = 1 Then
.Range("A2:D" & Cells(Rows.Count, "C").End(xlUp).Row).Copy wbBook2.Sheets(1).Range("A1")
Else:
sheetIndex = sheetIndex + 1
wbBook2.Worksheets.Add After:=wbBook2.Sheets(sheetIndex - 1) 'Add additional worksheet on the end to paste our other data
.Range("A2:D" & Cells(Rows.Count, "C").End(xlUp).Row).Copy wbBook2.Sheets(sheetIndex).Range("A1")
End If
End With
Next ws
End Sub
You're almost there but you need to be specific about which sheets and workbooks you're dealing with. Also, you don't need to select them to copy / paste.
Assuming the sheet you're pasting to in Book2.xlsm is Sheet1:
Sub CopyData()
' Copy A:D from all sheets to template
Dim ws As Worksheet, ws2 as worksheet
Dim sheetIndex As Integer
Dim wb1 as workbook, wb2 as workbook
Set wb1 = Workbooks("Book1.xlsx")
set wb2 = Workbooks("Book2.xlsx")
Set ws = wb1.sheets(1)
set ws2 = wb2.sheets(2)
'First Sheet pulls in headers and data
ws.Range("A1:D" & Cells(Rows.Count, "C").End(xlUp).Row).Copy ws2.range("A1")
'Every other worksheet only copies over data
For Each ws In wb1
If ws.Index <> 1 Then
ws.Range("A2:D" & Cells(Rows.Count, "C").End(xlUp).Row).Copy ws2.Range("A1").End(xlDown).Offset(1,0)
End If
Next ws
End Sub
I have sheet1, sheet2 , sheet3, sheet4.
Of the 4 Sheets, sheet 1 and sheet2 has data in list. and sheet3 and sheet 4 has Pivot tables for the same.
I would like to have a VBA, in such a way that, in my workbook, if it find Sheets with list, then it shoudl Format it to table. The table should be only for the cells it has value.
I used record macro, to get the code, but i am struck how i should implement it for all my Sheets.
the code, from record macro for one sheet:
sub macro()
Cells.Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$1:$1048576"), , xlYes).Name = _
"Table2"
Cells.Select
ActiveSheet.ListObjects("Table2").TableStyle = "TableStyleLight9"
End Sub
I think you meant something like the code below:
Option Explicit
Sub macro()
Dim ws As Worksheet
Dim ListObj As ListObject
For Each ws In ThisWorkbook.Worksheets
With ws
For Each ListObj In .ListObjects
ListObj.TableStyle = "TableStyleLight9"
Next ListObj
End With
Next ws
End Sub
If your question is that change range to Listobject, look at follow code.
Sub macro()
Dim Ws As Worksheet
Dim LstObj As ListObject
Dim rngDB As Range, n As Integer
For Each Ws In Worksheets
With Ws
Set rngDB = .Range("a1").CurrentRegion
For Each LstObj In Ws.ListObjects
LstObj.Unlist
Next
If WorksheetFunction.CountA(rngDB) > 0 Then
n = n + 1
Set LstObj = .ListObjects.Add(xlSrcRange, rngDB, , xlYes)
With LstObj
.Name = "Table" & n
.TableStyle = "TableStyleLight9"
End With
End If
End With
Next Ws
End Sub
This question already has answers here:
Run a macro on each sheet except Sheet1 and Sheet2
(1 answer)
Excel iterate through worksheets
(1 answer)
Closed 5 years ago.
I am very new to VBA and have been using the recorder. I have created macros that create a new sheet for each salesman.
I need the format of the combined data wks to copy to all but 2 worksheets in the workbook.
Sub Headings()
'Headings Macro
Sheets("PipelineReport").Select
Range("B2:N2").Select
Selection.Copy
**How can i paste to all sheets except "PipelineReport" & "Raw Data"??
The code would to be like this.
Sub test()
Dim Ws As Worksheet
Dim vDB
vDB = Sheets("PipelineReport").Range("b2").Resize(1, 14)
For Each Ws In Worksheets
If Ws.Name = "PipelineReport" Or Ws.Name = "Raw Data" Then
Else
Ws.Range("b2").Resize(1, 14) = vDB
End If
Next Ws
End Sub
or
Sub test()
Dim Ws As Worksheet
Dim rngDB As Range
Set rngDB = Sheets("PipelineReport").Range("b2").Resize(1, 14)
For Each Ws In Worksheets
If Ws.Name = "PipelineReport" Or Ws.Name = "Raw Data" Then
Else
rngDB.Copy Ws.Range("b2")
End If
Next Ws
End Sub
I would to copy paste the first cell of a sheet trough the last row for all sheets in a workbook and my code is not working, the code is done on the active sheet only.
Sub Macro5()
'
' Macro5
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Range("A2").Copy Destination:=Range("A3:A" & Cells(Rows.Count, "B").End(xlUp).Row)
Next ws
End Sub
Thanks for your help
Looping the worksheets does not make them active. You want something like this.
Dim WS As Excel.Worksheet
Dim iIndex As Integer
For iIndex = 1 To ActiveWorkbook.Worksheets.count
Set WS = Worksheets(iIndex)
With WS
'Do something here.
.Range("A2").Copy Destination:=.Range("A3:A" & .Cells(.Rows.Count, "B").End(xlUp).Row)
End With
Next iIndex
You could use the ws in your loop, but I always set the object.
For Each ws In ActiveWorkbook.Worksheets
ws.Range("A2").Copy Destination:=ws.Range("A3:A" & Cells(Rows.Count, "B").End(xlUp).Row)
Next ws
I would to copy paste the first cell of a sheet trough the last row for all sheets in a workbook and my code is not working, the code is done on the active sheet only.
Sub Macro5()
'
' Macro5
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Range("A2").Copy Destination:=Range("A3:A" & Cells(Rows.Count, "B").End(xlUp).Row)
Next ws
End Sub
Thanks for your help
Looping the worksheets does not make them active. You want something like this.
Dim WS As Excel.Worksheet
Dim iIndex As Integer
For iIndex = 1 To ActiveWorkbook.Worksheets.count
Set WS = Worksheets(iIndex)
With WS
'Do something here.
.Range("A2").Copy Destination:=.Range("A3:A" & .Cells(.Rows.Count, "B").End(xlUp).Row)
End With
Next iIndex
You could use the ws in your loop, but I always set the object.
For Each ws In ActiveWorkbook.Worksheets
ws.Range("A2").Copy Destination:=ws.Range("A3:A" & Cells(Rows.Count, "B").End(xlUp).Row)
Next ws