0
votes

I've written 2 macros to do this task but I'm trying to consolidate and make it more efficient.

  • If there is a value = 1 in column I (it will either be blank or = 1) then look at column G
  • If the Value in column 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

4
What have you tried so far? What's working/not working? - BruceWayne
Can you edit your question and add your macro there? - Sergio Prats
Thanks for the guidance. Sorry for my ignorance.It should be edited... hopefully somewhat correctly - VBA Noob
You mentioned that you are checking to see if there is a value of 1 in Column 'I', but I don't see that anywhere in your code. Are you doing that calculation somewhere else? - K Paul
Is there a problem with the code? If it works as intended but it's slow, repetitive or otherwise inefficient, consider presenting your code on Code Review; Stack Overflow is about specific programming issues... which I'm still not seeing in this post :( - Mathieu Guindon

4 Answers

2
votes

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
0
votes

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
0
votes

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
0
votes

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