Home > Net >  Cannot get correct output
Cannot get correct output

Time:01-14

I am attempting to code a macro that will produce the number 8 in column C anytime there is a 1 in column A such as this:

Desired Output

enter image description here

My Current code is:

Sub STD()

For Each X In Range("A2:A7")

If X.Value = 1 Then 

Range("C2:C7").Value = 8

Else

End If

Next X

End Sub

However the output fills all of the range of column C with the number 8.

Current Output

enter image description here

What am I doing wrong?

CodePudding user response:

Just substitute the line Range("C2:C7").Value = 8 with the following line

Worksheets("Sheet1").Cells(x.Row, "C").Value = 8

, assuming the current worksheet is "Sheet1".

CodePudding user response:

Simply edit your line
Range("C2:C7").Value = 8
with
Cells(x.Row, "C").Value = 8

CodePudding user response:

The cell with the result is located two column to the right of the X object. Hence, one of the way to address that cell would be to use .OFFSET property as follow instead of using Range("C2:C7").Value = 8

For Each X In Range("A2:A7")
    If X.Value = 1 Then
        X.Offset(0, 2).Value = 8  
    Else
        X.Offset(0, 2).Value = ""
    End If
Next X
  •  Tags:  
  • Related