Reputation: 9
I've written 2 macros to do this task but I'm trying to consolidate and make it more efficient.
I
(it will either be blank or = 1) then look at column G
G
< 30 OR if the Value in column H
< 0.03 THEN
overwrite the value in column I to = "0" ... (if not then don't change the value in column I
and move on to check the next)The Ranges are I9:I45000
, G9:G45000
, and H9:H45000
.
I think there is a simple solution but after a few hours my un-educated self can't find it.
Module1:
Dim rngCell As Range, _
rngDataRange As Range
Set rngDataRange = Range("G9:G45000")
For Each rngCell In rngDataRange
With rngCell
If .Value < 30 Then
.Offset(0, 2).Value = "0" 'A[rngCell] to C[rngCell]
End If
End With
Next rngCell
End Sub
Module2:
Sub Macro1()
Dim rngCell As Range, _
rngDataRange As Range
Set rngDataRange = Range("H9:H45000")
For Each rngCell In rngDataRange
With rngCell
If .Value < 0.03 Then
.Offset(0, 1).Value = "0" 'A[rngCell] to C[rngCell]
End If
End With
Next rngCell
End Sub
This is the macro I run first.... It puts values in some of the cells in column I (where column C has values less than 1575):
Sub Macro1 () Dim rngCell As Range,_ rngDataRange As Range
Set rngdataRange = Range (C9:C45000)
For Each rngCell In rngDataRange
With rngCell
If .Value < 1575 Then
.Offset (0,6).Value="1"
End If
End With
Next rngCell
End Sub
Upvotes: 0
Views: 493
Reputation: 14373
This should do the job.
Sub CheckClmI()
Dim Rl As Long ' Last row
Dim R As Long
Application.ScreenUpdating = False
With ActiveSheet
' Used range should be enough
Rl = .UsedRange.Rows.Count
For R = 9 To Rl
If Val(.Cells(R, "I").Value) = 1 Then
If Val(.Cells(R, "G").Value) < 30 Or _
Val(.Cells(R, "H").Value < 0.03) Then
.Cells(R, "I").Value = 0
End If
End If
Next R
End With
Application.ScreenUpdating = True
End Sub
Upvotes: 2
Reputation: 354
You can just do all the tests in one go:
Dim rngCell As Range
Dim rngDataRange As Range
Dim iCell as range
Dim hVal as variant
Set rngDataRange = Range("G9:G45000")
For Each rngCell In rngDataRange
With rngCell
Set iCell = .Offset (0,2)
hVal = .Offset (0,1).Value
If iVal = 0 or iVal = vbnullstring then
If .Value < 30 or hVal > .3 Then
iCell.Value = "0"
End If
End if
End With
Next rngCell
End Sub
Upvotes: 0
Reputation: 6984
I like to count the rows so you don't have wasted loops.
Dim LstRw As Long
Dim Rng As Range, c As Range
LstRw = Cells(Rows.Count, "G").End(xlUp).Row
Set Rng = Range("G9:G" & LstRw)
For Each c In Rng.Cells
If c < 30 Or c.Offset(, 1) < 0.03 Then c.Offset(, 2) = 0
Next c
Upvotes: 0
Reputation: 144
What about something like this?
Sub Macro1()
OnError Goto OopsIDidItAgain
Dim rngCell As Range, rngDataRange As Range
Application.ScreenUpdating = False
Set rngDataRange = Range("G9:G45000")
For Each rngCell In rngDataRange
With rngCell
If .Value < 30 Or .Offset(0, 1).Value < 0.03 Then .Offset(0, 2).Value = "0"
End With
Next rngCell
OopsIDidItAgain:
Application.ScreenUpdating = True
End Sub
Upvotes: 0