In the VBA event below, if the value 'x' is found in column B, the row values in A:B are…
- User
In the VBA event below, if the value 'x' is found in column B, the row values in A:B are deleted and the row below is moved up. Can you please amend the code so that after the deletion the row below is NOT moved up: Sub DeleteRowsWithXValue() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Notes") Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row Dim i As Long i = 1 Dim answer As VbMsgBoxResult answer = MsgBox("Do you want to continue?", vbQuestion + vbYesNo, "Confirmation") If answer = vbYes Then Do While i <= lastRow If ws.Range("B" & i).Value = "x" Then ws.Range("A" & i & ":B" & i).Delete Shift:=xlUp lastRow = lastRow - 1 Else i = i + 1 End If Loop Else Exit Sub End If End Sub
- Assistant
Sub DeleteRowsWithXValue() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Notes") Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row Dim i As Long i = 1 Dim answer As VbMsgBoxResult answer = MsgBox("Do you want to continue?", vbQuestion + vbYesNo, "Confirmation") If answer = vbYes Then Do While i <= lastRow If ws.Range("B" & i).Value = "x" Then ws.Rows(i).Delete lastRow = lastRow - 1 Else i = i + 1 End If Loop Else Exit Sub End If End Sub
- User
Will this aork: Sub DeleteRowsWithXValue() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets(“Notes”) Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, “B”).End(xlUp).Row Dim i As Long i = 1 Dim answer As VbMsgBoxResult answer = MsgBox(“Do you want to continue?”, vbQuestion + vbYesNo, “Confirmation”) If answer = vbYes Then Do While i <= lastRow If ws.Range(“B” & i).Value = “x” Then ws.Range(“A” & i & “:B” & i).Delete lastRow = lastRow - 1 Else i = i + 1 End If Loop Else Exit Sub End If End Sub
- Assistant
There seems to be an issue with the quotation marks used in your code. VBA requires straight quotation marks (" "), but the code you provided contains curly quotation marks (“ ”). To fix the issue, replace all the curly quotation marks with straight quotation marks in your code. Here's the corrected version:
Sub DeleteRowsWithXValue() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Notes") Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row Dim i As Long i = 1 Dim answer As VbMsgBoxResult answer = MsgBox("Do you want to continue?", vbQuestion + vbYesNo, "Confirmation") If answer = vbYes Then Do While i <= lastRow If ws.Range("B" & i).Value = "x" Then ws.Range("A" & i & ":B" & i).Delete lastRow = lastRow - 1 Else i = i + 1 End If Loop Else Exit Sub End If End Sub
- User
In the VBA event below, I want to cahnge the 'destinationRange' to A2:A17. Can you please do this for me.
Sub CopyToNotes() Application.ScreenUpdating = False Dim sourceRange As Range Set sourceRange = Sheets("Today").Range("B1:C21") Dim destinationSheet As Worksheet Dim destinationRange As Range Set destinationSheet = Sheets("Notes") Set destinationRange = destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Offset(1, 0) sourceRange.Copy destinationRange.PasteSpecial xlPasteValues Application.CutCopyMode = False Application.ScreenUpdating = True Call StartC End Sub
- Assistant
Sure! Here's the modified code with the destinationRange changed to A2:A17:
Sub CopyToNotes() Application.ScreenUpdating = False Dim sourceRange As Range Set sourceRange = Sheets("Today").Range("B1:C21") Dim destinationSheet As Worksheet Dim destinationRange As Range Set destinationSheet = Sheets("Notes") Set destinationRange = destinationSheet.Range("A2:A17") sourceRange.Copy destinationRange.PasteSpecial xlPasteValues Application.CutCopyMode = False Application.ScreenUpdating = True Call StartC End Sub