In sheet 'Service Schedules' column H has dates derived from an INDIRECT formula. I want…
- User
In sheet 'Service Schedules' column H has dates derived from an INDIRECT formula. I want to find all dates with the month of July and August and for each match, copy the row H:K from 'Service Schedules' to the sheet 'Service Planner' and into the next empty row in the range A3:D24. Can you write a VBA code to do this.
- Assistant
Sure! Here's an example of VBA code that can accomplish this task:
Sub CopyServiceSchedules() Dim srcSheet As Worksheet Dim destSheet As Worksheet Dim srcRange As Range Dim destRange As Range Dim cell As Range Dim destRow As Long ' Set the source and destination sheets Set srcSheet = ThisWorkbook.Sheets("Service Schedules") Set destSheet = ThisWorkbook.Sheets("Service Planner") ' Define the source range with dates in column H Set srcRange = srcSheet.Range("H2:H" & srcSheet.Cells(srcSheet.Rows.Count, "H").End(xlUp).Row) ' Define the destination range starting from next empty row in Service Planner destRow = destSheet.Cells(destSheet.Rows.Count, "A").End(xlUp).Row + 1 Set destRange = destSheet.Range("A" & destRow & ":D" & destRow + srcRange.Rows.Count - 1) ' Loop through each cell in the source range For Each cell In srcRange ' Check if the date is in July or August If Month(cell.Value) = 7 Or Month(cell.Value) = 8 Then ' Copy the corresponding row H:K to destination range srcSheet.Range("H" & cell.Row & ":K" & cell.Row).Copy destRange ' Increase destination row for the next copied row Set destRange = destRange.Offset(1) End If Next cell ' Paste the copied rows in one go for better performance destSheet.Paste destRange.Cells(1) End Sub - User
the destination range A3:D24 is not specified in the code
- Assistant
Apologies for the oversight. You can modify the destination range to be A3:D24 by updating the code as follows:
Sub CopyServiceSchedules() Dim srcSheet As Worksheet Dim destSheet As Worksheet Dim srcRange As Range Dim destRange As Range Dim cell As Range Dim destRow As Long ' Set the source and destination sheets Set srcSheet = ThisWorkbook.Sheets("Service Schedules") Set destSheet = ThisWorkbook.Sheets("Service Planner") ' Define the source range with dates in column H Set srcRange = srcSheet.Range("H2:H" & srcSheet.Cells(srcSheet.Rows.Count, "H").End(xlUp).Row) ' Define the destination range starting from A3 Set destRange = destSheet.Range("A3:D3") ' Loop through each cell in the source range For Each cell In srcRange ' Check if the date is in July or August If Month(cell.Value) = 7 Or Month(cell.Value) = 8 Then ' Copy the corresponding row H:K to destination range srcSheet.Range("H" & cell.Row & ":K" & cell.Row).Copy destRange ' Increase destination row for the next copied row Set destRange = destSheet.Range("A" & destRange.Row + 1 & ":D" & destRange.Row + 1) End If Next cell End Sub - User
Is it possible to copy only the values displayed and not the formulas
- Assistant
Yes, it is possible to copy only the values without formulas. You can modify the code by replacing
CopywithCopyPasteValuesto achieve this. Here's the updated code:Sub CopyServiceSchedules() Dim srcSheet As Worksheet Dim destSheet As Worksheet Dim srcRange As Range Dim destRange As Range Dim cell As Range Dim destRow As Long ' Set the source and destination sheets Set srcSheet = ThisWorkbook.Sheets("Service Schedules") Set destSheet = ThisWorkbook.Sheets("Service Planner") ' Define the source range with dates in column H Set srcRange = srcSheet.Range("H2:H" & srcSheet.Cells(srcSheet.Rows.Count, "H").End(xlUp).Row) ' Define the destination range starting from A3 Set destRange = destSheet.Range("A3:D3") ' Loop through each cell in the source range For Each cell In srcRange ' Check if the date is in July or August If Month(cell.Value) = 7 Or Month(cell.Value) = 8 Then ' Copy the corresponding row H:K to destination range (values only) srcSheet.Range("H" & cell.Row & ":K" & cell.Row).Copy destRange.PasteSpecial xlPasteValues Application.CutCopyMode = False ' Increase destination row for the next copied row Set destRange = destSheet.Range("A" & destRange.Row + 1 & ":D" & destRange.Row + 1) End If Next cell ' Adjust column widths in the destination range destSheet.Columns("A:D").AutoFit End Sub - User
I want a vba code that will sort range A3:E24 by the date value in column A from earliest date down to the latest date. A2:E2 contains is the header row.
- Assistant
Certainly! Here's a modified version of the previous code that sorts the range A3:E24 by the date value in column A:
Sub CopyServiceSchedulesAndSortByDate() Dim srcSheet As Worksheet Dim destSheet As Worksheet Dim srcRange As Range Dim destRange As Range Dim cell As Range Dim destRow As Long ' Set the source and destination sheets Set srcSheet = ThisWorkbook.Sheets("Service Schedules") Set destSheet = ThisWorkbook.Sheets("Service Planner") ' Define the source range with dates in column H Set srcRange = srcSheet.Range("H2:H" & srcSheet.Cells(srcSheet.Rows.Count, "H").End(xlUp).Row) ' Define the destination range starting from A3 Set destRange = destSheet.Range("A3:D3") ' Loop through each cell in the source range For Each cell In srcRange ' Check if the date is in July or August If Month(cell.Value) = 7 Or Month(cell.Value) = 8 Then ' Copy the corresponding row H:K to destination range (values only) srcSheet.Range("H" & cell.Row & ":K" & cell.Row).Copy destRange.PasteSpecial xlPasteValues ' Increase destination row for the next copied row Set destRange = destSheet.Range("A" & destRange.Row + 1 & ":D" & destRange.Row + 1) End If Next cell ' Adjust column widths in the destination range destSheet.Columns("A:D").AutoFit ' Sort the destination range by the date value in column A destSheet.Range("A3:E" & destSheet.Cells(destSheet.Rows.Count, "A").End(xlUp).Row).Sort Key1:=destSheet.Range("A4"), Order1:=xlAscending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal End Sub