Excel VBA: Automatically insert sheet [duplicate] - excel

Hope someone can assist me with my issue. I want to copy specific columns and rows to another worksheet after the condition is met. my worksheet consist of 43 columns where in I have to copy only 29 columns.

The range for the code example below looks like this
Column A : Header in A1 = Country, A2:A? = Country names
Column B : Header in B1 = Name, B2:B? = Names
Column C : Header in C1 = Gender, C2:C? = F or M
Column D : Header in D1 = Birthday, D2:D? = Dates
This example copy all rows with the same value in the first column of the range to a new worksheet. It will do this for every unique value in this column. The sheets will be named after the Unique value.
Check if the information in these lines in the macro is correct before you run the macro
1: Set filter range on ActiveSheet: A1 is the top left cell of your filter range and the header of the first column, D is the last column in the filter range. You can also add the sheet name to the code like this :
Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
2: Set the Filter field:This example filter on the first column in the range (change the field if needed). In this case the range starts in A so Field 1 is column A, 2 = column B, ......
FieldNum = 1
3:Important:This macro call a function named LastRow
You find this function below the macro, copy this function together with the macro in a standard module
Sub Copy_To_Worksheets()
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
'and the header of the first column, D is the last column in the filter range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
'No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
My_Range.Parent.Select
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If
'This example filters on the first column in the range(change the field if needed)
'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
FieldNum = 1
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws2 = Worksheets.Add
With ws2
'first we copy the Unique data from the filter field to ws2
My_Range.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("A1"), Unique:=True
'loop through the unique list in ws2 and filter/copy to a new sheet
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A2:A" & Lrow)
'Filter the range
My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
'Check if there are no more then 8192 areas(limit of areas)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
.Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas for the value : " & cell.Value _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Split in worksheets"
Else
'Add a new worksheet
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = cell.Value
If Err.Number > 0 Then
ErrNum = ErrNum + 1
WSNew.Name = "Error_" & Format(ErrNum, "0000")
Err.Clear
End If
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
End If
'Show all data in the range
My_Range.AutoFilter Field:=FieldNum
Next cell
'Delete the ws2 sheet
On Error Resume Next
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End With
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
https://www.rondebruin.nl/win/s3/win006_4.htm

Related

how to copy values from one sheet where the values are starting from a specific row

I want to copy values from sheet named "Price Schedule" where the values which I want to start copying from "Row 10" and ONLY "Column D" and "Column F" should be copied. And paste it into another sheet named "Sheet1". It should start pasting values from "row 25" and paste under "Column H" and "Column I".
I want to put a condition statement where I want to copy only the rows which have the value grater than "zero" in "Column D" in sheet "Price Schedule" and paste it in "sheet1" under Column"H" and Column "I" starting from "row 25".
Private Sub CommandButton1_Click()
a = Worksheets("PRICE SCHEDULE").Cells(Rows.Count, 1).End(xlUp).Row
For I = 2 To a
If Worksheets("PRICE SCHEDULE").Cells(I, 4).Value = ">0" Then
Worksheets("PRICE SCHEDULE").Rows(I).Copy
Worksheets("Sheet1").Activate
b = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Paste
Worksheets("PRICE SCHEDULE").Activate
End If
Next
End Sub
I tried doing this and passed a msgbox to see the results but it shows no results of the copied data.
Please see the images for better understanding.
I'd use a filter for this task, like so:
Sub tgr()
Dim wb As Workbook
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim rDest As Range
Set wb = ActiveWorkbook
Set wsData = wb.Worksheets("Price Schedule")
Set wsDest = wb.Worksheets("Sheet1")
Set rDest = wsDest.Cells(wsDest.Rows.Count, "H").End(xlUp).Offset(1)
If rDest.Row < 25 Then Set rDest = wsDest.Range("H25")
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
With wsData.Range("D9:F" & wsData.Cells(wsData.Rows.Count, "D").End(xlUp).Row)
If .Row < 9 Then GoTo CleanExit 'No data
.AutoFilter 1, ">0", xlFilterValues 'Filter on column D for values >0
Intersect(.Worksheet.Range("D:D,F:F"), .Offset(1)).Copy 'Copy filtered values in columns D and F only
rDest.PasteSpecial xlPasteValues 'Paste values only to destination
.AutoFilter 'Clear filter
End With
CleanExit:
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Try something like the code below:
Option Explicit
Private Sub CommandButton1_Click()
Dim LastRow As Long, i As Long, b As Long
With Worksheets("PRICE SCHEDULE")
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 10 To LastRow ' loop from row 10 and forward
If .Range("D" & i).Value >= 0 Then
' first get the next empty row to paste
b = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
' copy column "D" to column "H"
.Range("D" & i).Copy Destination:=Worksheets("Sheet1").Range("H" & b)
' copy column "F" to column "I"
.Range("F" & i).Copy Destination:=Worksheets("Sheet1").Range("I" & b)
End If
Next
End With
End Sub

