Hi I have code which is meant to
Loop through all worksheets which begin with "673"
Copy all the rows which have data from row 5 onwards
Paste the entries on the next empty row in the "Colours" worksheet
I'm having the following issues:
Code only runs in the worksheet that is active
Doesn't loop through all worksheets
When it pastes in the "Colours" worksheet, it pastes directly over the headings in row 2. The first blank row is row 3 onwards and I would like the logic to paste at the next available blank row as it loops through the sheets.
Sub Consolidate()
Dim lastrow As Long
Dim report As Worksheet
Set report = Excel.ActiveSheet
For Each Sheet In ActiveWorkbook.Worksheets
If InStr(Sheet.Name, "673") > 0 Then
With report
.Range(.Cells(5, "K"), .Cells(.Rows.Count, "K").End
(xlUp)).EntireRow.Copy
End With
Worksheets("Colours").Select
lastrow = Worksheets("Colours").Cells(Worksheets("Colours").Rows.Count, 1).End(xlUp).Row
Worksheets("Colours").Cells(lastrow + 1, 1).Select
ActiveSheet.Paste
End If
Next
End Sub
Your help would be greatly appreciated.
KS is right, to get your code functioning you just need to reference the sheet. I'd started modifying it further so I'll post what I've done in totality:
Firstly I removed the 'Set report = ' line, that's not needed (alternatively you could have 'Set report' at the beginning of the loop, but it's easier to work directly 'With Sheet' as KS says).
CHANGED1 = You said it should loop through worksheets that 'begin' with 673, so this new line checks for the first three characters matching 673, rather than just looking to see if 673 appears anywhere in the sheet name.
NEW = Activates the sheet, this makes the next copy command work.
CHANGED2 = With Sheet as explained above.
CHANGED3 = You said it should copy the rows that have data from row 5 onwards (previously your code would copy rows 1-5).
Sub Consolidate()
Dim lastrow As Long
Dim report As Worksheet
For Each Sheet In ActiveWorkbook.Worksheets
If Left(Sheet.Name, 3) = "673" Then 'CHANGED1
Worksheets(Sheet.Name).Select 'NEW
With Sheet 'CHANGED2
.Range("A5", Range("A" & 65536).End(xlUp)).EntireRow.Copy 'CHANGED3
End With
Worksheets("Colours").Select
lastrow = Worksheets("Colours").Cells(Worksheets("Colours").Rows.Count, 1).End(xlUp).Row
Worksheets("Colours").Cells(lastrow + 1, 1).Select
ActiveSheet.Paste
End If
Next
End Sub
Hope this helps!
try the following code
Sub Consolidate()
Dim sheet As Worksheet, coloursSheet As Worksheet
Set coloursSheet = ActiveWorkbook.Worksheets("Colours")
For Each sheet In ActiveWorkbook.Worksheets
If Left(sheet.Name, 3) = "673" Then
sheet.Range("K5:K" & sheet.Cells(sheet.Rows.Count, "K").End(xlUp).Row).SpecialCells(xlCellTypeConstants).EntireRow.Copy _
Destination:=coloursSheet.Cells(coloursSheet.Rows.Count, "A").End(xlUp).Offset(1)
End If
Next
End Sub
it:
avoids useless selections and variables
copies non blank cells only (assuming data are "constants", i.e. not formulas)
Related
Dears,
I would like to add to the below code, the lines, which do the following:
Copy the range from Results tab and pastes it to every newly created sheet. It should be copied to the same column which is populated by the below macro.
I think we would need to add this code somewhere:
Worksheets("Results").Range("A1:A65").Copy Destination:=ActiveSheet.Range("a50:a150") ???
It should also check this column for duplicates.
Will you ba able to help?
the initial code is the following:
Sub YouShouldHavePostedAnAttemptFirst()
Dim c As Range
Dim CtRows, SheetCtr As Integer
'Try to put your data on sheet 1 then create a new sheet so that it is the
'second sheet in the workbook.
SheetCtr = 4
CtRows = Application.CountA(Sheets("2nd step").Range("r:r"))
For Each c In Range(Cells(1, 18), Cells(CtRows, 18))
c.Offset(, -10).Copy Sheets(SheetCtr).Cells(Rows.Count, "a:a").End(xlUp).Offset(1, 0)
If c.Offset(1, 0) <> c Then
Sheets.Add after:=Sheets(ActiveWorkbook.Sheets.Count)
SheetCtr = SheetCtr + 1
End If
Next c
End Sub
Thank you,
This code will copy the data from Results into your existing sheets and then create four new sheets and paste the data in there as well:
Sub PopulateSheets()
Dim wrkSht As Worksheet
Dim SheetCtr As Long, x As Long
'First go through each sheet in the workbook.
'If you want other sheets apart from 'Results' to be ignored just add them to the Case.
'e.g. Case "Results", "Sheet1" will ignore Results & Sheet1.
For Each wrkSht In ThisWorkbook.Worksheets
Select Case wrkSht.Name
Case "Results"
'Do nothing - we're copying from this sheet.
Case Else
'Copy from Results to the other worksheet.
With ThisWorkbook.Worksheets("Results")
.Range("A1:A65").Copy Destination:=wrkSht.Range("A50")
End With
End Select
Next wrkSht
'Creates 4 sheets, copies the data over and moves the sheet to the end.
SheetCtr = 4
With ThisWorkbook
For x = 1 To SheetCtr
Set wrkSht = ThisWorkbook.Worksheets.Add
.Worksheets("Results").Range("A1:A65").Copy Destination:=wrkSht.Range("A50")
wrkSht.Move After:=Sheets(.Sheets.Count)
Next x
End With
End Sub
If you just want to copy the data when a new sheet is added -
In a normal module add the below code. The procedure takes a reference to a worksheet and copies the data from the Results sheet to it and removes any duplicates.
Public Sub CopyToNewSheet(sht As Worksheet)
With sht
ThisWorkbook.Worksheets("Results").Range("A1:A65").Copy Destination:=.Range("A50")
.Range("A50:A114").RemoveDuplicates Columns:=1, Header:=xlNo
End With
End Sub
In the ThisWorkbook module add the below code. This checks that you're adding a worksheet rather than a chart sheet or any other type and passes the sheet reference to the CopyToNewSheet procedure:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If Sh.Type = xlWorksheet Then
CopyToNewSheet Sh
End If
End Sub
I am quite new to Excel Macro and need some help to achieve the following task.
I have two sheets in the same workbook; one is the main sheet that can be edited, and the other one is to extract certain columns from the main sheet. Since the main sheet may have columns inserted or deleted, my approach is to input specific titles that I would extract to sheet 2 (blank sheet initially), look up/match these texts/column titles in sheet 1 (main sheet); then, copy the entire column under that match column title and paste it to sheet 2.
I have the following code, but errors still pop up. Since I am not quite familiar with Macro syntax, I am not so sure if this approach is applicable. I do appreciate any help, comment, or suggestion. Thanks in advance.
Private Sub CommandButton1_Click()
Dim i As Long
Dim j As Long
Dim r As Long
For i = 1 To 30
For j = 1 to 30
If Sheets(2).Cells(1, j).Value = Sheets(7).Cells(1, i).Value Then
For r = 2 To 1000
Sheets(2).Cells(r, j).Copy
Sheets(7).Cells(r, i).PasteSpecial Paste:=xlPasteFormats
Sheets(7).Cells(r, i).PasteSpecial Paste:=xlPasteValue
Next r
End If
Next j
Next i
End Sub
you'd better use Find() method of Range object to find values in a range
so you may want to use this code
Option Explicit
Private Sub CommandButton1_Click()
Dim f As Range, mainShtHeaderRng As Range, blankShtHeaderRng As Range, cell As Range
Dim mainSht As Worksheet, blankSht As Worksheet
Set mainSht = Worksheets("mainSht") '<--| set your "main" sheet
Set blankSht = Worksheets("blankSht") '<--| set your "blank" sheet
Set mainShtHeaderRng = mainSht.Rows(1).SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues) '<--| set your header range in the "main" sheet
Set blankShtHeaderRng = blankSht.Rows(1).SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues) '<--| set your header range in the "blank" sheet
For Each cell In blankShtHeaderRng '<--| loop through "blank" sheet headers...
Set f = mainShtHeaderRng.Find(what:=cell.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False) '<--|... and search them between the "main" sheet headers
If Not f Is Nothing Then '<--| if found...
Range(f, mainSht.Cells(mainSht.Rows.Count, f.Column).End(xlUp)).Copy '<--| copy "main" sheet corresponding column doqwn to its last non empty cell...
cell.PasteSpecial Paste:=xlPasteFormats '<--| ... and paste formats...
cell.PasteSpecial Paste:=xlPasteValues '<--| ... and values to "blank" sheet current header column
End If
Next cell
End Sub
I am new using macro of Excel, and this is the first time I ask a question here.
I did some research here about this problem, but still can't solve it.
I have several sheets named :page 1, page 2, etc, and I want to copy the data in specific range and paste them to the sheets named "ULD".
When I run the macro in page 1, everything works perfect. But when I run it in page 2 or other sheet, the new data will paste and replace the data pasted from page 1.
The following is my vba code:
Sub test()
Dim ws As Worksheet
Set ws = Sheets("ULD")
'Only Copy Visible Cells'
Range("L3:L100").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("ULD").Activate
For Each cell In ws.Range("I4:I10").Cells
If IsEmpty(cell) = True Then cell.Select.Paste: Exit For
Next cell
End Sub
Besides, I want the data being paste only value, how should I write the code?
There is some confusion, this is how I interpret what you are trying to do.
Sub test2()
Dim ws As Worksheet, LstRw As Long
Set ws = Sheets("ULD")
With ws
LstRw = .Cells(.Rows.Count, "I").End(xlUp).Row
End With
'Only Copy Visible Cells'
Range("L3:L100").SpecialCells(xlCellTypeVisible).Copy
ws.Range("I" & LstRw + 1).PasteSpecial xlPasteValues
Application.CutCopyMode = 0
End Sub
I have an audit that is done daily. I have been asked to save the audit points to a separate sheet for review by the mgmt. team. The code is below but when I run it I get an error: Script out of range.
Sub copy1()
Dim sheet2 As Worksheet
Dim sheet10 As Worksheet
Set sheet2 = Worksheets("sheet2")
Set sheet10 = Worksheets("sheet10")
sheet2.Range("a2:g10").Copy
sheet10.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).pastspecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
It's difficult to say for sure, but it is likely that you are receiving the "Subscript out of range" error because your workbook does not have the worksheets named "Sheet2" and "Sheet10".
The following code copies data from Sheet2 and pastes the values of the data to the next available row on Sheet10.
Sub Copy2()
Dim DestinationStartingCell As Range
Dim SheetRowCount As Long
Worksheets("Sheet2").Range("A2:I29").Copy
SheetRowCount = Worksheets("Sheet10").Rows.Count '1048576 for Excel 2007 and later
Set DestinationStartingCell = Worksheets("Sheet10") _
.Range("A" & SheetRowCount).End(xlUp).Offset(1, 0)
DestinationStartingCell.PasteSpecial xlPasteValues
End Sub
You could use this code
emptyrow=WorksheetFunction.CountA(Workbooks(<workbookname>).Sheets(10).Range("A:A"))+1
Workbooks(<workbookname>).Sheets(10).Cells(emptyrow,1).pastespecial xlPasteValues
to dynamically find the first empty row in Sheet10 to paste to.
I receive a workbook daily that lists 50 rows of information per page on a variable number of pages depending on how many rows there are total.
How can I copy the 50 rows from each page onto one master list?
From recording a macro I get
Sub Macro2()
Sheets("Page1_2").Select
Rows("5:54").Select
Selection.Copy
Sheets("Page1_1").Select
Range("A56").Select
ActiveSheet.Paste
End Sub
But I need it to loop through the entire workbook. I can't find a way to increment the sheet selection by 1 for each iteration and the paste range by 50.
Any help?
How about:
Sub test()
Dim curRow As Integer
Dim activeWorksheet As Worksheet
Set activeWorksheet = ActiveSheet
curRow = 1
For Each ws In ActiveWorkbook.Worksheets
If Not ws.Name = activeWorksheet.Name Then
ws.Range("5:54").Copy Destination:=activeWorksheet.Range(CStr(curRow) & ":" & CStr(curRow + 49))
curRow = curRow + 50
End If
Next ws
End Sub
It loops over all worksheets in the workbook and copies the contents to the current active sheet. The looping excludes the current active worksheet. It assumes that the contents that you are trying to aggregate are always in rows 5 through 54.