Delete row based on one letter - excel

Need help with my code.
In (Column C) I have values MG01, MG02a, MG02b, MG02c. And in (Column A) different values. Code needs to delete row if value in column A is "1" and
in Column C if it finds letters at the end of text such as b, c, d, e, ....
And with "c" code do not recognized MG02c help please.
Sub xDeleteRowz()
Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "A").Value) = "1" And (Cells(i, "C").Value) = "*c*" Then
Cells(i, "A").EntireRow.Delete
End If
Next i
End Sub

This sounds like something more suited for a regular expression:
Sub xDeleteRowz()
Last = Cells(Rows.Count, "A").End(xlUp).Row
With CreateObject("VBScript.RegExp")
.Pattern = "MG\d{2}[a-z]"
.IgnoreCase = False
For i = Last To 1 Step -1
If (Cells(i, "A").Value) = "1" And .Test(Cells(i, "C").Value) Then
Cells(i, "A").EntireRow.Delete
End If
Next i
End With
End Sub
Note that the expression requires the value to begin with MG##. If the start of the value can be different, replace this line...
.Pattern = "MG\d{2}[a-z]"
...with...
.Pattern = ".+[a-z]"
... and it will match anything with a lowercase letter at the end. You can also limit to specific letters by changing the range inside the brackets. I.e., if it's only 'a' through 'g', it would be:
.Pattern = ".+[a-g]"

Change
If (Cells(i, "A").Value) = "1" And (Cells(i, "C").Value) = "*c*" Then
to
If (Cells(i, "A").Value) = "1" And (Cells(i, "C").Value) like "*c*" Then
or
If (Cells(i, "A").Value) = "1" And InStr(1, Cells(i, "C").Value, "c", vbTextCompare) Then
May I also suggest that you make some more changes to your code and start coding explicitly like so:
Option Explicit
Sub xDeleteRowz()
Dim i As Long, Last As Long
Last = Cells(Rows.Count, "A").End(xlUp).Row
With ThisWorkbook.Worksheets(1)
For i = Last To 1 Step -1
If .Cells(i, "A").Value = "1" And InStr(1, .Cells(i, "C").Value, "c", vbTextCompare) Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub
Update:
Based on the latest comment a more suitable approach might be the following:
Option Explicit
Sub xDeleteRowz()
Dim i As Long, Last As Long
Last = Cells(Rows.Count, "A").End(xlUp).Row
With ThisWorkbook.Worksheets(1)
For i = Last To 1 Step -1
If .Cells(i, "A").Value = "1" Then
Select Case LCase(Right(.Cells(i, "C").Value, 1))
Case "b", "c", "d", "e", "f", "g"
.Rows(i).EntireRow.Delete
End Select
End If
Next i
End With
End Sub

Related

VBA: statement in if Then loop fails

I have a sheet with Columns A to P.
In columns B i have customer names. Want to find rows with substring “ABC -“ and copy the content of the cell in column B to Column G on the same row.
My code fails on this:
For I= 1 to finalrow
If Left(Cells(I,2).Value,5) = “ABC -“ Then
Rownumber= ActiveCell.Row
Range("B" & Rownumber).Select
Range("B" & Rownumber).Copy
Range("G" & rownumber).Select
ActiveSheet.Paste
Range("G" & rownumber).Select
End if
Next I
This one works as expected, writing the values from column "B" to column "G":
Sub TestMe()
Dim i As Long
For i = 1 To 10
With ThisWorkbook.Worksheets("Sheet1")
Dim myCell As Range
Set myCell = .Cells(i, "B")
If Trim(Left(myCell.Value, 5)) = "ABC -" Then
.Cells(i, "G").Value = myCell.Value
End If
End With
Next i
End Sub
Try to avoid .Select and .Activate - https://stackoverflow.com/a/35864330/5448626
Use Trim()
Using . and referring the worksheet is always a good practice
.Cells(i, "B") improves readability
“ probably should be "
For I = 1 To finalrow
With Cells(I, 2)
If .Text Like "ABC -*" Then .Offset(0, 5) = .Value
End With
Next I
For I = 1 to finalrow
If Left(Cells(I,2).Value,5) = "ABC -" Then
Cells(I,7).Value = Cells(I,2).Value
End if
Next I

Why does this vba sentence go wrong? If Cells(i, 1).Value = "M" Then

Sub a()
Dim i As Integer
For i = 1 To 8
If Cells(i, 1).Value = "M" Then
Cells(i, 2).Value = ""
End If
Next i
End Sub
You need to include the sheet information along with the cells.
Example: Sheet1.Cells(i,1).Value or Worksheets("Sheet name").Cells(i,1).Value

Search for a text in column B and past other text to column L if true

I need to search in column B:B for a specific text, then if true paste other text to column L:L, exemple:
Sub teste()
Application.ScreenUpdating = False
last = Cells(Rows.Count, "B").End(xlUp).Row
For i = last To 1 Step -1
If (Cells(i, "B").Value) = "string_1" Then
Range("L2").Select
ActiveCell.FormulaR1C1 = "some_text_1"
'LastRow = Range("A" & Rows.Count).End(xlUp).Row
'Range("L2").AutoFill Destination:=Range("L2:L" & LastRow)
End If
Next i
End Sub
I can only paste the first text if true or fill the column L:L with the same text.
You mean something like that?
If column B is string_1 then copy column C to column L
For i = last To 1 Step -1
If (Cells(i, "B").Value) = "string_1" Then
'copy value from C to L
Cells(i, "L").Value = Cells(i, "C").Value
End If
Next i
You might benefit from reading
How to avoid using Select in Excel VBA.
Sub teste()
Application.ScreenUpdating = False
s1 = "first_text"
s2 = "second_text"
s3 = "third_text"
last = Cells(Rows.Count, "B").End(xlUp).Row
For i = last To 1 Step -1
If (Cells(i, "B").Value) = "string_1" Then
Cells(i, "L").Value = s1
ElseIf (Cells(i, "B").Value) = "String_2" Then
Cells(i, "L").Value = s2
ElseIf (Cells(i, "B").Value) = "string_3" Then
Cells(i, "L").Value = s3
End If
Next i
End Sub

