• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help, what's wrong with my code?

kiwi

New Member
Code:
Sub check1()

Dim pre1 As Range
Dim nex1 As Boolean
Dim result1 As Integer

For Each pre1 In Range("d1")

If pre1.Value = 23 Then
  
  If pre1.Offset(, 1) = 15 Then
  nex1 = True
  End If
  
  If nex1 = False Then
  result1 = -779
  ElseIf nex1 = True Then
  result1 = 221
  End If
  
  pre1.Offset(1, 1) = result1
  
End If

Next pre1

End Sub

d2 -- pre1.offset(,1) is 15, but result1 always return as -779, which means nex1 is false; but in my code, I said If pre1.Offset(, 1) = 15 Then nex1 = true.
I don't understand what is the wrong with my code, can someone please help
 
Hi Kiwi

You have all this code to evaluate 1 cell D1. Your For each statement is only evaluating a single cell and you probably want more cells evaluated than just 1 right. Here

For Each pre1 In Range("d1") 'This is 1 cell then next part has no where to go.

What would be more helpful would be to see your file or a dummy version of your file if the data is sensitive and state what you are trying to do. There will be a much more efficient method than the above.

Take care

Smallman
 
Hi ,

Your statement that d2 -- pre1.offset(,1) is wrong !

pre1.Offset(,1) refers to the cell E1 , since Offset(,1) is the same as Offset(0,1) which means a row offset of 0 and a column offset of 1 ; thus if pre1 is D1 , then pre1.Offset(,1) will be E1.

To get D2 , you need to use pre1.Offset(1) , without the comma ",".

Narayan
 
Hi ,

Your statement that d2 -- pre1.offset(,1) is wrong !

pre1.Offset(,1) refers to the cell E1 , since Offset(,1) is the same as Offset(0,1) which means a row offset of 0 and a column offset of 1 ; thus if pre1 is D1 , then pre1.Offset(,1) will be E1.

To get D2 , you need to use pre1.Offset(1) , without the comma ",".

Narayan

Thanks so much, I don't know what I was thinking back there. Now it works! :p
 
Back
Top