joseph.bloggs
New Member
- Joined
- Nov 7, 2011
- Messages
- 4
Hello guys,
I've been trying to figure this out the whole evening yesterday and today, however I'm lost. This is the 3rd time i am trying to write a macros and most of the knowledge I have is through googling snippets of what I want it to do.
What I have: Spreadsheet with column Area, containing values in ascending order. They mayb be either all positive, or half positive, half negative or all negative. Rows start most often with negative values. What I am trying to do, is
1. Find First positive value in the rows and select containing it cell, stop the for each loop as there is no need to go further.
2. If there is no positive value in the rows, do other loop and select the largest negative number from a list of negative values in the same column. I am trying to do it using the Application.max(). I then want to select the cell in which the max value is located, but can't. Seem like Find doesn't work. It returns error which I do not understand. Maybe there is a flaw in the way I designed loops?
Could you please look into this?
Thank you
I've been trying to figure this out the whole evening yesterday and today, however I'm lost. This is the 3rd time i am trying to write a macros and most of the knowledge I have is through googling snippets of what I want it to do.
What I have: Spreadsheet with column Area, containing values in ascending order. They mayb be either all positive, or half positive, half negative or all negative. Rows start most often with negative values. What I am trying to do, is
1. Find First positive value in the rows and select containing it cell, stop the for each loop as there is no need to go further.
2. If there is no positive value in the rows, do other loop and select the largest negative number from a list of negative values in the same column. I am trying to do it using the Application.max(). I then want to select the cell in which the max value is located, but can't. Seem like Find doesn't work. It returns error which I do not understand. Maybe there is a flaw in the way I designed loops?
Could you please look into this?
Code:
Sub FirstPositive()
Dim Area As Range
Dim r As Range
Dim MaxValue As Variant
Dim s As Range
Cells.Find(What:="Area", LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Activate
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Set Area = Selection
Area.Interior.ColorIndex = 0
For Each r In Area.Rows
If r.Value > 0 Then
r.Select
ActiveCell.Interior.ColorIndex = 4
Exit For
End If
Next
For Each r In Area.Rows
If r.Value < 0 Then
MaxValue = Application.Max(Area)
Area.Find(What:="MaxValue", LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Interior.ColorIndex = 17
Exit For
End If
Next
End Sub
Last edited: