IF Condition for Two Different Scenarios

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello Guys, I have two scenarios need to be solve with IF conditions. In my sheet1 I column cells i have a string which consists of alphanumeric and symbols. I am in the process of cleaning up the strings. I cant fix these 2 cases. Please heads up and help.

1st case: If any cell in I column has "A" as second word, the space between 1st and 2nd word should be deleted. Example: 70 A to 70A) Note: Second word is "A" letter alone not words starts with A.

2nd case: If any of the cell in in I column starts with "NAME" the first word below to that cell should be deleted.

I need 2 separate macros for the 2 cases. Thank you.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
In your first scenario, do you have instances where there would be data followed by a space, followed by an A, that you don't want to delete the space?

For your second scenario, you don't actually need VBA. You can use a formula:

NAME
PurpleDelete=IF(A1="NAME","Delete","")
Dog
Cat
NAME
FishDelete

<tbody>
</tbody>
 
Upvote 0
This code will deal with your first condition, your second requirement is unclear what does:
the first word below to that cell should be deleted.
mean??

code for first requirement
Code:
Sub test()

Dim txt As String


lastrow = Cells(Rows.Count, "I").End(xlUp).Row
inarr = Range(Cells(1, 9), Cells(lastrow, 9))
 For i = 1 To lastrow
  txt = inarr(i, 1)
  ' find the first space
  sp = InStr(txt, (" "))
  If sp > 0 Then
  If Mid(txt, sp + 1, 1) = "A" Then
   inarr(i, 1) = Left(txt, sp - 1) & Mid(txt, sp + 1)
  End If
  End If
Next i
Range(Cells(1, 9), Cells(lastrow, 9)) = inarr


   
End Sub
 
Upvote 0
Hello Offthelip, the code is working great. Thank you. Please check the below table for my 2nd case. If any of the cell in I column starts with NAME the first word in the cell below to that should get deleted. Hope you understand. Please help. Thank you.



I
I
NAME AGE
NAME AGE
Mr John
John
Test
Test
NAME DOB
NAME DOB
AA Sara
Sara
Tst
Tst
NAME MARK
NAME MARK
Adam Smith
Smith

<tbody>
</tbody>
 
Upvote 0
Hello Reberry, thanks for your formula. Since i am running batch of macros in the process. So i required in macros.
 
Upvote 0
Check if this is what you want:

Code:
Sub madhuchelliah()


Dim lastrow As Integer, i As Integer
Dim MyRng As Range
Dim Rng As Range
Dim ChkVal As Variant
Dim text As String


lastrow = ActiveSheet.Cells(Rows.Count, 9).End(xlUp).Row
Set MyRng = ActiveSheet.Range("I1:I" & lastrow)


For Each Rng In MyRng
    
    ChkVal = Split(Rng.Value, " ")
    If UBound(ChkVal) > 0 Then
        If ChkVal(1) = "A" Then
            text = ChkVal(0) & ChkVal(1)
            Rng.Value = text & Replace(Rng.Value, ChkVal(0) & " " & ChkVal(1), "")
         End If
    End If
        
    If Rng.Row <> 1 Then


        If InStr(LCase(Rng.Offset(-1, 0).Value), "name") <> 0 Then
            Rng.Value = Mid(Rng.Value, InStr(Rng.Value, " ") + 1, Len(Rng.Value))
        End If
    
    End If


Next Rng
   
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top