VBA Script to Delete Column Values based on other Column Values - excel

I'm looking for a VBA code to as the title specifies, delete data based on conditions
So I have Column A and Column B, Rows starts from 2 until the end of the sheet, so as an example If the value in B2 is "OK", I would like for the value in A2 to be cleared and then loop the same process until the end of both columns, this is what I have so far but it's not working properly:
Sub Clear()
Dim myLastRow As Long
Dim i As Long
Application.ScreenUpdating = False
Find last row
myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
Loop through range
For i = 2 To myLastRow
If Cells(i, "B").Value = "OK" Then Range(Cells(i, "A")).ClearContents
Next i
Application.ScreenUpdating = True
End Sub

Quick fix for your code is to remove Range
Sub Clear()
Dim myLastRow As Long
Dim i As Long
Application.ScreenUpdating = False
'Find last row
myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
' Loop through range
For i = 2 To myLastRow
' If Cells(i, "B").Value = "OK" Then Range(Cells(i, "A")).ClearContents
If Cells(i, "B").Value = "OK" Then Cells(i, "A").ClearContents
Next i
Application.ScreenUpdating = True
End Sub
Pay attention as Cells refers to the active sheet. In case you would like to run the code on a specific sheet you should better specifiy the sheet.

Related

How to add row and copy formula from a cell and paste into a cell of the new row?

I made a button that adds a new row above another row where the value of the cell in column C is "add row above".
I did it like this because there is a formula on the row below that which totals all of column E.
So when I add a row above C with value add row above it auto updates the formula.
I need to copy a formula from column B into each now. The formula is =ROW(A1) so it numbers the row.
My code to add the new row:
Sub AddRow()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
For i = Lastrow To 1 Step -1
If Cells(i, "C").Value = "Add row above" Then If i > 1 Then Rows(i).Resize(1).Insert xlUp
Next
Application.ScreenUpdating = True
End Sub
Insert Row and Copy Formula
Note that =ROW(A1), =ROW(Z1) or just =ROW() produces the same result.
Option Explicit
Sub AddRow()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
For i = Lastrow To 2 Step -1
If Cells(i, "C").Value = "Add row above" Then
Rows(i).Insert xlShiftDown
Cells(i + 1, "B").Copy Cells(i, "B")
' Or (if below is not numbered):
'Cells(i - 1, "B").Copy Cells(i, "B")
End If
Next
Application.ScreenUpdating = True
End Sub
I find it easier to create a named range and refer to that. This way, if it moves around the sheet, the named range will follow it and you don't have to go looking.
When you do that, this code works quite easily, you just need to adapt it.
Also, the ROW() function doesn't actually need a parameter IF you want to refer to the row that the ROW() formula is on.
Public Sub AddRowAndCopyFormula()
Dim lngAddAtRow As Long
With ThisWorkbook.Names("AddRowAbove")
lngAddAtRow = .RefersToRange.Cells(1, 1).Row
.RefersToRange.Worksheet.Rows(lngAddAtRow).Insert xlShiftDown
.RefersToRange.Worksheet.Range("B" & lngAddAtRow).Formula = "=ROW() - 7"
End With
End Sub
This is what my worksheet looks like.

Delete rows with multiple criteria in VBA

my goal is to delete rows with column 3 with the cell value that has inventory (>0) and column 4 that has the cell value TRUE in the current sheet. I tried to use the code to this website and I'm pretty sure I did something wrong where it says ActiveSheet.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Public Sub FilterStock()
ActiveSheet.Range("A1").AutoFilter Field:=4, Criteria1:="TRUE"
ActiveSheet.Range("A1").AutoFilter Field:=3, Criteria1:=">0"
Application.DisplayAlerts = False
ActiveSheet.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
ActiveSheet.AutoFilter.ShowAllData
End Sub
This code worked for me:
Sub DeletelRows()
Dim lastRow As Long
Dim debug1 As Variant
Dim debug2 As Variant
'Find the last non-blank cell in column C
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
For x = lastRow To 2 Step -1 'Start at bottom and go up to avoid complications when the row is deleted.
debug1 = Cells(x, 3).Value 'You can set breakpoints to see what the values are.
debug2 = Cells(x, 4).Value
If (Cells(x, 3).Value > 0 And UCase(Cells(x, 4).Value) = "TRUE") Then
Rows(x).Delete
End If
Next x
End Sub

How to traverse to each Column in the same Row in Excel VBA

I'm trying to traverse each column in the same row, I'm new to VBA and any help would be appreciated..
Here's my code:
Sub dural()
Dim i As Long
Dim j As Long
i = 2
Cells(1, i).Select
For i = 2 To Columns.Count
Cells(1, i + j).Select
'Selection.Copy
j = i + 1
Next i
End Sub
Luan Yi,
Your question states "trying to traverse each column in the same row";
the code below shows how to loop through each Column, or loop through
each cell in Row 1 and use .EntireColumn to do something; without using .Select
'Use the With statement that meets your needs
'define your variables
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") 'change worksheet name as needed
Dim lCol As Long: lCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column 'define last used column in row 1
For i = 2 To lCol
'If you want to loop through each column, you can use...
With ws.Columns(i)
'you can do something, for example using .Delete or .Copy, etc.
End With
'or
'If you want to loop through each cell in row 1, the you can use...
With ws.Cells(1, i)
'you can do something, for example using .EntireColumn.Delete or .EntireColumn.Copy, etc.
End With
Next i

