Quick Simple Newbie Question

Alfred12

New Member
Joined
Feb 28, 2011
Messages
15
Hi all --

Can someone please tell me what is wrong with this line of code:

If Cells(i, 4).Value = 1 Then

This line appears in a for next loop being counted by the variable "i". The idea is to look at the value in column D in all the rows, and if the cell contains the number 1, the "then" part is executed.

It compiles fine, and I've got messages inserted so that I know that this line is being executed. But it is not finding my "1"s to trigger the "then" action. (And in my test cases I certainly have some "1"s in the D column.) What super-basic obvious thing am i missing?

It is as if there is no tutorial basic enough for me!

Thanks!

Alfred
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
forgive me for asking but i have not seen the code

do you have the line for each cell in selection?
 
Upvote 0
Thanks much for helping Dryver.

I am enough of a noob that I do not understand your question. Can you please elaborate / dumb it down a level?
 
Upvote 0
Here is the whole thing, if context is helpful:

Dim S As Integer
Dim i As Integer
MsgBox "Hi!"
For S = 2 To Sheets.Count
Worksheets(S).Activate
For i = 1 To 5
If Cells(i, 4).Value = 1 Then
MsgBox "Hi Again"
If Cells(i, 5).Value = “yes” Then
Range(Cells(i, 1)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(1, 0).Paste
Range(Cells(i, 4)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(0, 1).Paste
Range(Cells(i, 6)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(0, 2).Paste
Range(Cells(i, 7)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(0, 3).Paste
Range(Cells(i, 8)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(0, 4).Paste
End If
End If
Next i
Next S
End Sub

(Sorry, not sure how to maintain indenting in the post.)
 
Upvote 0
Ive indented the 2 lines to enter this only looks down 5 rows is that right?

If it where it says i = 1 to 5 change 5 to a number that covers all the rows



Code:
Sub WhatEver()
Dim S As Integer
Dim i As Integer
MsgBox "Hi!"
For S = 2 To Sheets.Count
Worksheets(S).Activate
                        For Each cell In Selection
For i = 1 To 5
If Cells(i, 4).Value = 1 Then
MsgBox "Hi Again"
If Cells(i, 5).Value = “yes” Then
Range(Cells(i, 1)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(1, 0).Paste
Range(Cells(i, 4)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(0, 1).Paste
Range(Cells(i, 6)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(0, 2).Paste
Range(Cells(i, 7)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(0, 3).Paste
Range(Cells(i, 8)).Copy
Sheets(“Master”).Range(“a65536”).End(x1Up).Offset(0, 4).Paste
End If
End If
Next i
                        Next cell
Next S
End Sub
 
Upvote 0
I lowered the row count to 5 just for testing purposes. When I see it functioning properly I'll put that back up high.

I entered the text you suggested with no change in results. Sadly.

Is there something else wrong with the If Then statement? Do I need to use "Range" instead of "Cells"? Is it proper to have the ".Value" where I've got it? Should I replace the "1" with a variable set to equal 1?

Thanks yet again.

Alfred
 
Upvote 0
I only tested it using F8 to see if i could get the second message box to come up if a 1 was in a cell.

What is not working?
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top