Insert a value given a certain condition

abberyfarm

Well-known Member
Joined
Aug 14, 2011
Messages
733
Hi here,

I have two Columns, A and B. Column A contains numbers and Column B contains zeros. See below.

I need to insert 1's on certain rows on Column C based the condition below..

The condition is:

If there is a zero in Column B and the value in Column A is > 0 and <2.22, then insert a 1 three rows above in Column C and a 1 on the next row in Column C.

Thank you

Excel Workbook
ABCD
12.001
22.00
30.44
40.640Value in Col A is > 0 and
50.671
62.25
71.67
Sheet1
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try

Code:
Sub test2()
Dim LR As Long, i As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For i = 1 To LR
    If Range("A" & i).Value > 0 And Range("A" & i).Value < 2.2 And Range("B" & i).Value <> "" Then
        Range("C" & i - 3).Value = 1
        Range("C" & i + 1).Value = 1
    End If
Next i
End Sub
 
Upvote 0
Hi,

Thank you that works perfect.

I'm using a variable
Code:
Const ContRws As Long = 4
for a different purpose in a macro. This value of this variable changes each time.

In this case I moved the marker 3 rows up ( which is one less than this variable).

Would it be or is it possible to change the code so that it moves the marker up one row less than the value of the variable?

That would be a great help.

Thank you
 
Upvote 0
Try

Rich (BB code):
Sub test2()
Dim LR As Long, i As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For i = 1 To LR
    If Range("A" & i).Value > 0 And Range("A" & i).Value < 2.2 And Range("B" & i).Value <> "" Then
        Range("C" & i - ContRws + 1).Value = 1
        Range("C" & i + 1).Value = 1
    End If
Next i
End Sub
 
Upvote 0
Hi,

That is not giving me an error but it does not work either. It doesn't a insert a 1. When I hover the cursor over ContRows it says that it is empty?

Thanks
 
Upvote 0
Try

Rich (BB code):
Sub test2()
Const ContRws As Long = 4
Dim LR As Long, i As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For i = 1 To LR
    If Range("A" & i).Value > 0 And Range("A" & i).Value < 2.2 And Range("B" & i).Value <> "" Then
        Range("C" & i - ContRws + 1).Value = 1
        Range("C" & i + 1).Value = 1
    End If
Next i
End Sub
 
Upvote 0
Thanks that works. That variable is defined in a different module, is there anyway on linking them, so that if the value is change in other module it will be changed in this module?
 
Upvote 0
Yep. In the other module, at the top, before any subs

Rich (BB code):
Public Const ContRws As Long = 4

then remove the Const declaration from my code above.
 
Upvote 0

Forum statistics

Threads
1,226,453
Messages
6,191,134
Members
453,642
Latest member
jefals

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