consolidate multiple worksheets into one worksheet

I want to consolidate multiple worksheets into one worksheet in the same excel, but i don't want some data after a specific word "Total" in all the worksheets. What should i do to delete the data after the word "Total" and then consolidate all the sheets.
Below code is written to add multiple worksheets.
Sub Consolidate()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim erow As Long, lrowsh As Long, startrow As Long
Dim CopyRng As Range
startrow = 3
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Deleting "Consolidate" sheet
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Consolidate").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Adding worksheet with the name "Consolidate"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Consolidate"
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
'Find the next blank or empty row on the DestSh
erow = DestSh.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
'Find the last row with data in the Sheet
lrowsh = sh.Range("A" & Rows.Count).End(xlUp).Row
Set CopyRng = sh.Range(sh.Rows(startrow), sh.Rows(lrowsh))
'copies Values / formats
CopyRng.Copy
With DestSh.Cells(erow, 1)
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
Next
End Sub
Interesting Workbook Consolidation
Change the constants (Const) to fit your needs.
The Code
Sub Consolidate()
' Target
Const cTarget As String = "Consolidate" ' Target Worksheet Name
' Source
Const cFR As Long = 3 ' First Row Number
Const cLRC As Variant = 1 ' Last-Row Column Letter/Column Number
Const cCrit As String = "Total" ' Criteria
Dim wb As Workbook ' Target Workbook
Dim wsT As Worksheet ' Target Worksheet
Dim ws As Worksheet ' Current Source Worksheet
Dim eRow As Long ' Target First Empty Row
Dim lRow As Long ' Source Last Used Row
Dim lCol As Long ' Source Last Used Column
Dim rngCell As Range ' Cell Ranges
Dim rng As Range ' Ranges
' Speed up.
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
' Create a reference to Target Workbook. If the code will NOT be in the
' workbook to be processed, then use its name (preferable) or
' ActiveWorkbook instead of ThisWorkbook.
Set wb = ThisWorkbook
' Note: Instead of the following with block you could use code to clear
' or clear the contents of the Target Worksheet.
With wb
'Delete Target Worksheet.
Application.DisplayAlerts = False
On Error Resume Next
.Worksheets("Consolidate").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add Target Worksheet.
Set wsT = .Worksheets.Add(Before:=.Sheets(1)) ' First Tab
wsT.Name = "Consolidate"
End With
' Handle errors.
On Error GoTo ErrorHandler
' Loop through all worksheets.
For Each ws In wb.Worksheets
If ws.Name <> wsT.Name Then
With ws.Cells(cFR, cLRC).Resize(ws.Rows.Count - cFR + 1, _
ws.Columns.Count - cLRC + 1)
' Note: Choose only one of the following two lines.
'Find the first occurrence of Criteria in Current Worksheet.
Set rngCell = .Find(cCrit, .Cells(.Rows.Count, .Columns _
.Count), xlValues, xlWhole, xlByRows, xlNext)
' 'Find the last occurrence of Criteria in Current Worksheet.
' Set rng = .Find(cCrit, , xlValues, xlWhole, xlByRows, _
' xlPrevious)
' Clear the range below the row where Criteria was found.
ws.Rows(rngCell.Row + 1 & ":" & ws.Rows.Count).Clear
' Create a reference to Row Range (of Copy Range).
Set rng = .Cells(1).Resize(rngCell.Row - cFR + 1, _
.Columns.Count - cLRC + 1)
End With
' Create a reference to last cell in last column of Row
' Range (of Copy Range).
Set rngCell = rng.Find("*", , xlFormulas, , _
xlByColumns, xlPrevious)
' Create a reference to Copy Range.
Set rng = rng.Cells(1).Resize(rng.Rows.Count, _
rngCell.Column - cLRC + 1)
'Find the next blank or empty row in Target Worksheet.
eRow = wsT.Cells(wsT.Rows.Count, cLRC).End(xlUp) _
.Offset(1, 0).Row
' Copy Copy Range.
rng.Copy
' In (First Empty Row of) Target Worksheet
With wsT.Cells(eRow, 1)
' First paste the formats to avoid trouble mostly when pasting
' dates or time. Excel might firstly format it differently, and
' when pasting the formats might not revert to desired formats.
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
End If
Next
' Go to the top of Target Worksheet.
ActiveSheet.Range("A1").Select
' Inform user of success (Since the code is fast, you might not know if it
' had run at all).
MsgBox "The operation finished successfully.", vbInformation, "Success"
ProcedureExit:
' Speed down.
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
Exit Sub
ErrorHandler:
MsgBox "An unexpected error occurred. Error '" & Err.Number & "': " _
& Err.Description, vbCritical, "Error"
GoTo ProcedureExit
End Sub

