I have a button that calls 'CopyMatchingValues'. The 'CopyMatchingValues' event then…
- User
I have a button that calls 'CopyMatchingValues'. The 'CopyMatchingValues' event then requires me to select the range of values in column C that I want to process using an input box. If the OvertimeCell.Value that matches the OTFormRange.Value from the selection, exceeds more than 11 matching values, I want the Sub to Exit, the input box to close and the event to call 'CopyMatchingXValues' Can you please show me how to do this.
Sub CopyMatchingValues() Dim OvertimeSheet As Worksheet Dim OTFormSheet As Worksheet Dim OvertimeRange As Range Dim OTFormRange As Range Dim OvertimeCell As Range Dim OTFormCell As Range Dim LastRow As Long Dim i As Long Set OvertimeSheet = ThisWorkbook.Sheets("Overtime") On Error Resume Next Set OvertimeRange = Application.InputBox("Select range in column C", Type:=8) On Error GoTo 0 If OvertimeRange Is Nothing Or Not OvertimeRange.Columns(1).Column = 3 Then MsgBox "No valid range selected in column C.", vbCritical Exit Sub End If Set OTFormSheet = ThisWorkbook.Sheets("OTForm") Set OTFormRange = OTFormSheet.Range("B4") LastRow = 11 For Each OvertimeCell In OvertimeRange If OvertimeCell.Value = OTFormRange.Value Then OTFormSheet.Cells(LastRow, "D").Value = OvertimeCell.Offset(0, 28).Value OTFormSheet.Cells(LastRow, "C").Value = OvertimeCell.Offset(0, 27).Value OTFormSheet.Cells(LastRow, "B").Value = OvertimeCell.Offset(0, 1).Value OTFormSheet.Cells(LastRow, "A").Value = OvertimeCell.Offset(0, -2).Value LastRow = LastRow + 1 End If Next OvertimeCell OTFormSheet.Range("I24").Value = Date OTFormSheet.Range("I25").Value = Date Dim message As String message = OTFormSheet.Range("B4").Value & "'s overtime dates have been entered" MsgBox message, vbInformation, "OVERTIME ENTRY" ThisWorkbook.Sheets("OTForm").Activate End Sub
Sub CopyMatchingXValues() MsgBox "Values found exceed space allocated for standard sheet, please use Extended Overtime Sheet" Dim OvertimeSheet As Worksheet Dim OTFormXSheet As Worksheet Dim OvertimeRange As Range Dim OTFormXRange As Range Dim OvertimeCell As Range Dim OTFormXCell As Range Dim LastRow As Long Dim i As Long Set OvertimeSheet = ThisWorkbook.Sheets("Overtime") On Error Resume Next Set OvertimeRange = Application.InputBox("Select range in column C", Type:=8) On Error GoTo 0 If OvertimeRange Is Nothing Or Not OvertimeRange.Columns(1).Column = 3 Then MsgBox "No valid range selected in column C.", vbCritical Exit Sub End If Set OTFormXSheet = ThisWorkbook.Sheets("OTFormX") Set OTFormXRange = OTFormXSheet.Range("B4") LastRow = 11 For Each OvertimeCell In OvertimeRange If OvertimeCell.Value = OTFormXRange.Value Then OTFormXSheet.Cells(LastRow, "D").Value = OvertimeCell.Offset(0, 28).Value OTFormXSheet.Cells(LastRow, "C").Value = OvertimeCell.Offset(0, 27).Value OTFormXSheet.Cells(LastRow, "B").Value = OvertimeCell.Offset(0, 1).Value OTFormXSheet.Cells(LastRow, "A").Value = OvertimeCell.Offset(0, -2).Value LastRow = LastRow + 1 End If Next OvertimeCell OTFormXSheet.Range("I45").Value = Date OTFormXSheet.Range("I46").Value = Date Dim message As String message = OTFormXSheet.Range("B4").Value & "'s overtime dates have been entered" MsgBox message, vbInformation, "OVERTIME ENTRY" ThisWorkbook.Sheets("OTFormX").Activate End Sub
- Assistant
To accomplish this, you can add an additional check within the loop in the 'CopyMatchingValues' sub to count the number of matching values. If the count exceeds 11, you can exit the loop and call the 'CopyMatchingXValues' sub.
Here's an updated version of your code:
- User
The code worked and called 'CopyMatchingXValues', but it also copied the values to the OTFormSheet
- Assistant
- User
I would like to add a Yes No message that does the following; Message "STAFF FORM IN OVERTIME FORM WILL BE SEARCHED" vbNewline "Have you changed the Staff Name to correspond to your search" vbNewline "Yes- to continue, No - to stop search" If Yes is selected, then continue with rest of code, if No is selected then Exit Sub.
- Assistant
- User
I have created a Form called 'StaffForm1' with the following Text Boxes; 'FullName' 'JobTitle' 'ContType' 'WorkHrs' and a Submit button. I want the form to appear when I call event 'CopyMatchingValues'. All the text boxes must be filled before the submit button can executed. The values in the text boxes must then be copied to the following sheet in this order; 'FullName' copied to sheet 'OTForm' range "B4" 'JobTitle' copied to sheet 'OTForm' range "B7" 'ContType' copied to sheet 'OTForm' range "E4" 'WorkHrs' copied to sheet 'OTForm' range "E5" Can you please write the necessary codes for me and tell me where to place the codes.
- Assistant