Vba loop only when cells are not blank

Heather515

New Member
Joined
Sep 11, 2021
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Data.xlsx
D
6
Sheet1


I need help with making my code more efficient. As you can see, I want to code "Apple" as "1", and everything else as "0". However, I want to leave other cells blank when the cells in the first column is also blank. My current code first return 1 for Apple and 0 for others (including blank cells), and then I run the loop to remove 0 generated from the first step. It takes too long to remove the unwanted zeros since I have hundreds of thousands rows. I also need to keep the blank rows for merging data later.

Could you please help change my code or write a new code so cells with "Apple" return value of "1" and only other non-blank cells return value of "0" so I don't need to spend extra time to remove the unwanted zeros. Thanks!


Sub Apple()

Dim LRow As Long

LRow = Cells(Rows.Count, 1).End(xlUp).Row

For x = 2 To LRow

If InStr(Cells(x, 1), "Apple") > 0 Then
Cells(x, 2) = "1"
Else
Cells(x, 2) = "0"
End If
Next x

For x = 2 To LRow

If IsEmpty(Cells(x, 1)) Then

Cells(x, 2) = ""

End If

Next x


End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Give this non-looping macro a try...
VBA Code:
Sub Apples()
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    .SpecialCells(xlConstants).Offset(, 1) = 0
    .Offset(, 1) = Evaluate(Replace("IF(@="""","""",0+(@=""Apple""))", "@", .Address))
  End With
End Sub
 
Upvote 0
Slight variation from Rick's method...

VBA Code:
Sub Heather515()
    With ActiveSheet
        i = .Cells(.Rows.Count, "A").End(xlUp).Row
        x = "=if(A2:A" & i & "= ""apple"",1,if(A2:A" & i & "="""","""",0))"
        .Range("B2:B" & i).Value = Application.Evaluate(x)
    End With
End Sub
 
Upvote 0
Give this non-looping macro a try...
VBA Code:
Sub Apples()
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    .SpecialCells(xlConstants).Offset(, 1) = 0
    .Offset(, 1) = Evaluate(Replace("IF(@="""","""",0+(@=""Apple""))", "@", .Address))
  End With
End Sub
Data.xlsx
A
23NotenoteFishNoenote
Sheet1


Thanks. It works on the example data I uploaded. However, I have problems when I run the code in new data that are slightly different from the data I uploaded. I uploaded the second worksheet I'm working. How should I adjust the code? Thanks again!
 
Upvote 0
Slight variation from Rick's method...

VBA Code:
Sub Heather515()
    With ActiveSheet
        i = .Cells(.Rows.Count, "A").End(xlUp).Row
        x = "=if(A2:A" & i & "= ""apple"",1,if(A2:A" & i & "="""","""",0))"
        .Range("B2:B" & i).Value = Application.Evaluate(x)
    End With
End Sub
Thanks. As I replied to Rick above, it works in the data I uploaded. But I need some modification in the new data I'm working on. Thanks!
 
Upvote 0
Thanks. As I replied to Rick above, it works in the data I uploaded. But I need some modification in the new data I'm working on. Thanks!
Unfortunately, I can only see a single cell in what you've provided with your update...
 
Upvote 0
Data.xlsx
A
23NotenoteFishNoenote
Sheet1


How about this one? Just in case you can't see it, I also attached the screenshot.
 

Attachments

  • Screenshot 2022-01-22 204757.png
    Screenshot 2022-01-22 204757.png
    14.6 KB · Views: 10
Upvote 0
Unfortunately, I can only see a single cell in what you've provided with your update...
Data.xlsx
AB
1Data Results
2NoteAppleNoteNoteNote1
3
4
5NoteFoodNoteNote0
6
7NoteNoteAppleNoteNote1
8
9NotenoteNotenotenotenoteBananaNtoenote0
10
11
12NtoenoteNotenoteAppleNtoenote1
13
14
15NotenoteOrangeNotenote0
16
17NOtenoteCoconutNoteno0
18
19
20NonotenoteCherriesNotneote0
21
22
23NotenoteFishNoenote0
Sheet1


Here...Thanks.
 
Upvote 0
Data.xlsx
AB
1Data Results
2NoteAppleNoteNoteNote1
3
4
5NoteFoodNoteNote0
6
7NoteNoteAppleNoteNote1
8
9NotenoteNotenotenotenoteBananaNtoenote0
10
11
12NtoenoteNotenoteAppleNtoenote1
13
14
15NotenoteOrangeNotenote0
16
17NOtenoteCoconutNoteno0
18
19
20NonotenoteCherriesNotneote0
21
22
23NotenoteFishNoenote0
Sheet1


Here...Thanks.
Thanks Heather. So what are the 'rules' regarding the new data?
 
Upvote 0
Try this...

VBA Code:
Sub Heather515B()
    Dim i As Long, j As Long, ws As Worksheet, arrIn, arrOut
    Set ws = ActiveSheet
    i = ws.Cells(Rows.Count, 1).End(xlUp).Row
    arrIn = ws.Range(ws.Cells(2, 1), ws.Cells(i, 2))
    ReDim arrOut(1 To UBound(arrIn), 1 To 1)

    For j = 1 To i - 1
        If arrIn(j, 1) Like "*apple*" Then
            arrOut(j, 1) = 1
            ElseIf arrIn(j, 1) = "" Then
            arrOut(j, 1) = ""
            Else
            arrOut(j, 1) = 0
        End If
    Next j
    ws.Range("B2").Resize(UBound(arrOut)).Value = arrOut
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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