Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,836
Default Sum Column Y at first blank-bottom cell.

The code below fails on this line:
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
The balloon in the VBE says sumRng =Nothing
What am I doing wrong?

Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long, sumRng As Range, Tot As Double

For Each sh In Worksheets

If (sh.Name) < "Sheet1" Then
sh.Activate

Rows("1:1").Select
Selection.Font.Bold = True

lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For Each c In Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = Cells(Rows.Count, "Y").End(xlUp).Row
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
ActiveSheet.Rows(lastRow + 1) = Tot

Rows("2:2").Select
Selection.Delete Shift:=xlUp

End If

Next sh

Sheets("Sheet1").Select

End Sub

Regards,
Ryan---

--
RyGuy
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sum Column Y at first blank-bottom cell.

Since sumrng is an object (a range variable), you need to use Set:

Set sumrng = ....



ryguy7272 wrote:

The code below fails on this line:
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
The balloon in the VBE says sumRng =Nothing
What am I doing wrong?

Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long, sumRng As Range, Tot As Double

For Each sh In Worksheets

If (sh.Name) < "Sheet1" Then
sh.Activate

Rows("1:1").Select
Selection.Font.Bold = True

lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For Each c In Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = Cells(Rows.Count, "Y").End(xlUp).Row
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
ActiveSheet.Rows(lastRow + 1) = Tot

Rows("2:2").Select
Selection.Delete Shift:=xlUp

End If

Next sh

Sheets("Sheet1").Select

End Sub

Regards,
Ryan---

--
RyGuy


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,836
Default Sum Column Y at first blank-bottom cell.

Thanks Dave! The errors stopped, but the sum is not correct. Also, all
columns seem to have a sum at the bottom (first blank at bottom). How can I
sum just the Column Y and have the result appear in the first blank cell?

Regards,
Ryan---

--
RyGuy


"Dave Peterson" wrote:

Since sumrng is an object (a range variable), you need to use Set:

Set sumrng = ....



ryguy7272 wrote:

The code below fails on this line:
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
The balloon in the VBE says sumRng =Nothing
What am I doing wrong?

Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long, sumRng As Range, Tot As Double

For Each sh In Worksheets

If (sh.Name) < "Sheet1" Then
sh.Activate

Rows("1:1").Select
Selection.Font.Bold = True

lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For Each c In Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = Cells(Rows.Count, "Y").End(xlUp).Row
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
ActiveSheet.Rows(lastRow + 1) = Tot

Rows("2:2").Select
Selection.Delete Shift:=xlUp

End If

Next sh

Sheets("Sheet1").Select

End Sub

Regards,
Ryan---

--
RyGuy


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sum Column Y at first blank-bottom cell.

Option Explicit
Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long
Dim sumRng As Range
Dim Tot As Double

For Each sh In Worksheets
If LCase(sh.Name) < LCase("Sheet1") Then
With sh
.Rows(1).Font.Bold = True

lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For Each c In .Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = .Cells(.Rows.Count, "Y").End(xlUp).Row
Set sumRng = .Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
.Cells(lastRow + 1, "Y").Value = Tot

.Rows(2).Delete
End With

End If

Next sh

End Sub

One more thing...

You're deleting row 2 at the end of that loop.

What happens if row 2 contains a number? It'll be added to your total.

I think I'd exclude it from the sum or delete it before you do anything (well,
if you really don't want it included).

ryguy7272 wrote:

The code below fails on this line:
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
The balloon in the VBE says sumRng =Nothing
What am I doing wrong?

Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long, sumRng As Range, Tot As Double

For Each sh In Worksheets

If (sh.Name) < "Sheet1" Then
sh.Activate

Rows("1:1").Select
Selection.Font.Bold = True

lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For Each c In Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = Cells(Rows.Count, "Y").End(xlUp).Row
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
ActiveSheet.Rows(lastRow + 1) = Tot

Rows("2:2").Select
Selection.Delete Shift:=xlUp

End If

Next sh

Sheets("Sheet1").Select

End Sub

Regards,
Ryan---

