I would like a VBA code that can do the following: In sheet 'PrmRequests' search column…
- User
I would like a VBA code that can do the following: In sheet 'PrmRequests' search column I for the following values 'In Progress' , 'On Order' , 'To Wait'. For each of the values found, copy the row from 'A to K' and paste to sheet 'PStatus'. The paste will start from Row 2.
- Assistant
Below is a VBA code that performs the requested task:
Sub CopyRowsToPStatus() Dim ws1 As Worksheet, ws2 As Worksheet Dim lastRow As Integer, destRow As Integer Dim cell As Range ' Set the source and destination worksheets Set ws1 = ThisWorkbook.Sheets("PrmRequests") Set ws2 = ThisWorkbook.Sheets("PStatus") ' Clear the existing data in the destination sheet ws2.Range("A2:K" & ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row).ClearContents ' Find the last row in the source sheet lastRow = ws1.Cells(ws1.Rows.Count, "I").End(xlUp).Row ' Set the initial destination row destRow = 2 ' Loop through each cell in column I of the source sheet For Each cell In ws1.Range("I1:I" & lastRow) If cell.Value = "In Progress" Or cell.Value = "On Order" Or cell.Value = "To Wait" Then ' Copy the row from column A to K and paste to the destination sheet ws1.Range("A" & cell.Row & ":K" & cell.Row).Copy ws2.Range("A" & destRow) ' Increase the destination row counter destRow = destRow + 1 End If Next cell End SubPlease make sure to adjust the sheet names ("PrmRequests" and "PStatus") as per your actual sheet names.
- User
The code worked well. Can you please adjust it so that it only copies across values only
- Assistant
Certainly! Here's the adjusted code that copies only the values:
Sub CopyValuesToPStatus() Dim ws1 As Worksheet, ws2 As Worksheet Dim lastRow As Integer, destRow As Integer Dim cell As Range ' Set the source and destination worksheets Set ws1 = ThisWorkbook.Sheets("PrmRequests") Set ws2 = ThisWorkbook.Sheets("PStatus") ' Clear the existing data in the destination sheet ws2.Range("A2:K" & ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row).ClearContents ' Find the last row in the source sheet lastRow = ws1.Cells(ws1.Rows.Count, "I").End(xlUp).Row ' Set the initial destination row destRow = 2 ' Loop through each cell in column I of the source sheet For Each cell In ws1.Range("I1:I" & lastRow) If cell.Value = "In Progress" Or cell.Value = "On Order" Or cell.Value = "To Wait" Then ' Copy the row from column A to K and paste values to the destination sheet ws1.Range("A" & cell.Row & ":K" & cell.Row).Copy ws2.Range("A" & destRow).PasteSpecial Paste:=xlPasteValues Application.CutCopyMode = False ' Increase the destination row counter destRow = destRow + 1 End If Next cell End Sub - User
I would like a VBA code that can do the following: My sheet 'PStatus' has a header row - Row 1. I would like to sort all the rows below by the values in column H
- Assistant
Sure! You can use the following VBA code to sort the rows below the header row based on the values in column H:
Sub SortRowsByColumnH() Dim ws As Worksheet Dim lastRow As Long ' Set the worksheet Set ws = ThisWorkbook.Sheets("PStatus") ' Find the last row in the sheet lastRow = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row ' Sort the rows by column H With ws.Sort .SortFields.Clear .SortFields.Add Key:=ws.Range("H2:H" & lastRow), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange ws.Range("A1:K" & lastRow) .Header = xlYes .Apply End With End SubMake sure to adjust the sheet name ("PStatus") as per your actual sheet name. This code sorts the rows from the range "A1:K" (with the actual last row in column K) with a header row in ascending order based on the values in column H.
- User
I would like a VBA code that can do the following: My sheet 'PStatus' has a header row - Row 1. If the Value in column E is equal to the value in column F of the same row, then pop up a Yes No message "Same Premises Status & Request Status found, Row should be deleted" If Yes Selected, then delete the row where condition was found nad move all the rows beneath up to fill the deleted row and continue with search process. If No selected, do not delete the row and continue with search process.
- Assistant
Certainly! You can use the following VBA code to prompt a Yes/No message and delete rows based on the condition you mentioned:
Sub DeleteRowsWithCondition() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim deleteRow As Boolean ' Set the worksheet Set ws = ThisWorkbook.Sheets("PStatus") ' Find the last row in the sheet lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Loop through each row from bottom to top For i = lastRow To 2 Step -1 ' Check if the value in column E is equal to the value in column F of the same row If ws.Cells(i, "E").Value = ws.Cells(i, "F").Value Then ' Prompt the Yes/No message deleteRow = MsgBox("Same Premises Status & Request Status found, Row should be deleted. Do you want to proceed?", vbYesNo) = vbYes If deleteRow Then ' Delete the row and move the rows beneath up to fill the deleted row ws.Rows(i).Delete Shift:=xlUp End If End If Next i End Sub - User
After the message, the code goes into a continous loop
- Assistant
Apologies for the confusion. To prevent the code from going into a continuous loop, you need to exit the loop once the row is deleted. Below is the updated code with the inclusion of the
Exit Forstatement:Sub DeleteRowsWithCondition() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim deleteRow As Boolean ' Set the worksheet Set ws = ThisWorkbook.Sheets("PStatus") ' Find the last row in the sheet lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Loop through each row from bottom to top For i = lastRow To 2 Step -1 ' Check if the value in column E is equal to the value in column F of the same row If ws.Cells(i, "E").Value2 = ws.Cells(i, "F").Value2 Then ' Prompt the Yes/No message deleteRow = MsgBox("Same Premises Status & Request Status found, Row should be deleted. Do you want to proceed?", vbYesNo) = vbYes If deleteRow Then ' Delete the row and move the rows beneath up to fill the deleted row ws.Rows(i).Delete Shift:=xlUp ' Exit the loop after deleting the row Exit For End If End If Next i End Sub - User
Is it possible to to exit the loop after all the rows that meet the condition have have been deleted and if No is selected, then the row is omitted from the condition during the loop.
- Assistant
Apologies for the misunderstanding. To exit the loop after all the rows that meet the condition have been deleted, and to skip the row if 'No' is selected, you can modify the code as follows:
Sub DeleteRowsWithCondition() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim deleteRow As Boolean ' Set the worksheet Set ws = ThisWorkbook.Sheets("PStatus") ' Find the last row in the sheet lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Loop through each row from bottom to top For i = lastRow To 2 Step -1 ' Check if the value in column E is equal to the value in column F of the same row If ws.Cells(i, "E").Value2 = ws.Cells(i, "F").Value2 Then ' Prompt the Yes/No message deleteRow = MsgBox("Same Premises Status & Request Status found, Row should be deleted. Do you want to proceed?", vbYesNo) = vbYes If deleteRow Then ' Delete the row and move the rows beneath up to fill the deleted row ws.Rows(i).Delete Shift:=xlUp End If End If Next i End SubWith this modification, the loop will continue until all the rows meeting the condition have been deleted. If 'No' is selected, the row will be skipped, and the loop will move on to the next row.
- User
It is still going into a loop after the message, and not deleting or skipping the row
- Assistant
Apologies for the confusion. It seems the loop is not working as intended. To achieve your desired outcome accurately, we will use a Do Until loop instead. Here's the updated code:
Sub DeleteRowsWithCondition() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim deleteRow As Boolean ' Set the worksheet Set ws = ThisWorkbook.Sheets("PStatus") ' Find the last row in the sheet lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Loop through each row from bottom to top i = 2 Do Until i > lastRow ' Check if the value in column E is equal to the value in column F of the same row If ws.Cells(i, "E").Value = ws.Cells(i, "F").Value Then ' Prompt the Yes/No message deleteRow = MsgBox("Same Premises Status & Request Status found, Row should be deleted. Do you want to proceed?", vbYesNo) = vbYes If deleteRow Then ' Delete the row and move the rows beneath up to fill the deleted row ws.Rows(i).Delete Shift:=xlUp lastRow = lastRow - 1 ' Update the last row after deletion Else i = i + 1 ' Increment i without deleting the row End If Else i = i + 1 ' Increment i if the condition is not met End If Loop End Sub - User
The code now deletes but after all the deletions have been made it does not exit the loop
- Assistant
Apologies for the issue. To resolve this and correctly exit the loop once all the deletions have been made, you can modify the code to determine when to exit the loop. Here's the updated code:
Sub DeleteRowsWithCondition() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim deleteRow As Boolean Dim rowsDeleted As Boolean ' Set the worksheet Set ws = ThisWorkbook.Sheets("PStatus") ' Find the last row in the sheet lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Initialize rowsDeleted flag rowsDeleted = False ' Loop through each row from bottom to top i = 2 Do Until i > lastRow ' Check if the value in column E is equal to the value in column F of the same row If ws.Cells(i, "E").Value = ws.Cells(i, "F").Value Then ' Prompt the Yes/No message deleteRow = MsgBox("Same Premises Status & Request Status found, Row should be deleted. Do you want to proceed?", vbYesNo) = vbYes If deleteRow Then ' Delete the row and move the rows beneath up to fill the deleted row ws.Rows(i).Delete Shift:=xlUp lastRow = lastRow - 1 ' Update the last row after deletion rowsDeleted = True Else i = i + 1 ' Increment i without deleting the row End If Else i = i + 1 ' Increment i if the condition is not met End If Loop ' Exit the loop if no rows were deleted If Not rowsDeleted Then Exit Sub ' Continue deleting rows until no more rows meet the condition Call DeleteRowsWithCondition End Sub - User
I would like a VBA code that can do the following: When I activate sheet 'PSatusPrint' The following columns from sheet PStatus C, D, F, E, H, K are copied to the following columns in sheet PStatusPrint A, B, C, D, E, F, starting from row 2 and stopping when there are no values in column G of sheet PStatus
- Assistant
Certainly! Here's the VBA code that copies the specified columns from "PStatus" to "PStatusPrint" starting from row 2 and stopping when there are no values in column G of "PStatus":
Sub CopyToPStatusPrint() Dim wsSrc As Worksheet, wsDest As Worksheet Dim lastRowSrc As Long, destRow As Long Dim i As Long ' Set the source and destination worksheets Set wsSrc = ThisWorkbook.Sheets("PStatus") Set wsDest = ThisWorkbook.Sheets("PStatusPrint") ' Find the last row in the source sheet lastRowSrc = wsSrc.Cells(wsSrc.Rows.Count, "G").End(xlUp).Row ' Clear the existing data in the destination sheet from row 2 onwards wsDest.Range("A2:F" & wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row).ClearContents ' Set the initial destination row destRow = 2 ' Loop through each row in the source sheet until there are no values in column G For i = 2 To lastRowSrc If wsSrc.Cells(i, "G").Value = "" Then Exit For ' Copy the columns C, D, F, E, H, K to A, B, C, D, E, F in the destination sheet wsSrc.Range("C" & i & ":D" & i & ", F" & i & ":H" & i & ", K" & i).Copy wsDest.Range("A" & destRow) ' Increment the destination row counter destRow = destRow + 1 Next i End Sub - User
wsSrc.Range("C" & i & ":D" & i & ", E" & i & ":F" & i & ", H" & i & ":K" & i).Copy wsDest.Range("A" & destRow) . this line of code is pasting an unwanted extra column between H and K
- Assistant
Apologies for the mistake in the code. To correct it and eliminate the extra column, please use the following code instead:
Sub CopyToPStatusPrint() Dim wsSrc As Worksheet, wsDest As Worksheet Dim lastRowSrc As Long, destRow As Long Dim i As Long ' Set the source and destination worksheets Set wsSrc = ThisWorkbook.Sheets("PStatus") Set wsDest = ThisWorkbook.Sheets("PStatusPrint") ' Find the last row in the source sheet lastRowSrc = wsSrc.Cells(wsSrc.Rows.Count, "G").End(xlUp).Row ' Clear the existing data in the destination sheet from row 2 onwards wsDest.Range("A2:F" & wsDest.Cells(wsDest.Rows.Count, "A").End