Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 69
Default Select cell after last row in selection?

I'm writing a macro to sum all values in a selection (constrained to 1
column), except numbers that are displayed in gray. The macro should
work exactly like the built-in Sum button, so that the sum is placed
below the last row in the selection, and it's that part I can't figure
out how to do. How do I select the cell after the last row in the selection?

Public Sub CalculateSum()

' Prepare a sum variable
nsum = 0

' Make sure there is only one column in the range
If Selection.Columns.count 1 Then
MsgBox "This macro can only be used on 1 column at a time."
Exit Sub
End If

' Loop through selected range
For Each c In Selection.Cells
If c.Font.ColorIndex < 48 Then
nsum = nsum + c.Value
End If
Next c

MsgBox nsum

End Sub

Many thanks,

Gustaf
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 239
Default Select cell after last row in selection?

On Jun 28, 4:50*pm, Gustaf wrote:
I'm writing a macro to sum all values in a selection (constrained to 1
column), except numbers that are displayed in gray. The macro should
work exactly like the built-in Sum button, so that the sum is placed
below the last row in the selection, and it's that part I can't figure
out how to do. How do I select the cell after the last row in the selection?

Public Sub CalculateSum()

* * *' Prepare a sum variable
* * *nsum = 0

* * *' Make sure there is only one column in the range
* * *If Selection.Columns.count 1 Then
* * * * *MsgBox "This macro can only be used on 1 column at a time."
* * * * *Exit Sub
* * *End If

* * *' Loop through selected range
* * *For Each c In Selection.Cells
* * * * *If c.Font.ColorIndex < 48 Then
* * * * * * *nsum = nsum + c.Value
* * * * *End If
* * *Next c

* * *MsgBox nsum

End Sub

Many thanks,

Gustaf


Just add this line at the end of the code.
Range("A65536").End(xlUp).Offset(1, 0).Value = nsum

Regards,
Madiya
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 69
Default Select cell after last row in selection?

Madiya wrote:

Just add this line at the end of the code.
Range("A65536").End(xlUp).Offset(1, 0).Value = nsum


Thank you, but this is not really what I want. This adds the value to
the first column, after the last row. I want the value in the same
column as the range, but just below the range.

Gustaf
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 69
Default Select cell after last row in selection?

I found the solution I wanted now:

Selection.End(xlDown).Offset(1, 0).Value = nsum

Gustaf
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Select cell after last row in selection?

Which will place nsum in the first blank cell in the selected column.

OK as long as your data is contiguous with no blanks.


Gord Dibben MS Excel MVP

On Sat, 28 Jun 2008 17:48:37 +0200, Gustaf wrote:

I found the solution I wanted now:

Selection.End(xlDown).Offset(1, 0).Value = nsum

Gustaf




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Select cell after last row in selection?

I found the solution I wanted now:

Selection.End(xlDown).Offset(1, 0).Value = nsum


OK as long as your data is contiguous with no blanks.


Good point! This should work for all situations...

Cells(Selection.Row + Selection.Count, Selection.Column).Value = nsum

Rick
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
how to select entire row and column in a sigle cell selection sabu varoor Excel Worksheet Functions 3 December 22nd 08 05:19 PM
Select last cell in Selection jlclyde Excel Discussion (Misc queries) 4 June 27th 08 09:23 PM
Range(Selection, Selection.End(xlToRight)).Select Dave Birley Excel Programming 2 June 6th 07 04:53 PM
Select another Textbox After Selection Corey Excel Programming 1 February 21st 07 10:40 PM
Select specific columns for a given cell selection crazybass2 Excel Programming 2 April 21st 05 05:41 PM


All times are GMT +1. The time now is 06:11 PM.

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"