vba macro to paste data from multiple sheets one after another

I have an excel workbook that has invoice data for every month with an identical layout. I was wondering if there is a macro that can copy the data from each sheet and paste it all one after the other.
So the first sheet is P1, then P2, P3 etc until P12. I want a macro that will paste P1 data onto a new sheet, then P2 data right underneath it, then P3 etc until the end.
I imagine this would be some sort of For loop, but I'm not sure what the code would look like (I'm very new to vba)
Thank you in advance!!!
How about this option?
Sub CopyDataWithoutHeaders()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "RDBMergeSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name "RDBMergeSheet"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"
'Fill in the start row
StartRow = 2
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
'Find the last row with data on the DestSh and sh
Last = LastRow(DestSh)
shLast = LastRow(sh)
'If sh is not empty and if the last row >= StartRow copy the CopyRng
If shLast > 0 And shLast >= StartRow Then
'Set the range that you want to copy
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
'This example copies values/formats, if you only want to copy the
'values or want to copy everything look below example 1 on this page
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
https://www.rondebruin.nl/win/s3/win002.htm
Because details are very limited in order to get an idea of the sheets' structure i try to create general code which with some modifications will satisfy your needs.
Option Explicit
Sub test()
Dim wsTest As Worksheet, ws As Worksheet
Dim LRW As Long, LRF As Long, LCW As Long
'Here we create a separate sheet namded wsFull to paste the data in it.
Set wsTest = Nothing
On Error Resume Next
Set wsTest = ActiveWorkbook.Worksheets("wsFull")
On Error GoTo 0
If wsTest Is Nothing Then
Worksheets.Add.Name = "wsFull"
End If
Set wsTest = ActiveWorkbook.Worksheets("wsFull")
'Here we loop all sheets except the new sheet named wsFull
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "wsFull" Then
With ws
'Here we find last column (using first row) & last row (using Column A) for each sheet we loop
LRW = .Cells(.Rows.Count, "A").End(xlUp).Row
LCW = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
'Here we find the last row of wsFull in order to find where we will paste the data in.
LRF = wsTest.Cells(wsTest.Rows.Count, "A").End(xlUp).Row
'We paste the data in column A
If LRF = 1 And wsTest.Range("A1").Value = "" Then
ws.Range(ws.Cells(1, 1), ws.Cells(LRW, LCW)).Copy wsTest.Range("A1")
Else
ws.Range(ws.Cells(1, 1), ws.Cells(LRW, LCW)).Copy wsTest.Range("A" & LRF + 1)
End If
End If
Next ws
End Sub

How to copy data from one worksheet to another with condition