read Cellvalue through a range, and write to a different range

I am trying to make a VBA scrip that check all cells between B2 and B60 for the text "Ja" that's yes in Norwegian.
How can I make this a little bit simpler that making a "if" command for each cell?
I want it to, if the cell contains "ja"(yes) then write to colum D and the same number.
ie. B1,2,3,4,5 cotains "ja", I need it to take the previous cell value in D1.2,3,4,5 and add another digit to it +1.
If nothing is found in B(ie.false) it needs to write "NEI" in the current cell, and if "NEI" (no) is found in that cell it adds +1 to colum E
Sub Macro2()
Dim celltxt As String
Dim a As Variant
If IsEmpty(Range("B2").Value) = True Then
Cells(2, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B2").Text
If InStr(1, celltxt, "ja") Then
a = Cells(2, 1).Value
'write to cell
Cells(2, 4).Value = Cells(2, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(2, 5).Value = Cells(2, 5) + 1
End If
If IsEmpty(Range("B3").Value) = True Then
Cells(3, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B3").Text
If InStr(1, celltxt, "ja") Then
a = Cells(3, 1).Value
'write to cell
Cells(3, 4).Value = Cells(3, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(3, 5).Value = Cells(3, 5) + 1
End If
End Sub
Sub slettingALL()
Range("D2:E55").Select
Selection.ClearContents
End Sub
Sub slettingdeltakelse()
Range("B2:B60").Select
Selection.ClearContents
End Sub
The following code uses a For Each loop and an IF THEN ELSE statement to check for the value "JA" in the range B2:B60.
If it finds "JA", it looks two columns to the right from the current i location, and adds "+1" to the value above it. If it finds nothing, it writes "NEI" to the current i location, and then moves three columns to the right and adds +1 to the value above it.
Sub Macro2()
For Each i In Range(Cells(2, 2), Cells(60, 2))
If i.Value = "JA" Then
i.Offset(0, 2).Value = i.Offset(-1, 2).Value + 1
Else
i.Value = "NEI"
i.Offset(0, 3).Value = i.Offset(-1, 3).Value + 1
End If
Next i
End Sub
Please let me know if this code does not work for your purpose.

For each cell in column range table

I need help fixing my code and adding in the cell ranges.
I am trying to change the values in the cells to being the correct values if they are spelt incorrectly. But the table will be added to so I need to make it a flexible code. The code currently stops at the beginning sub with error code 424. I am fairly new to VBA and am stuck.
Sub Consolidates()
Dim datasheet As Worksheet
Set datasheet = ThisWorkbook.Sheets("sheet1")
lr = datasheet.Cells(Rows.Count, 9).End(xlUp).Row
For x = 2 To lr
If cell.Value = "B" Or "BR" Or " Then
cell.Value = "BR"
ElseIf cell.Value = "CL" Or "CR" _
Then cell.Value = "CR"
ElseIf cell.Value = "" Then
End If
Next x
End Sub
you could use something like follows
Option Explicit
Sub Consolidates()
Dim stringsToSearch As String, stringToSubstitute As String
Dim stringsToSearchArr As Variant, stringToSubstituteArr As Variant
' here define the "table"
stringsToSearch = "B,CL" '<--| type here the strings to be searched for
stringToSubstitute = "BR,CR" '<--| type here the corresponding strings to change searched ones into
stringsToSearchArr = Split(stringsToSearch, ",") '<--| turn "stringsToSearch" into an array
stringToSubstituteArr = Split(stringToSubstitute, ",") '<--| turn "stringToSubstitute" into an array
With ThisWorkbook.Sheets("sheetTest") '<--| change "sheetTest" with your actual sheet name
With .Range("I2:I" & .Cells(.Rows.Count, 9).End(xlUp).Row) '<--| consider all cells in column "I" from row 2 to last non empty one
For i = LBound(stringsToSearchArr) To UBound(stringsToSearchArr) '<--| loop through the "table"
.Replace What:=stringsToSearchArr(i), Replacement:=stringToSubstituteArr(i), LookAt:=xlWhole, MatchCase:=True '<--| find current string and replace it with its corresponding replacement
Next i
End With
End With
End Sub
Cell needs a reference to which cell. Also you can't use the or statement like that. Below a simple way to get it done.
For x = 1 To lr
If Cells(x, 9).Value = "B" Or Cells(x, 9).Value = "BR" Then
Cells(x, 9).Value = "BR"
ElseIf Cells(x, 9).Value = "CL" Or Cells(x, 9).Value = "CR" Then
Cells(x, 9).Value = "CR"
End If
Next x
You should consider a select statement
For x = 1 To lr
Select Case Cells(x, 9).Value
Case "B", "BR"
Cells(x, 9).Value = "BR"
Case "CL", "CR"
Cells(x, 9).Value = "CR"
End Select
Next x
Since it is case sensitive you could add an Lcase which could save you some time
For x = 1 To lr
Select Case LCase(Cells(x, 9).Value)
Case "b", "br"
Cells(x, 9).Value = "BR"
Case "cl", "cr"
Cells(x, 9).Value = "CR"
End Select
Next x

Resources