In the VBA below, when 'x' is written to row AB25:CP25, it starts the 'x' in the correct…
- User
In the VBA below, when 'x' is written to row AB25:CP25, it starts the 'x' in the correct column that has the expected date value in AB22:CP22. I want it to also write the date value in AB22:CP22 of each 'x' in each column to row 30. The current code 'wsLeave.Cells(30, i).Value = startDate' is writing the start date value only into row 30 for all columns with 'x'and the date is not incrementing.
Sub ProcessLeaveRequestGHI(wsLeave As Worksheet, firstMonthRange As Range, secondMonthRange As Range, Target As Range) Dim StartNumber As Long Dim StopNumber As Long Dim startDate As Date Dim endDate As Date Dim wsHolCard As Worksheet Dim startCol As Long Dim endCol As Long Dim cell As Range
codeSet wsHolCard = ThisWorkbook.Worksheets("HolCard") ' Record the Start Date and End Date from HolCard sheet startDate = wsHolCard.Cells(Target.row, "G").Value endDate = wsHolCard.Cells(Target.row, "H").Value ' Unprotect the 'Leave' sheet wsLeave.Unprotect Password:="edit" ' Find the corresponding column in 'Leave' sheet for Start Date For Each cell In wsLeave.Range("AB22:CP22") If cell.Value = startDate Then startCol = cell.Column Exit For End If Next cell ' Find the corresponding column in 'Leave' sheet for End Date For Each cell In wsLeave.Range("AB22:CP22") If cell.Value = endDate Then endCol = cell.Column Exit For End If Next cell ' Check if 'x' values already exist in the range AB25:CP25 For i = startCol To endCol If wsLeave.Cells(25, i).Value = "x" Then MsgBox "Warning: 'x' values already exist in the range.", vbExclamation, "Data Conflict" Exit Sub End If Next i ' Write 'x' from Start Date to End Date in row 25 For i = startCol To endCol wsLeave.Cells(25, i).Value = "x" wsLeave.Cells(30, i).Value = startDate Next i ' Protect the sheet 'Leave' wsLeave.Protect Password:="edit" ' Unprotect the 'HolCard' sheet, add a comment, and protect the sheet again wsHolCard.Unprotect Password:="edit" Target.AddComment "Leave dates recorded " & Date wsHolCard.Protect Password:="edit" MsgBox "Leave Dates Recorded", vInformation, "LEAVE RECORD" - Assistant
- User
- Assistant
- User
- Assistant
- User
How will your vVBA code 'Sub DetermineMonthRangeToCopy(Target As Range)' also transfer the next months range to the following VBA code 'Sub CopyPaste2Values()' so that I do not have to assume incorrectly 'ws.Range("BG12:CK17")' as the next months range
Sub CopyPaste2Values()
codeDim ws As Worksheet Dim firstDayOfNextMonth As Date Dim firstDayCol As Integer ' Unprotect the sheet Sheets("Leave").Unprotect Password:="edit" Set ws = ThisWorkbook.Sheets("Leave") ' Find the first day of the next month firstDayOfNextMonth = DateSerial(Year(ws.Cells(1, 2).Value), Month(ws.Cells(1, 2).Value) + 1, 1) ' Find the column for the first day of the next month firstDayCol = Application.WorksheetFunction.Match(CDbl(firstDayOfNextMonth), ws.Rows(1), 0) ' Copy the range BG12:CK17 ws.Range("BG12:CK17").Copy ' Paste values only to the cell in row 2 of the column where the first day of the next month starts ws.Cells(2, firstDayCol).PasteSpecial Paste:=xlPasteValues ' Clear clipboard Application.CutCopyMode = False Application.Wait Now + TimeValue("00:00:01") Call Clear12ValuesInRange - Assistant
- User
How does the module 'Sub Clear1ValuesInRange()' now call 'Sub DetermineMonthRangeToCopy(Target As Range)'
Sub Clear1ValuesInRange()
' Unprotect the sheet Sheets("Leave").Unprotect Password:="edit"
codeDim ws As Worksheet Set ws = ThisWorkbook.Sheets("Leave") Dim rng As Range Set rng = ws.Range("B2:CP7") rng.Value = "" Application.Wait Now + TimeValue("00:00:01") Call DetermineMonthRangeToCopyEnd Sub
Sub DetermineMonthRangeToCopy(Target As Range) Dim ws As Worksheet Dim firstMonthStart As Date Dim firstMonthEnd As Date Dim secondMonthStart As Date Dim secondMonthEnd As Date Dim endDateCol As Long Dim nextEndDateCol As Long Dim lastRow As Long Dim monthRange As Range Dim nextMonthRange As Range
codeSet ws = ThisWorkbook.Worksheets("Leave") ws.Unprotect Password:="edit" - Assistant