Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 247
Default If Statement to fill in column to last row

Frequent (large) downloads, into Excel, requires the addition of a column,
that is filled by the use of an "IF" statement.

The following If statement works fine, when tested.
How to get it to work thru all cells, to last row.

Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

If Range("C2") = "S" Or Range("C2") = "B" Then
Range("F2").Value = Range("D2").Value
Else
Range("F2") = 0
End If
End Sub

If there is a better, more efficient way, please also advise............
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default If Statement to fill in column to last row

Maybe this?

Dim X As Long
Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

For X = 2 To lastRow
If Cells(X, "C").Value = "S" Or Cells(X, "C").Value = "B" Then
Cells(X, "F").Value = Cells(X, "D").Value
Else
Cells(X, "F").Value = 0
End If
Next

--
Rick (MVP - Excel)


"BEEJAY" wrote in message
...
Frequent (large) downloads, into Excel, requires the addition of a column,
that is filled by the use of an "IF" statement.

The following If statement works fine, when tested.
How to get it to work thru all cells, to last row.

Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

If Range("C2") = "S" Or Range("C2") = "B" Then
Range("F2").Value = Range("D2").Value
Else
Range("F2") = 0
End If
End Sub

If there is a better, more efficient way, please also advise............


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 247
Default If Statement to fill in column to last row

Rick:
Thanks so much. Tried it out on my test sheet - works as expected/wanted.

IF I could trouble you again, could you explain the what and how of the X =2?


"Rick Rothstein" wrote:

Maybe this?

Dim X As Long
Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

For X = 2 To lastRow
If Cells(X, "C").Value = "S" Or Cells(X, "C").Value = "B" Then
Cells(X, "F").Value = Cells(X, "D").Value
Else
Cells(X, "F").Value = 0
End If
Next

--
Rick (MVP - Excel)


"BEEJAY" wrote in message
...
Frequent (large) downloads, into Excel, requires the addition of a column,
that is filled by the use of an "IF" statement.

The following If statement works fine, when tested.
How to get it to work thru all cells, to last row.

Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

If Range("C2") = "S" Or Range("C2") = "B" Then
Range("F2").Value = Range("D2").Value
Else
Range("F2") = 0
End If
End Sub

If there is a better, more efficient way, please also advise............



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default If Statement to fill in column to last row

The variable X serves as a counter, or maybe "indexer" is a better word, a
loop. The For statement (which is always coupled with a Next statement)
forms a loop. The X=2 establishes a starting value for the indexer and the
value in the lastRow variable sets the ending value for it. So, X will take
on the values (inside the loop's code) of 2, 3, 4, etc. until it reaches the
value in the lastRow variable (at which time the loop will end). I used X=2
as the starting point for the loop because the code you posted seemed to
suggest that Row 2 is where you tried to start your code at. Here is an
online link to the help file for the For..Next statement...

http://msdn.microsoft.com/en-us/libr...70(VS.60).aspx

--
Rick (MVP - Excel)


"BEEJAY" wrote in message
...
Rick:
Thanks so much. Tried it out on my test sheet - works as expected/wanted.

IF I could trouble you again, could you explain the what and how of the X
=2?


"Rick Rothstein" wrote:

Maybe this?

Dim X As Long
Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

For X = 2 To lastRow
If Cells(X, "C").Value = "S" Or Cells(X, "C").Value = "B" Then
Cells(X, "F").Value = Cells(X, "D").Value
Else
Cells(X, "F").Value = 0
End If
Next

--
Rick (MVP - Excel)


"BEEJAY" wrote in message
...
Frequent (large) downloads, into Excel, requires the addition of a
column,
that is filled by the use of an "IF" statement.

The following If statement works fine, when tested.
How to get it to work thru all cells, to last row.

Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

If Range("C2") = "S" Or Range("C2") = "B" Then
Range("F2").Value = Range("D2").Value
Else
Range("F2") = 0
End If
End Sub

If there is a better, more efficient way, please also
advise............




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 247
Default If Statement to fill in column to last row

Rick:
Not THAT is an explanation that even I can understand.
Thanks for everything.

"Rick Rothstein" wrote:

The variable X serves as a counter, or maybe "indexer" is a better word, a
loop. The For statement (which is always coupled with a Next statement)
forms a loop. The X=2 establishes a starting value for the indexer and the
value in the lastRow variable sets the ending value for it. So, X will take
on the values (inside the loop's code) of 2, 3, 4, etc. until it reaches the
value in the lastRow variable (at which time the loop will end). I used X=2
as the starting point for the loop because the code you posted seemed to
suggest that Row 2 is where you tried to start your code at. Here is an
online link to the help file for the For..Next statement...

http://msdn.microsoft.com/en-us/libr...70(VS.60).aspx

--
Rick (MVP - Excel)


"BEEJAY" wrote in message
...
Rick:
Thanks so much. Tried it out on my test sheet - works as expected/wanted.

IF I could trouble you again, could you explain the what and how of the X
=2?


"Rick Rothstein" wrote:

Maybe this?

Dim X As Long
Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

For X = 2 To lastRow
If Cells(X, "C").Value = "S" Or Cells(X, "C").Value = "B" Then
Cells(X, "F").Value = Cells(X, "D").Value
Else
Cells(X, "F").Value = 0
End If
Next

--
Rick (MVP - Excel)


"BEEJAY" wrote in message
...
Frequent (large) downloads, into Excel, requires the addition of a
column,
that is filled by the use of an "IF" statement.

The following If statement works fine, when tested.
How to get it to work thru all cells, to last row.

Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

If Range("C2") = "S" Or Range("C2") = "B" Then
Range("F2").Value = Range("D2").Value
Else
Range("F2") = 0
End If
End Sub

If there is a better, more efficient way, please also
advise............






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default If Statement to fill in column to last row

the column you use to find the last row must be a column that contains data
in the last row. You picked column C in this case. Most worksheets have a
least one column that is always contains data. problems occur if you have
columns where there isn't data in every row.

"BEEJAY" wrote:

Frequent (large) downloads, into Excel, requires the addition of a column,
that is filled by the use of an "IF" statement.

The following If statement works fine, when tested.
How to get it to work thru all cells, to last row.

Dim lastRow As Long
lastRow = Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

If Range("C2") = "S" Or Range("C2") = "B" Then
Range("F2").Value = Range("D2").Value
Else
Range("F2") = 0
End If
End Sub

If there is a better, more efficient way, please also advise............

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill Color If Statement Elgee Excel Worksheet Functions 3 August 5th 08 10:13 PM
How to fill a cell using an IF statement Rudi Excel Discussion (Misc queries) 1 April 25th 08 10:55 AM
How do you Control the fill color using an if statement formula? RussCrowder Excel Worksheet Functions 2 June 1st 07 09:31 PM
How to fill a column with color in statement anamarie30 Excel Programming 3 April 2nd 07 03:52 PM
Fill an Indirect Statement D Excel Discussion (Misc queries) 4 November 20th 06 07:21 PM


All times are GMT +1. The time now is 02:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"