View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
JLGWhiz JLGWhiz is offline
external usenet poster
 
Posts: 3,986
Default Loop through table of columns and rows

Part of your code got clipped on the paste, here is the complete modified
version:

Sub Search()
LastColumn = 18
NewColumnCount = 6
For ColumnCount = NewColumnCount To LastColumn
LastRw = 54
NewRowCount = 38
For RowCount = NewRowCount To LastRw
If Cells(RowCount, ColumnCount) < 0 Then
Cells(RowCount, ColumnCount) = 0
NewRowCount = NewRowCount + 2
End If
Next RowCount
NewColumnCount = NewColumnCount + 2
Next ColumnCount
End Sub


"JLGWhiz" wrote:

Your code modified:

For ColumnCount = NewColumnCount To LastColumn
LastRw = 54
NewRowCount = 38
For RowCount = NewRowCount To LastRw
If Cells(RowCount, ColumnCount) < 0 Then
Cells(RowCount, ColumnCount) = 0
NewRowCount = NewRowCount + 2
End If
Next RowCount
NewColumnCount = NewColumnCount + 2
Next ColumnCount
End Sub


'This could be written as:

Sub SearchAlt()
For Each c In Range("F38:R54")
If c.Value < 0 Then
c.Value = 0
End If
Next
End Sub


" wrote:

Iam trying to create a loop that searches through a table of columns
and rows to zero and negative values. My columns portion of the code
is causing an error. How should I change the code to have it work
properly.

Sub Search()
LastColumn = "R"
NewColumnCount = "F"
For ColumnCount = NewColumnCount To LastColumn
LastRow = 54
NewRowCount = 38
For RowCount = NewRowCount To LastRow
If Range(ColumnCount & RowCount) < 0 Then
Range(ColumnCount & RowCount) = 0
NewRowCount = NewRowCount + 2
End If
Next RowCount
NewColumnCount = NewColumnCount + 2
Next ColumnCount
End Sub

Frank