How to copy columns from one worksheet to another on excel with VBA?

I am trying to copy certain column from one worksheet to another but when I apply my code, I get no errors but also no results. I get blank paper. I applied this methodolgy on copying a certain row and it was copied to another worksheet perfectly.
This is regarding the successful attempt to copy row.
The code works just fine:
Sub skdks()
Dim OSheet As Variant
Dim NSheet As Variant
Dim i As Integer
Dim LRow As Integer
Dim NSLRow As Integer
OSheet = "Tabelle3" 'Old Sheet Name
NSheet = "Tabelle5" 'New Sheet Name
LRow = Sheets(OSheet).Cells(Rows.Count, 1).End(xlUp).row 'Last Row in Old Sheet
Sheets(OSheet).Activate
For i = 2 To LRow
'Finds last row in the New Sheet
If Sheets(NSheet).Cells(2, 1) = "" Then
NSLRow = 1
Else
NSLRow = Sheets(NSheet).Cells(Rows.Count, 1).End(xlUp).row
End If
'If cell has "certain # then..."
If Cells(i, 1).Value = Cells(13, 2).Value Then
Cells(i, 1).EntireRow.Copy
Sheets(NSheet).Cells(NSLRow + 1, 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If
Next i
End Sub
This little piece of code is the failed attempt to copy column to another worksheet.
Sub trial()
Dim OSheet As Variant
Dim NSheet As Variant
Dim j As Integer
Dim LColumn As Integer
Dim NSLColumn As Integer
OSheet = "Tabelle2" 'Old Sheet Name
NSheet = "Tabelle5" 'New Sheet Name
LColumn = Sheets(OSheet).Cells(1, Columns.Count).End(xlToLeft).Column 'Last Column in Old Sheet
Sheets(OSheet).Activate
For j = 2 To LColumn
'Finds last column in the New Sheet
If Sheets(NSheet).Cells(1, 2) = "" Then
NSLColumn = 1
Else
NSLColumn = Sheets(NSheet).Cells(1, Columns.Count).End(xlToLeft).Column
End If
'If cell has "certain # then..."
If Cells(2, j) = Cells(13, 2) Then
Cells(2, j).EntireColumn.Copy
Sheets(NSheet).Cells(2, 2).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If
Next j
End Sub
....
'If cell has "certain # then..."
If Cells(2, j) = Cells(13, 2) Then
debug.Print Cells(2, j).Address; " = "; Cells(13, 2).Address; " ---- COPY"
debug.print Cells(2, j).EntireColumn.address; Cells(2, j).EntireColumn.cells.count
debug.Print Sheets(NSheet).Cells(2, 2).Address
Cells(2, j).EntireColumn.Copy
Sheets(NSheet).Cells(2, 2).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If
....
With the line If Cells(2, j) = Cells(13, 2) Then you compare the different cells from row 2 (B2, C2, D2, ...) with the value of cell "B13". If the value is the same you copy this column to the new worksheet.
Is there any equal value in your data? If yes you should get an error message with your code.
You try to copy the values of an entire column to the range starting with "B2". Of cause there is not enough space for this.
=> Either you reduce the source range or you start the destination range on row 1!
To add to the paste destination size, if you really want to paste the entire column, you either need to start at the beginning of the column or choose the entire column. Also, I think you want to make the paste column increase with your NSLColumn
If Cells(2, j) = Cells(13, 2) Then
Cells(2, j).EntireColumn.Copy
Sheets(NSheet).Columns(NSLColumn + 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If

VBA code for clearing last cell and formulas from last cell

I want to clear the whole cell including formulas from the last blank cell. Here is what i'm trying to work with but its not working. Would other code affect it? Thanks
Dim myLastRow As Long
Dim clearCell As Long
Application.ScreenUpdating = False
' Find last row
myLastRow = Cells(Rows.Count, "C").End(xlUp).Row
' Loop through range
For clearCell = 4 To myLastRow
If Cells(clearCell, "C").Value = "" Then Range(Cells(clearCell, "C"), Cells(clearCell, "C")).Clear
Next clearCell
Application.ScreenUpdating = True
I don't see anything that would cause an error in your code, other than the fact that your references aren't qualified with the sheet that is being used - which means everything will default to operating on ActiveSheet which may not be the sheet you are wanting it to work on.
Assuming the sheet you want to process has a Name of "Stow", the following code should be safer:
Dim myLastRow As Long
Dim clearCell As Long
Application.ScreenUpdating = False
'Use a "With" block to save typing Worksheets("Stow") in lots of places
'(e.g. within the block we can type ".Cells" instead of "Worksheets("Stow").Cells" and
' ".Rows" instead of "Worksheets("Stow").Rows")
With Worksheets("Stow")
' Find last row
myLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
' Loop through range
For clearCell = 4 To myLastRow
''Clear any cells with a value of ""
'If .Cells(clearCell, "C").Value = "" Then .Cells(clearCell, "C").Clear
'Clear any cells with a value of "-"
If .Cells(clearCell, "C").Value = "-" Then .Cells(clearCell, "C").Clear
Next clearCell
End With
Application.ScreenUpdating = True

Resources