Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default How do I programmatically build a range?

I have a fixed range of cells (41) that I need to chart. The user may or may
not fill all the final cells. I need to exclude these unused cells at the end
from the chart.

Dim LastColReservedCell As Integer
Dim LastActiveCell As Integer
Dim MyRange As Range

LastActiveCell = 0
LastColReservedCell = 41

For Each c In Worksheets("Charts").Range _
("B1:B41").Cells

If c.Value < 1 Then
Blanks = Blanks + 1
Else
Blanks = Blanks
End If
Next

LastActiveCell = LastColReservedCell - Blanks

'Here is where the problem starts. Trying to build the range generates an
error 13.
MyRange = "A1:" + LastActiveCell

'If I can build a valid Range, I would like to use it as shown below:

ActiveChart.SetSourceData Source:=Sheets("Charts").Range(MyRange), PlotBy _
:=xlColumns

'Using (MyRange) instead of ("A1:B41")

Is this possible?

Thank You for your help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default How do I programmatically build a range?

MyRange = "A1:A" & LastActiveCell


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"JudgeMental" wrote in message
...
I have a fixed range of cells (41) that I need to chart. The user may or
may
not fill all the final cells. I need to exclude these unused cells at the
end
from the chart.

Dim LastColReservedCell As Integer
Dim LastActiveCell As Integer
Dim MyRange As Range

LastActiveCell = 0
LastColReservedCell = 41

For Each c In Worksheets("Charts").Range _
("B1:B41").Cells

If c.Value < 1 Then
Blanks = Blanks + 1
Else
Blanks = Blanks
End If
Next

LastActiveCell = LastColReservedCell - Blanks

'Here is where the problem starts. Trying to build the range generates an
error 13.
MyRange = "A1:" + LastActiveCell

'If I can build a valid Range, I would like to use it as shown below:

ActiveChart.SetSourceData Source:=Sheets("Charts").Range(MyRange), PlotBy
_
:=xlColumns

'Using (MyRange) instead of ("A1:B41")

Is this possible?

Thank You for your help.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default How do I programmatically build a range?

n = Cells(42, "b").End(xlUp).Row
MyRange = "A1:B" & n
--
Gary''s Student - gsnu200776


"JudgeMental" wrote:

I have a fixed range of cells (41) that I need to chart. The user may or may
not fill all the final cells. I need to exclude these unused cells at the end
from the chart.

Dim LastColReservedCell As Integer
Dim LastActiveCell As Integer
Dim MyRange As Range

LastActiveCell = 0
LastColReservedCell = 41

For Each c In Worksheets("Charts").Range _
("B1:B41").Cells

If c.Value < 1 Then
Blanks = Blanks + 1
Else
Blanks = Blanks
End If
Next

LastActiveCell = LastColReservedCell - Blanks

'Here is where the problem starts. Trying to build the range generates an
error 13.
MyRange = "A1:" + LastActiveCell

'If I can build a valid Range, I would like to use it as shown below:

ActiveChart.SetSourceData Source:=Sheets("Charts").Range(MyRange), PlotBy _
:=xlColumns

'Using (MyRange) instead of ("A1:B41")

Is this possible?

Thank You for your help.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How do I programmatically build a range?

You have MyRange declared as being of type Range, but you are trying to
assign a text to it... they are not compatible. As a Range, you need to Set
another Range object to it. Also, the text you are trying to assign is
incorrect; it needs a column reference after the colon since the only thing
in LastActiveCell is a number. Instead of this...

'Here is where the problem starts. Trying to build the range generates an
error 13.
MyRange = "A1:" + LastActiveCell


do it this way...

Set MyRange = Range("A1:B" + LastActiveCell)

By the way, you can solve for the last used cell directly without a loop...

LastActiveCellRow = Range("B42").End(xlUp).Row

Note that you have to start looking from below the last piece of data. In
your case, data can be filled down to B41, so start looking from below that
(hence, my use of B42). In the general case, assuming no data is located
below the cell you want to find (B41 in this case), you can use this...

LastActiveCellRow = Cells(Rows.Count, "B").End(xlUp).Row

Rick


"JudgeMental" wrote in message
...
I have a fixed range of cells (41) that I need to chart. The user may or
may
not fill all the final cells. I need to exclude these unused cells at the
end
from the chart.

Dim LastColReservedCell As Integer
Dim LastActiveCell As Integer
Dim MyRange As Range

LastActiveCell = 0
LastColReservedCell = 41

For Each c In Worksheets("Charts").Range _
("B1:B41").Cells

If c.Value < 1 Then
Blanks = Blanks + 1
Else
Blanks = Blanks
End If
Next

LastActiveCell = LastColReservedCell - Blanks

'Here is where the problem starts. Trying to build the range generates an
error 13.
MyRange = "A1:" + LastActiveCell

'If I can build a valid Range, I would like to use it as shown below:

ActiveChart.SetSourceData Source:=Sheets("Charts").Range(MyRange), PlotBy
_
:=xlColumns

'Using (MyRange) instead of ("A1:B41")

Is this possible?

Thank You for your help.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default How do I programmatically build a range?

or another way that may show another method.

Sub test()
Dim MyRange As Range
Dim lastactivecell As Range
Set lastactivecell = Range("B42").End(xlUp)
Set MyRange = Range("A1:" + lastactivecell.Address)
End Sub

--


Gary


"JudgeMental" wrote in message
...
I have a fixed range of cells (41) that I need to chart. The user may or may
not fill all the final cells. I need to exclude these unused cells at the end
from the chart.

Dim LastColReservedCell As Integer
Dim LastActiveCell As Integer
Dim MyRange As Range

LastActiveCell = 0
LastColReservedCell = 41

For Each c In Worksheets("Charts").Range _
("B1:B41").Cells

If c.Value < 1 Then
Blanks = Blanks + 1
Else
Blanks = Blanks
End If
Next

LastActiveCell = LastColReservedCell - Blanks

'Here is where the problem starts. Trying to build the range generates an
error 13.
MyRange = "A1:" + LastActiveCell

'If I can build a valid Range, I would like to use it as shown below:

ActiveChart.SetSourceData Source:=Sheets("Charts").Range(MyRange), PlotBy _
:=xlColumns

'Using (MyRange) instead of ("A1:B41")

Is this possible?

Thank You for your help.



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
Build Select Case Programmatically Dave Birley Excel Programming 11 June 1st 07 05:39 PM
Build a range from a cell [email protected] Excel Programming 3 April 12th 07 11:08 PM
How to build a forward/back range finder John Wirt[_10_] Excel Programming 1 July 24th 05 09:21 PM
How do I programmatically specify a range. Hari[_3_] Excel Programming 4 January 17th 05 03:31 PM
How to build build a macro that automatically imports PedroPeso Excel Programming 1 December 26th 03 08:14 PM


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