The code below, copies only the cell row selected in the worksheet 'PrmRequests' of the…
- User
The code below, copies only the cell row selected in the worksheet 'PrmRequests' of the workbook 'Premises Requests' to the sheet 'Request Events' of the workbook 'Service Providers'. I want a similar code 'CopyAllPRtoEvents' that will copy all the same rows where the value in column E is not empty from row 2 downwards but not the E1 row. Before executing the copy, it must first Clear the contents of the sheet 'Request Events' from row 2 downwards.
Sub CopyPRtoEvents() Dim prmrequestsWorkbook As Workbook Set prmrequestsWorkbook = Workbooks("Premises Requests") Dim serviceprovidersWorkbook As Workbook Set serviceprovidersWorkbook = Workbooks("Service Providers")
Dim prmrequestsSheet As Worksheet Set prmrequestsSheet = ThisWorkbook.Sheets("PrmRequests") Dim selectedRow As Range Set selectedRow = Selection.EntireRow Dim prmrequestsRange As Range 'Set prmrequestsRange = prmrequestsSheet.Range("B" & selectedRow.Row & ":D" & selectedRow.Row) Set prmrequestsRange = prmrequestsSheet.Range("A" & selectedRow.Row & ":H" & selectedRow.Row) If Not IsWorkbookOpen("Service Providers") Then MsgBox "Workbook 'Service Providers' not open" Exit Sub End If
Dim response As Integer response = MsgBox("Have you selected a cell in the Event Row you would like to send to the Request Events?" & vbCrLf & "If not, please select a cell in the row to send.", vbQuestion + vbYesNo, "Cell Selection") If response = vbNo Then Exit Sub End If
Dim reventsSheet As Worksheet Set reventsSheet = serviceprovidersWorkbook.Sheets("Request Events")
serviceprovidersWorkbook.Activate reventsSheet.Select
Dim lastRow As Long lastRow = reventsSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1
reventsSheet.Range("B" & lastRow).Value = prmrequestsSheet.Range("F" & selectedRow.Row).Value reventsSheet.Range("C" & lastRow).Value = prmrequestsSheet.Range("D" & selectedRow.Row).Value reventsSheet.Range("D" & lastRow).Value = prmrequestsSheet.Range("E" & selectedRow.Row).Value reventsSheet.Range("F" & lastRow).Value = prmrequestsSheet.Range("M" & selectedRow.Row).Value
prmrequestsWorkbook.Activate prmrequestsWorkbook.Sheets("PrmRequests").Select
MsgBox "Request successfully sent to Request Events."
End Sub
- Assistant
Sub CopyAllPRtoEvents() Dim prmrequestsWorkbook As Workbook Set prmrequestsWorkbook = Workbooks("Premises Requests")
Dim serviceprovidersWorkbook As Workbook Set serviceprovidersWorkbook = Workbooks("Service Providers") Dim prmrequestsSheet As Worksheet Set prmrequestsSheet = prmrequestsWorkbook.Sheets("PrmRequests") Dim reventsSheet As Worksheet Set reventsSheet = serviceprovidersWorkbook.Sheets("Request Events") ' Clearing contents of Request Events sheet from row 2 downwards reventsSheet.Range("B2").Resize(reventsSheet.Cells(Rows.Count, "B").End(xlUp).Row - 1).ClearContents Dim lastRow As Long lastRow = prmrequestsSheet.Cells(Rows.Count, "B").End(xlUp).Row Dim i As Long For i = 2 To lastRow If prmrequestsSheet.Range("E" & i).Value <> "" Then reventsSheet.Range("B" & i).Value = prmrequestsSheet.Range("F" & i).Value reventsSheet.Range("C" & i).Value = prmrequestsSheet.Range("D" & i).Value reventsSheet.Range("D" & i).Value = prmrequestsSheet.Range("E" & i).Value reventsSheet.Range("F" & i).Value = prmrequestsSheet.Range("M" & i).Value End If Next i MsgBox "Requests successfully sent to Request Events."End Sub
- User
Can you please adjust the code so that when clearing contents of Request Events sheet from row 2 downwards, it only clears the values in column B,C,D, E & F from row 2 downwards
- Assistant
Sub CopyAllPRtoEvents() Dim prmrequestsWorkbook As Workbook Set prmrequestsWorkbook = Workbooks("Premises Requests")
Dim serviceprovidersWorkbook As Workbook Set serviceprovidersWorkbook = Workbooks("Service Providers") Dim prmrequestsSheet As Worksheet Set prmrequestsSheet = prmrequestsWorkbook.Sheets("PrmRequests") Dim reventsSheet As Worksheet Set reventsSheet = serviceprovidersWorkbook.Sheets("Request Events") ' Clearing contents of Request Events sheet from row 2 downwards in columns B to F reventsSheet.Range("B2:F" & reventsSheet.Cells(Rows.Count, "B").End(xlUp).Row).ClearContents Dim lastRow As Long lastRow = prmrequestsSheet.Cells(Rows.Count, "B").End(xlUp).Row Dim i As Long For i = 2 To lastRow If prmrequestsSheet.Range("E" & i).Value <> "" Then reventsSheet.Range("B" & i).Value = prmrequestsSheet.Range("F" & i).Value reventsSheet.Range("C" & i).Value = prmrequestsSheet.Range("D" & i).Value reventsSheet.Range("D" & i).Value = prmrequestsSheet.Range("E" & i).Value reventsSheet.Range("F" & i).Value = prmrequestsSheet.Range("M" & i).Value End If Next i MsgBox "Requests successfully sent to Request Events."End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.