I would like the VBA below to copy also to sheet 'Archive' exactly as it does to sheet…
- User
I would like the VBA below to copy also to sheet 'Archive' exactly as it does to sheet 'Asset Events' Can you please adjust the VBA to do this.
Sub CopyDataToAssetEvents() Application.EnableEvents = False Dim wsBooking As Worksheet Dim wsAssetEvents As Worksheet Dim wsBookingArchive As Worksheet Dim nextRow As Long
'Set references to the 'Booking' and 'Asset Events' sheets Set wsBooking = ThisWorkbook.Worksheets("Booking") Set wsAssetEvents = ThisWorkbook.Worksheets("Asset Events") Set wsBookingArchive = ThisWorkbook.Worksheets("Archive") 'Find the next available row in 'Asset Events' sheet column A nextRow = wsAssetEvents.Cells(wsAssetEvents.Rows.Count, "A").End(xlUp).Row + 1 'Copy data from 'Booking' sheet to 'Asset Events' sheet With wsBooking 'Copy A2 to column A in 'Asset Events' sheet on the next available row wsAssetEvents.Cells(nextRow, "A").Value = .Range("A2").Value 'Copy A4 to column B in 'Asset Events' sheet on the same row wsAssetEvents.Cells(nextRow, "B").Value = .Range("A4").Value 'Copy A6 to column C in 'Asset Events' sheet on the same row wsAssetEvents.Cells(nextRow, "C").Value = .Range("A6").Value 'Copy A8 to column D in 'Asset Events' sheet on the same row wsAssetEvents.Cells(nextRow, "D").Value = .Range("A8").Value 'Copy A10 to column E in 'Asset Events' sheet on the same row wsAssetEvents.Cells(nextRow, "E").Value = .Range("A10").Value 'Copy A12 to column F in 'Asset Events' sheet on the same row wsAssetEvents.Cells(nextRow, "F").Value = .Range("A12").Value 'Copy A14 to column G in 'Asset Events' sheet on the same row wsAssetEvents.Cells(nextRow, "G").Value = .Range("A14").Value 'Copy A27 to column H in 'Asset Events' sheet on the same row wsAssetEvents.Cells(nextRow, "H").Value = .Range("A27").Value End With 'Find the next available row in 'Asset Events' sheet column A nextRow = wsBookingArchive.Cells(wsBookingArchive.Rows.Count, "A").End(xlUp).Row + 1 'Copy A2 to column A in 'Archive' sheet on the next available row wsBookingArchive.Cells(nextRow, "A").Value = .Range("A2").Value 'Copy A4 to column B in 'Archive' sheet on the same row wsBookingArchive.Cells(nextRow, "B").Value = .Range("A4").Value 'Copy A6 to column C in 'Archive' sheet on the same row wsBookingArchive.Cells(nextRow, "C").Value = .Range("A6").Value 'Copy A8 to column D in 'Archive' sheet on the same row wsBookingArchive.Cells(nextRow, "D").Value = .Range("A8").Value 'Copy A10 to column E in 'Archive' sheet on the same row wsBookingArchive.Cells(nextRow, "E").Value = .Range("A10").Value 'Copy A12 to column F in 'Archive' sheet on the same row wsBookingArchive.Cells(nextRow, "F").Value = .Range("A12").Value 'Copy A14 to column G in 'Archive' sheet on the same row wsBookingArchive.Cells(nextRow, "G").Value = .Range("A14").Value 'Copy A27 to column H in 'Archive' sheet on the same row wsBookingArchive.Cells(nextRow, "H").Value = .Range("A27").Value End With MsgBox "Data has been successfully copied to 'Asset Events'." wsBooking.Range("A2").ClearContents wsBooking.Range("A4").ClearContents wsBooking.Range("A6").ClearContents wsBooking.Range("A8").ClearContents wsBooking.Range("A10").ClearContents wsBooking.Range("A12").ClearContents wsBooking.Range("A14:A25").ClearContents wsBooking.Range("A27").ClearContents wsBooking.Range("D2:J" & wsBooking.Cells(wsBooking.Rows.Count, "D").End(xlUp).Row).Interior.ColorIndex = xlNone - Assistant
- User
Can you please write me an excel VBA event that does the following:
On sheet 'Asset Events' activate starting from row 2, check if the date value in column E is before the current week indicating that the date has expired. If the date is now in the past, delete the entire row so that the rows below can move up.
- Assistant
Certainly! Below is a VBA code snippet that implements the functionality you've described. It checks each row starting from row 2 in the "Asset Events" sheet, evaluates the date in column E, and deletes the row if the date is before the start of the current week.
- User
Is it possible to set Interior.ColorIndex = xlNone for a group of ranges, D3:J8, D10:J15, D17:J22, D24:J29, D31:J36
wsBooking.Range("D2:J" & wsBooking.Cells(wsBooking.Rows.Count, "D").End(xlUp).Row).Interior.ColorIndex = xlNone
- Assistant
Yes, it is certainly possible to set the
Interior.ColorIndextoxlNonefor specific non-contiguous ranges in Excel using VBA. To apply this to the specified ranges (D3:J8, D10:J15, D17:J22, D24:J29, D31:J36) in the specified worksheet, you can use theUnionmethod to combine these ranges together.Here's how you can do it:
Sub ClearColorForSpecificRanges() Dim wsBooking As Worksheet Set wsBooking = ThisWorkbook.Worksheets("Booking") ' Change "Booking" if your sheet has a different name ' Use Union to combine the specified ranges Dim targetRange As Range Set targetRange = Union(wsBooking.Range("D3:J8"), _ wsBooking.Range("D10:J15"), _ wsBooking.Range("D17:J22"), _ wsBooking.Range("D24:J29"), _ wsBooking.Range("D31:J36")) ' Set the interior color of the combined range to none targetRange.Interior.ColorIndex = xlNone End Sub - User
In this VBA I want to change the MsgBox to a Yes No message box. asking "The start date already has a booking." line break "Check Period of booking in Asset Event" line break "Yes to continue and add date" line break "No to check periods do not clash" If No, clear contents of A8 and Exit Sub
' Check if the changed cell is A8 If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, wsBooking.Range("A8")) Is Nothing Then Application.EnableEvents = False Dim startDate As Date Dim cell2 As Range
startDate = wsBooking.Range("A8").Value For Each cell2 In wsBooking.Range("D2:J" & wsBooking.Cells(wsBooking.Rows.Count, "D").End(xlUp).Row) If cell2.Value = startDate And cell2.Interior.Color = RGB(255, 0, 0) Then MsgBox "The start date already has a booking." & vbCrLf & vbCrLf & "Check Period of booking in Asset Event", vbCritical, "DATE CLASH" Exit Sub End If Next cell2 wsBooking.Range("A10").ClearContents wsBooking.Range("A12").ClearContents wsBooking.Range("A14:A25").ClearContents wsBooking.Range("A27").ClearContents Application.EnableEvents = TrueEnd If
' Check if the changed cell is A10 If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, wsBooking.Range("A10")) Is Nothing Then Application.EnableEvents = False Call Module2.WriteNewDatesToMatchingSheet wsBooking.Range("A12").ClearContents wsBooking.Range("A14:A25").ClearContents wsBooking.Range("A27").ClearContents Application.EnableEvents = True End If
- Assistant
- User
I applied a sort to this code but it did not work: Private Sub Worksheet_Activate() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim currentDate As Date Dim startOfWeek As Date
' Set the worksheet Set ws = ThisWorkbook.Worksheets("Asset Events") With ws .Range("A1:H" & lastRow).Sort Key1:=.Range("D1:D" & lastRow), _ Key2:=.Range("F1:F" & lastRow), _ Order1:=xlAscending, Order2:=xlAscending, Header:=xlYes End With Application.Wait (Time + TimeValue("00:00:01")) ' Get the current date and the start of the current week (Monday) currentDate = Date startOfWeek = currentDate - Weekday(currentDate, vbMonday) + 1 ' Find the last row in column E lastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row ' Loop through rows from the last to the second For i = lastRow To 2 Step -1 ' Check if the value in column E is a date and if it is before the start of the week If IsDate(ws.Cells(i, "E").Value) Then If ws.Cells(i, "E").Value < startOfWeek Then ' Delete the entire row if the date is before the current week ws.Rows(i).Delete End If End If Next iEnd Sub
- Assistant
- User
Can you please add the following condition to the vba code below:
If any of the cells are blank A2, A4, A6, A8, A10, A12 then exit the sub
Sub CopyDataToAssetEvents() Application.EnableEvents = False Dim wsBooking As Worksheet Dim wsAssetEvents As Worksheet Dim wsBookingArchive As Worksheet Dim nextRow As Long
'Set references to the 'Booking' and 'Asset Events' and 'Archive' sheets Set wsBooking = ThisWorkbook.Worksheets("Booking") Set wsAssetEvents = ThisWorkbook.Worksheets("Asset Events") Set wsBookingArchive = ThisWorkbook.Worksheets("Archive") 'Find the next available row in 'Asset Events' sheet column A nextRow = wsAssetEvents.Cells(wsAssetEvents.Rows.Count, "A").End(xlUp).Row + 1 'Copy data from 'Booking' sheet to 'Asset Events' sheet With wsBooking 'Copy A27 to column H in 'Asset Events' sheet on the same row wsAssetEvents.Cells(nextRow, "H").Value = .Range("A27").Value End WithEnd Sub
- Assistant