Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 202
Default How to define a range based on first and last cells with data?

This is my first Excel macro. I can get this code to work on a defined range
like "A1:A10". But how can I code the macro to select the range by first and
last cells with data? This is my code so far...

Sub Macro1()

'1. Column reference will always be constant, rows will be variable
'2. Find first non-zero length cell
'3. Find the last cell with data
'4. Select range
'5. Make each value in selected range a working hyperlink


Dim FirstRow As Long
Dim LastRow As Long
FirstRow = Worksheets("Sheet1").Range("a1").End(xlDown).Row + 1 ' this
will give you the row of the 1st blank cell in A
LastRow = Worksheets("Sheet1").Range("a65536").End(xlUp).Row + 1 ' this
will give you the row of the last blank cell in A


Dim myRange As Range
Dim myVar As Range

'This is where I ran into trouble...
Set myRange = Range("A" & "'" & blankrow & "'" & ":A" & "'" & myvalue & "'"
& ") 'set range

myRange.Select


'Microsoft KB271856
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell

End Sub

--
Dennis
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to define a range based on first and last cells with data?


try

activesheet.usedrange

regards


--
tony h
------------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=517527

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to define a range based on first and last cells with data?


try

activesheet.usedrange

regards


--
tony h
------------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=517527

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to define a range based on first and last cells with data?


LastRow = Cells(Rows.Count, "A").End(xlUp).Row
FirstRow = Cells(1, "A").End(xlDown).Row
Set myRange = Range(Cells(FirstRow, "A"), Cells(LastRow, "A"))


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"Dennis" wrote in message
...
This is my first Excel macro. I can get this code to work on a defined

range
like "A1:A10". But how can I code the macro to select the range by first

and
last cells with data? This is my code so far...

Sub Macro1()

'1. Column reference will always be constant, rows will be variable
'2. Find first non-zero length cell
'3. Find the last cell with data
'4. Select range
'5. Make each value in selected range a working hyperlink


Dim FirstRow As Long
Dim LastRow As Long
FirstRow = Worksheets("Sheet1").Range("a1").End(xlDown).Row + 1 ' this
will give you the row of the 1st blank cell in A
LastRow = Worksheets("Sheet1").Range("a65536").End(xlUp).Row + 1 ' this
will give you the row of the last blank cell in A


Dim myRange As Range
Dim myVar As Range

'This is where I ran into trouble...
Set myRange = Range("A" & "'" & blankrow & "'" & ":A" & "'" & myvalue &

"'"
& ") 'set range

myRange.Select


'Microsoft KB271856
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell

End Sub

--
Dennis



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to define a range based on first and last cells with data?


Dennis

You can try Tony's suggestion.

or you can try this which fixes the error in your code.

Please note that you do not need to select a workbook, worksheet, range
etc to work with them - some example listed below your macro

Sub Macro1()

'1. Column reference will always be constant, rows will be variable
'2. Find first non-zero length cell
'3. Find the last cell with data
'4. Select range
'5. Make each value in selected range a working hyperlink


Dim FirstRow As Long
Dim LastRow As Long
FirstRow = Worksheets("Sheet1").Range("a1").End(xlDown).Row + 1 ' this
will give you the row of the 1st blank cell in A
LastRow = Worksheets("Sheet1").Range("a65536").End(xlUp).Row + 1 '
this
will give you the row of the last blank cell in A


Dim myRange As Range
Dim myVar As Range

'This is where I ran into trouble...
Set myRange = Range("A" & FirstRow & ":A" & LastRow) 'set range


'Microsoft KB271856
For Each xCell In myRange
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell

End Sub


range("a1").value = "Hi"
workbooks("Book1").sheets(1).cells(1,1).value = "Hello"


--
mudraker
------------------------------------------------------------------------
mudraker's Profile: http://www.excelforum.com/member.php...fo&userid=2473
View this thread: http://www.excelforum.com/showthread...hreadid=517527



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 202
Default How to define a range based on first and last cells with data?

Thank you all for your help
--
Dennis


"Bob Phillips" wrote:


LastRow = Cells(Rows.Count, "A").End(xlUp).Row
FirstRow = Cells(1, "A").End(xlDown).Row
Set myRange = Range(Cells(FirstRow, "A"), Cells(LastRow, "A"))


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"Dennis" wrote in message
...
This is my first Excel macro. I can get this code to work on a defined

range
like "A1:A10". But how can I code the macro to select the range by first

and
last cells with data? This is my code so far...

Sub Macro1()

'1. Column reference will always be constant, rows will be variable
'2. Find first non-zero length cell
'3. Find the last cell with data
'4. Select range
'5. Make each value in selected range a working hyperlink


Dim FirstRow As Long
Dim LastRow As Long
FirstRow = Worksheets("Sheet1").Range("a1").End(xlDown).Row + 1 ' this
will give you the row of the 1st blank cell in A
LastRow = Worksheets("Sheet1").Range("a65536").End(xlUp).Row + 1 ' this
will give you the row of the last blank cell in A


Dim myRange As Range
Dim myVar As Range

'This is where I ran into trouble...
Set myRange = Range("A" & "'" & blankrow & "'" & ":A" & "'" & myvalue &

"'"
& ") 'set range

myRange.Select


'Microsoft KB271856
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell

End Sub

--
Dennis




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 define a charts range based on the value of a cell chris Excel Discussion (Misc queries) 1 December 2nd 09 04:34 PM
Re-define a range in cells Geoff Excel Programming 0 June 1st 05 01:00 AM
Define a range based on another named range Basil Excel Worksheet Functions 2 February 21st 05 01:47 PM
Define Range based on cell color StephanieH Excel Programming 6 December 15th 04 04:24 PM
Define a Dynamic Range Based on an Index Mike Roberto Excel Programming 4 August 5th 04 02:02 PM


All times are GMT +1. The time now is 12:53 AM.

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

About Us

"It's about Microsoft Excel"