--
RyGuy


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,836
Default Sum Column Y at first blank-bottom cell.

Thanks Dave. Just out of curosity, how can I get the =sum() function in that
first blank cell? That Application.WorksheetFunction.Sum(sumRng) is pretty
neat, but I was hoping to be able to use the =sum() function because it will
be a lot easier to audit.

Regards,
Ryan--


--
RyGuy


"Dave Peterson" wrote:

Option Explicit
Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long
Dim sumRng As Range
Dim Tot As Double

For Each sh In Worksheets
If LCase(sh.Name) < LCase("Sheet1") Then
With sh
.Rows(1).Font.Bold = True

lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For Each c In .Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = .Cells(.Rows.Count, "Y").End(xlUp).Row
Set sumRng = .Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
.Cells(lastRow + 1, "Y").Value = Tot

.Rows(2).Delete
End With

End If

Next sh

End Sub

One more thing...

You're deleting row 2 at the end of that loop.

What happens if row 2 contains a number? It'll be added to your total.

I think I'd exclude it from the sum or delete it before you do anything (well,
if you really don't want it included).

ryguy7272 wrote:

The code below fails on this line:
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
The balloon in the VBE says sumRng =Nothing
What am I doing wrong?

Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long, sumRng As Range, Tot As Double

For Each sh In Worksheets

If (sh.Name) < "Sheet1" Then
sh.Activate

Rows("1:1").Select
Selection.Font.Bold = True

lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For Each c In Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = Cells(Rows.Count, "Y").End(xlUp).Row
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
ActiveSheet.Rows(lastRow + 1) = Tot

Rows("2:2").Select
Selection.Delete Shift:=xlUp

End If

Next sh

Sheets("Sheet1").Select

End Sub

Regards,
Ryan---

--
RyGuy


--

Dave Peterson



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sum Column Y at first blank-bottom cell.

First, there was a mistake in the original code.

This line:
..Cells(lastRow + 1, "Y").Value = Tot
referred to lastrow instead of lastYRow.

I didn't notice it before.

But there's no reason you can't reuse that variable again -- and make sure you
don't make an important typo <bg.



Option Explicit
Sub Math2()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet

For Each sh In Worksheets
If LCase(sh.Name) < LCase("Sheet1") Then
With sh
.Rows(1).Font.Bold = True

lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For Each c In .Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastRow = .Cells(.Rows.Count, "Y").End(xlUp).Row
.Cells(lastRow + 1, "Y").FormulaR1C1 _
= "=sum(r3c:r[-1]c)"

.Rows(2).Delete
End With

End If

Next sh

End Sub

This formular1c1
=sum(r3c:r[-1]c)
means to start at row 3 of the same column through the row above the cell with
the formula r[-1] of the same column.




ryguy7272 wrote:

Thanks Dave. Just out of curosity, how can I get the =sum() function in that
first blank cell? That Application.WorksheetFunction.Sum(sumRng) is pretty
neat, but I was hoping to be able to use the =sum() function because it will
be a lot easier to audit.

Regards,
Ryan--

--
RyGuy

"Dave Peterson" wrote:

Option Explicit
Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long
Dim sumRng As Range
Dim Tot As Double

For Each sh In Worksheets
If LCase(sh.Name) < LCase("Sheet1") Then
With sh
.Rows(1).Font.Bold = True

lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For Each c In .Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = .Cells(.Rows.Count, "Y").End(xlUp).Row
Set sumRng = .Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
.Cells(lastRow + 1, "Y").Value = Tot

.Rows(2).Delete
End With

End If

Next sh

End Sub

One more thing...

You're deleting row 2 at the end of that loop.

What happens if row 2 contains a number? It'll be added to your total.

I think I'd exclude it from the sum or delete it before you do anything (well,
if you really don't want it included).

ryguy7272 wrote:

The code below fails on this line:
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
The balloon in the VBE says sumRng =Nothing
What am I doing wrong?

Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long, sumRng As Range, Tot As Double

For Each sh In Worksheets

If (sh.Name) < "Sheet1" Then
sh.Activate

Rows("1:1").Select
Selection.Font.Bold = True

lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For Each c In Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = Cells(Rows.Count, "Y").End(xlUp).Row
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
ActiveSheet.Rows(lastRow + 1) = Tot

Rows("2:2").Select
Selection.Delete Shift:=xlUp

End If

Next sh

Sheets("Sheet1").Select

End Sub

Regards,
Ryan---

--
RyGuy


--

Dave Peterson


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,836
Default Sum Column Y at first blank-bottom cell.

Thank you so much!! It's really amazing what can be done with both VBA and
Excel.

--
RyGuy


"Dave Peterson" wrote:

First, there was a mistake in the original code.

This line:
..Cells(lastRow + 1, "Y").Value = Tot
referred to lastrow instead of lastYRow.

I didn't notice it before.

But there's no reason you can't reuse that variable again -- and make sure you
don't make an important typo <bg.



Option Explicit
Sub Math2()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet

For Each sh In Worksheets
If LCase(sh.Name) < LCase("Sheet1") Then
With sh
.Rows(1).Font.Bold = True

lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For Each c In .Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastRow = .Cells(.Rows.Count, "Y").End(xlUp).Row
.Cells(lastRow + 1, "Y").FormulaR1C1 _
= "=sum(r3c:r[-1]c)"

.Rows(2).Delete
End With

End If

Next sh

End Sub

This formular1c1
=sum(r3c:r[-1]c)
means to start at row 3 of the same column through the row above the cell with
the formula r[-1] of the same column.




ryguy7272 wrote:

Thanks Dave. Just out of curosity, how can I get the =sum() function in that
first blank cell? That Application.WorksheetFunction.Sum(sumRng) is pretty
neat, but I was hoping to be able to use the =sum() function because it will
be a lot easier to audit.

Regards,
Ryan--

--
RyGuy

"Dave Peterson" wrote:

Option Explicit
Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long
Dim sumRng As Range
Dim Tot As Double

For Each sh In Worksheets
If LCase(sh.Name) < LCase("Sheet1") Then
With sh
.Rows(1).Font.Bold = True

lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For Each c In .Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = .Cells(.Rows.Count, "Y").End(xlUp).Row
Set sumRng = .Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
.Cells(lastRow + 1, "Y").Value = Tot

.Rows(2).Delete
End With

End If

Next sh

End Sub

One more thing...

You're deleting row 2 at the end of that loop.

What happens if row 2 contains a number? It'll be added to your total.

I think I'd exclude it from the sum or delete it before you do anything (well,
if you really don't want it included).

ryguy7272 wrote:

The code below fails on this line:
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
The balloon in the VBE says sumRng =Nothing
What am I doing wrong?

Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long, sumRng As Range, Tot As Double

For Each sh In Worksheets

If (sh.Name) < "Sheet1" Then
sh.Activate

Rows("1:1").Select
Selection.Font.Bold = True

lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For Each c In Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value = "=RC[-17]*RC[-2]"
End If
Next c

lastYRow = Cells(Rows.Count, "Y").End(xlUp).Row
sumRng = ActiveSheet.Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
MsgBox Tot
ActiveSheet.Rows(lastRow + 1) = Tot

Rows("2:2").Select
Selection.Delete Shift:=xlUp

End If

Next sh

Sheets("Sheet1").Select

End Sub

Regards,
Ryan---

--
RyGuy

--

Dave Peterson


--

Dave Peterson

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
show last used cell in a column at the bottom of column. dsharris59 Excel Worksheet Functions 3 February 4th 09 07:09 PM
Finding the bottom non-blank cell in a range Fenneth Excel Discussion (Misc queries) 7 July 6th 06 06:05 PM
Find value in bottom cell in a column [email protected] Excel Programming 7 June 28th 06 07:46 PM
Dynamic named list includes blank cell at bottom L Scholes Excel Discussion (Misc queries) 1 April 13th 06 06:13 PM
dislike jump bottom of column by double-clicking the bottom of cel Joe Excel Discussion (Misc queries) 1 April 9th 06 09:27 PM


All times are GMT +1. The time now is 10:11 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"