Hope someone can assist me with my issue. I want to copy specific columns and rows to another worksheet after the condition is met. my worksheet consist of 43 columns where in I have to copy only 29 columns.
The range for the code example below looks like this
Column A : Header in A1 = Country, A2:A? = Country names
Column B : Header in B1 = Name, B2:B? = Names
Column C : Header in C1 = Gender, C2:C? = F or M
Column D : Header in D1 = Birthday, D2:D? = Dates
This example copy all rows with the same value in the first column of the range to a new worksheet. It will do this for every unique value in this column. The sheets will be named after the Unique value.
Check if the information in these lines in the macro is correct before you run the macro
1: Set filter range on ActiveSheet: A1 is the top left cell of your filter range and the header of the first column, D is the last column in the filter range. You can also add the sheet name to the code like this :
Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
2: Set the Filter field:This example filter on the first column in the range (change the field if needed). In this case the range starts in A so Field 1 is column A, 2 = column B, ......
FieldNum = 1
3:Important:This macro call a function named LastRow
You find this function below the macro, copy this function together with the macro in a standard module
Sub Copy_To_Worksheets()
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
'and the header of the first column, D is the last column in the filter range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
'No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
My_Range.Parent.Select
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If
'This example filters on the first column in the range(change the field if needed)
'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
FieldNum = 1
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws2 = Worksheets.Add
With ws2
'first we copy the Unique data from the filter field to ws2
My_Range.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("A1"), Unique:=True
'loop through the unique list in ws2 and filter/copy to a new sheet
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A2:A" & Lrow)
'Filter the range
My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
'Check if there are no more then 8192 areas(limit of areas)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
.Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas for the value : " & cell.Value _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Split in worksheets"
Else
'Add a new worksheet
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = cell.Value
If Err.Number > 0 Then
ErrNum = ErrNum + 1
WSNew.Name = "Error_" & Format(ErrNum, "0000")
Err.Clear
End If
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
End If
'Show all data in the range
My_Range.AutoFilter Field:=FieldNum
Next cell
'Delete the ws2 sheet
On Error Resume Next
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End With
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
https://www.rondebruin.nl/win/s3/win006_4.htm

Copy specified columns to a worksheet based on value in column A

I have the following which works ok but instead of copying the entire row from the "Combined" worksheet to the "Summary" worksheet I only want to copy columns A to T. This is a first attempt so any help would be gratefully received!
`Private Sub CommandButton1_Click()
'Define Variables
Dim DestSh As Worksheet
Dim s As Worksheet
Dim c As Integer
Dim i
Dim LastRow
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the Combined sheet if it exists
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Combined").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a new Combined worksheet
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Combined"
'Select Summary worksheet and copy headings and column widths to Combined worksheet
Sheets("Summary").Activate
Range("A24").EntireRow.Select
Selection.Copy Destination:=Sheets("Combined").Range("A1")
For c = 1 To Sheets("Summary").Columns.Count
Sheets("Combined").Columns(c).ColumnWidth = Sheets("Summary").Columns(c).ColumnWidth
Next
'Loop through all worksheets sheets that begin with ra
'and copy to the combined worksheet
For Each s In ActiveWorkbook.Sheets
If LCase(Left(s.Name, 2)) = "ra" Then
Application.Goto Sheets(s.Name).[A1]
Selection.Range("A23:Q50").Select
Selection.Copy Destination:=Sheets("Combined"). _
Cells(Rows.Count, 1).End(xlUp)(2)
End If
Next
'Copy all rows that contain Yes in column A to Summary worksheet
LastRow = Sheets("Combined").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Summary").Range("A25:V500").ClearContents
For i = 1 To LastRow
If Sheets("Combined").Cells(i, "A").Value = "Yes" Then
Sheets("Combined").Cells(i, "A").EntireRow.Copy Destination:=Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
'Force return to Summary worksheet
Worksheets("Summary").Activate
End Sub
You can use the .Resize() method to change the range that is copied. Replace your line where you copy and paste it to the new destination with this one and it should work:
Sheets("Combined").Cells(i, "A").Resize(1, 20).Copy Destination:=Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Offset(1)

Resources