View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Gary Keramidas Gary Keramidas is offline
external usenet poster
 
Posts: 2,494
Default VBA Simple question

this works for me, you may have to adjust rows as jim stated

Public Sub assign2()
Dim ws As Worksheets
Worksheets("Sheet3").Activate
Dim node As Integer
Dim xrow As Integer
Dim Sh As Worksheet
node = 1

Set Sh = Worksheets("sheet3")
For xrow = 2 To 10000
If Sh.Range("b" & xrow).Value < 0 And Sh.Range("b" & xrow).Value _
< Sh.Range("b" & xrow - 1).Value Then
Sh.Range("a" & xrow).Value = 33
End If
Next
End Sub

--


Gary


"Shane" wrote in message
...
I am received the error "1004" Method 'Range' of object '_Global' failed.

I tried both suggestions, neither changed this error.

"JE McGimpsey" wrote:

By looping xrow from 1 to 10000, the first iteration will error since
there is no Range("B0"). You might be able to use

For xrow = 2 to 10000

FYI, you don't gain anything by defining xrow as an Integer rather than
a Long (VBA uses longs internally), and if you ever use more than 32767
rows, you'll get an error.

In article ,
"Shane" wrote:

I apologize, the code should be shown as:

Public Sub assign()
Worksheets("Sheet3").Activate
Dim node As Integer
Dim xrow As Integer
node = 1
For xrow = 1 To 10000
If Range("b" & xrow).Value < 0 And range("b" & xrow).Value <
range("b" &
xrow - 1).Value Then
Range("a" & xrow).Value = 33
End If
Next
End Sub

I feel that the range("b" & xrow - 1).value is causing the problem.
How
should I write it?

"Shane" wrote:

I am looking to automate a cell comparison.

The code I have is as follows:

Public Sub assign()
Worksheets("Sheet3").Activate
Dim node As Integer
Dim xrow As Integer
node = 1
For xrow = 1 To 10000
If Range("b" & xrow).Value < 0 And Cells("b" & xrow).Value <
Cells("b" &
xrow).Value Then
Range("a" & xrow).Value = 33
End If
Next
End Sub

I want to search through 10000 cells in column B and if the value is
not 0,
and not equal to the previous cell, then enter "33". The figures are
arbitrary, but this is the task I need completed. Thanks for the
help!