vba question

flynpat46

New Member
Joined
Sep 24, 2018
Messages
11
Hi,
Please help..
I am having trouble finding right code for my project. There are three rows. The third rows is empty. I need vba to evaluate B1, if less than 0, put an X in C1. Repeat on down until there isn't a value in column B.
175
215
30
40
50
63
70
80
90
100
110
120
130
140
150
1616
1784
18185
<colgroup><col width="44" style="width: 33pt; mso-width-source: userset; mso-width-alt: 1609;"> <col width="43" style="width: 32pt; mso-width-source: userset; mso-width-alt: 1572;"> <col width="44" style="width: 33pt; mso-width-source: userset; mso-width-alt: 1609;"> <tbody> </tbody>
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Here's two possible ways (as the second doesn't loop it's probably the preferred):

Code:
Option Explicit
Sub Macro1()

    Dim rngMyCell As Range
    Dim xlnCalcMethod As XlCalculation
    
    With Application
        .ScreenUpdating = False
        xlnCalcMethod = .Calculation
        .Calculation = xlCalculationManual
    End With

    For Each rngMyCell In Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
        If Val(rngMyCell) = 0 Then rngMyCell.Offset(0, 1).Value = "X"
    Next rngMyCell
    
    With Application
        .Calculation = xlnCalcMethod
        .ScreenUpdating = True
    End With

End Sub
Sub Macro2()

    Dim lngLastRow As Long
    Dim xlnCalcMethod As XlCalculation
    
    With Application
        .ScreenUpdating = False
        xlnCalcMethod = .Calculation
        .Calculation = xlCalculationManual
    End With
    
    lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    With Range("C1:C" & lngLastRow)
        .Formula = "=IF(B1=0,""X"","""")"
        .Value = .Value
    End With
    
    With Application
        .Calculation = xlnCalcMethod
        .ScreenUpdating = True
    End With

End Sub

Hope that helps,

Robert
 
Upvote 0
Another option
Code:
Sub AddX()
   With Range("C1", Range("B" & Rows.Count).End(xlUp).Offset(, 1))
      .Value = Evaluate(Replace("if(@=0,""X"","""")", "@", .Offset(, -1).Address))
   End With
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
I have another question
instead of starting at "C1", Range("B",
I want to start at "F4", Range("B", will that work or will the next line need changes. I coded in VB, learning VBA and it hasn't clicked yet.
 
Upvote 0
correction on , Range("B", it should be , Range("E" but starting with an "X" in E4 if its cell is 0.
 
Upvote 0
Try
Code:
Sub AddX()
   With Range("[COLOR=#ff0000]E4[/COLOR]", Range("B" & Rows.Count).End(xlUp).Offset(, [COLOR=#ff0000]3[/COLOR]))
      .Value = Evaluate(Replace("if(@=0,""X"","""")", "@", .Offset(, -1).Address))
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,543
Messages
6,120,123
Members
448,947
Latest member
test111

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