Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Range as cell numbers

I have this line of code and I want to remove the hard-coded "N" and replace
it with the last column (lngMaxColumn retruned by SpecialCells).

objExcel.range("A1:N" & lngMaxRow).Sort Key1:="Date", Header:=xlYes

I have a routine which turns the column number into a letter, but I'd rather
use the column number if possible. I know I can change Range("N1") to
Cells(14,1), but how do you specify a range of cells in this way?

I've tried

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort Key1:="Date",
Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.

Excel VBA Help gives Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True as
an example, but I can't figure out how to adapt this to late-bound code. It
needs to be late-bound as it's running under Access.

Any ideas?

Ian


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Range as cell numbers

What type of object is objExcel? I would assume it is a worksheet? That being
the case I do not see anything specifically wrong with your code. That
however being said getting the last cell from specialcells can be an issue.
It is not alway what you would think it is. Here is a function that is
guaranteed to get the the true last populated cell...

Public Function LastCell(Optional ByVal wks As Worksheet) As Range
Dim lngLastRow As Long
Dim intLastColumn As Integer

If wks Is Nothing Then Set wks = ActiveSheet
On Error Resume Next
lngLastRow = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
intLastColumn = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
If lngLastRow = 0 Then
lngLastRow = 1
intLastColumn = 1
End If
Set LastCell = wks.Cells(lngLastRow, intLastColumn)
End Function

You could use this function something like this...

With objExcel
.range(.range("A1"), LastCell(objExcel)).Sort Key1:="Date",
Header:=xlYes
End With

--
HTH...

Jim Thomlinson


"Ian" wrote:

I have this line of code and I want to remove the hard-coded "N" and replace
it with the last column (lngMaxColumn retruned by SpecialCells).

objExcel.range("A1:N" & lngMaxRow).Sort Key1:="Date", Header:=xlYes

I have a routine which turns the column number into a letter, but I'd rather
use the column number if possible. I know I can change Range("N1") to
Cells(14,1), but how do you specify a range of cells in this way?

I've tried

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort Key1:="Date",
Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.

Excel VBA Help gives Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True as
an example, but I can't figure out how to adapt this to late-bound code. It
needs to be late-bound as it's running under Access.

Any ideas?

Ian



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Range as cell numbers

Hi Jim

What type of object is objExcel? I would assume it is a worksheet? That
being
the case I do not see anything specifically wrong with your code. That

Yes, it's a worksheet.
however being said getting the last cell from specialcells can be an
issue.


I realise Excel sometimes lies about what the last occupied cell is,
particularly if data has been entered, then deleted. In this case, it's not
an issue as the spreadsheet is created from scratch by output from Access.

It is not alway what you would think it is. Here is a function that is
guaranteed to get the the true last populated cell...


I'll file your routine away for future use. Thank you.

Ian


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Range as cell numbers

On 23 Nov., 18:38, "Ian" wrote:
I have this line of code and I want to remove the hard-coded "N" and replace
it with the last column (lngMaxColumn retruned by SpecialCells).

objExcel.range("A1:N" & lngMaxRow).Sort Key1:="Date", Header:=xlYes

I have a routine which turns the column number into a letter, but I'd rather
use the column number if possible. I know I can change Range("N1") to
Cells(14,1), but how do you specify a range of cells in this way?

I've tried

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort Key1:="Date",
Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.

Excel VBA Help gives Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True as
an example, but I can't figure out how to adapt this to late-bound code. It
needs to be late-bound as it's running under Access.

Any ideas?

Ian


Hi Ian

Try to remove the dots before "Cells..."

With objExcel
.Range(Cells(1, 1), Cells(lngMaxRow, lngMaxColumn)).Sort
Key1:="Date", _
Header:=xlYes
End With

Regards,

Per
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Range as cell numbers

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort
Key1:="Date",
Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.


Try to remove the dots before "Cells..."

I get "Sub or Function not defined" as Access doesn't understand "Cells"

Ian




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Range as cell numbers

You can use Resize to create the range you want. I would think this will do
what you want...

objExcel.Range("A1").Resize(lngMaxRow, 14).Sort Key1:="Date", Header:=xlYes

You could use Cells(1, 1) in place of the Range("A1")... this would allow
you to use variables in order to specify the initial cell from which you
perform the resize instead.

Rick


"Ian" wrote in message
...
I have this line of code and I want to remove the hard-coded "N" and
replace it with the last column (lngMaxColumn retruned by SpecialCells).

objExcel.range("A1:N" & lngMaxRow).Sort Key1:="Date", Header:=xlYes

I have a routine which turns the column number into a letter, but I'd
rather use the column number if possible. I know I can change Range("N1")
to Cells(14,1), but how do you specify a range of cells in this way?

I've tried

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort
Key1:="Date", Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.

Excel VBA Help gives Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True as
an example, but I can't figure out how to adapt this to late-bound code.
It needs to be late-bound as it's running under Access.

Any ideas?

Ian


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Range as cell numbers

Boy, do I feel stupid!!!

You can use Resize to create the range you want. I would think this will
do what you want...

objExcel.Range("A1").Resize(lngMaxRow, 14).Sort Key1:="Date",
Header:=xlYes


This works perfectly well until I substitute 14 with lngMaxColumn. Major
hint here, I think.

I am working with data exported to several spreadsheets from Access and
different subroutines work on different ones.

I'd missed finding lngMaxColumn in the subroutine I was having problems
with.

All works perfectly, now!!

Ian


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Range as cell numbers

Hi Rick

Is it possible to create a range variable from Resize?

strRange = .Cells(1,1).Resize(lngMaxRow, lngMaxColumn) gives Type mismatch.

Alternatively, how could I achieve this using Cells, rather than Range

strRange = .Range(.Cells(1, 1), .Cells(lngMaxRow, lngMaxColumn)) also gives
Type mismatch


"Rick Rothstein (MVP - VB)" wrote in
message ...
You can use Resize to create the range you want. I would think this will
do what you want...

objExcel.Range("A1").Resize(lngMaxRow, 14).Sort Key1:="Date",
Header:=xlYes

You could use Cells(1, 1) in place of the Range("A1")... this would allow
you to use variables in order to specify the initial cell from which you
perform the resize instead.

Rick


"Ian" wrote in message
...
I have this line of code and I want to remove the hard-coded "N" and
replace it with the last column (lngMaxColumn retruned by SpecialCells).

objExcel.range("A1:N" & lngMaxRow).Sort Key1:="Date", Header:=xlYes

I have a routine which turns the column number into a letter, but I'd
rather use the column number if possible. I know I can change Range("N1")
to Cells(14,1), but how do you specify a range of cells in this way?

I've tried

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort
Key1:="Date", Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.

Excel VBA Help gives Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True
as an example, but I can't figure out how to adapt this to late-bound
code. It needs to be late-bound as it's running under Access.

Any ideas?

Ian




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Range as cell numbers

What is the 'dot' doing in front of the keyword Cells? Do you have a With
statement somewhere?

Anyway, to see that you can create a range using Resize, copy/paste and then
run the following code...

Sub Test()
Dim R As Range
Set R = Cells(1, 1).Resize(10, 10)
R.Value = "I'm yellow."
R.Interior.Color = vbYellow
End Sub

It will fill the first 10 columns and rows with the words "I'm yellow" and
make the cell's interiors yellow.

Rick



"Ian" wrote in message
...
Hi Rick

Is it possible to create a range variable from Resize?

strRange = .Cells(1,1).Resize(lngMaxRow, lngMaxColumn) gives Type
mismatch.

Alternatively, how could I achieve this using Cells, rather than Range

strRange = .Range(.Cells(1, 1), .Cells(lngMaxRow, lngMaxColumn)) also
gives Type mismatch


"Rick Rothstein (MVP - VB)" wrote in
message ...
You can use Resize to create the range you want. I would think this will
do what you want...

objExcel.Range("A1").Resize(lngMaxRow, 14).Sort Key1:="Date",
Header:=xlYes

You could use Cells(1, 1) in place of the Range("A1")... this would allow
you to use variables in order to specify the initial cell from which you
perform the resize instead.

Rick


"Ian" wrote in message
...
I have this line of code and I want to remove the hard-coded "N" and
replace it with the last column (lngMaxColumn retruned by SpecialCells).

objExcel.range("A1:N" & lngMaxRow).Sort Key1:="Date", Header:=xlYes

I have a routine which turns the column number into a letter, but I'd
rather use the column number if possible. I know I can change
Range("N1") to Cells(14,1), but how do you specify a range of cells in
this way?

I've tried

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort
Key1:="Date", Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.

Excel VBA Help gives Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True
as an example, but I can't figure out how to adapt this to late-bound
code. It needs to be late-bound as it's running under Access.

Any ideas?

Ian





  #10   Report Post  
Posted to microsoft.public.excel.programming
Ian Ian is offline
external usenet poster
 
Posts: 238
Default Range as cell numbers

Hi Rick

The code is running under Access VBA. The Excel application is defined as
objExcel and, as you suggested there is a preceding With statement.

The problem I have with your suggested code the first line. Under Access I
can not dimension as a range.

I've managed to get it working with:

Dim objRange as Object
Set objRange = objExcel.range(.Cells(3, lngMaxColum), .Cells(lngMaxRow,
lngMaxColumn))

Thanks for the pointer, I hadn't thought to use Set.

--
Ian
--
"Rick Rothstein (MVP - VB)" wrote in
message ...
What is the 'dot' doing in front of the keyword Cells? Do you have a With
statement somewhere?

Anyway, to see that you can create a range using Resize, copy/paste and
then run the following code...

Sub Test()
Dim R As Range
Set R = Cells(1, 1).Resize(10, 10)
R.Value = "I'm yellow."
R.Interior.Color = vbYellow
End Sub

It will fill the first 10 columns and rows with the words "I'm yellow" and
make the cell's interiors yellow.

Rick



"Ian" wrote in message
...
Hi Rick

Is it possible to create a range variable from Resize?

strRange = .Cells(1,1).Resize(lngMaxRow, lngMaxColumn) gives Type
mismatch.

Alternatively, how could I achieve this using Cells, rather than Range

strRange = .Range(.Cells(1, 1), .Cells(lngMaxRow, lngMaxColumn)) also
gives Type mismatch


"Rick Rothstein (MVP - VB)" wrote in
message ...
You can use Resize to create the range you want. I would think this will
do what you want...

objExcel.Range("A1").Resize(lngMaxRow, 14).Sort Key1:="Date",
Header:=xlYes

You could use Cells(1, 1) in place of the Range("A1")... this would
allow you to use variables in order to specify the initial cell from
which you perform the resize instead.

Rick


"Ian" wrote in message
...
I have this line of code and I want to remove the hard-coded "N" and
replace it with the last column (lngMaxColumn retruned by SpecialCells).

objExcel.range("A1:N" & lngMaxRow).Sort Key1:="Date", Header:=xlYes

I have a routine which turns the column number into a letter, but I'd
rather use the column number if possible. I know I can change
Range("N1") to Cells(14,1), but how do you specify a range of cells in
this way?

I've tried

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort
Key1:="Date", Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.

Excel VBA Help gives Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True
as an example, but I can't figure out how to adapt this to late-bound
code. It needs to be late-bound as it's running under Access.

Any ideas?

Ian









  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Range as cell numbers

Since I don't use Access, I'm glad your noticing my having used Set helped
you solve your problem as I wouldn't have what to tell you otherwise. For
your future consideration, the Set keyword is always used when assigning an
object reference to a variable or property.

Rick


"Ian" wrote in message
...
Hi Rick

The code is running under Access VBA. The Excel application is defined as
objExcel and, as you suggested there is a preceding With statement.

The problem I have with your suggested code the first line. Under Access I
can not dimension as a range.

I've managed to get it working with:

Dim objRange as Object
Set objRange = objExcel.range(.Cells(3, lngMaxColum), .Cells(lngMaxRow,
lngMaxColumn))

Thanks for the pointer, I hadn't thought to use Set.

--
Ian
--
"Rick Rothstein (MVP - VB)" wrote in
message ...
What is the 'dot' doing in front of the keyword Cells? Do you have a With
statement somewhere?

Anyway, to see that you can create a range using Resize, copy/paste and
then run the following code...

Sub Test()
Dim R As Range
Set R = Cells(1, 1).Resize(10, 10)
R.Value = "I'm yellow."
R.Interior.Color = vbYellow
End Sub

It will fill the first 10 columns and rows with the words "I'm yellow"
and make the cell's interiors yellow.

Rick



"Ian" wrote in message
...
Hi Rick

Is it possible to create a range variable from Resize?

strRange = .Cells(1,1).Resize(lngMaxRow, lngMaxColumn) gives Type
mismatch.

Alternatively, how could I achieve this using Cells, rather than Range

strRange = .Range(.Cells(1, 1), .Cells(lngMaxRow, lngMaxColumn)) also
gives Type mismatch


"Rick Rothstein (MVP - VB)" wrote in
message ...
You can use Resize to create the range you want. I would think this
will do what you want...

objExcel.Range("A1").Resize(lngMaxRow, 14).Sort Key1:="Date",
Header:=xlYes

You could use Cells(1, 1) in place of the Range("A1")... this would
allow you to use variables in order to specify the initial cell from
which you perform the resize instead.

Rick


"Ian" wrote in message
...
I have this line of code and I want to remove the hard-coded "N" and
replace it with the last column (lngMaxColumn retruned by
SpecialCells).

objExcel.range("A1:N" & lngMaxRow).Sort Key1:="Date",
Header:=xlYes

I have a routine which turns the column number into a letter, but I'd
rather use the column number if possible. I know I can change
Range("N1") to Cells(14,1), but how do you specify a range of cells in
this way?

I've tried

With objExcel
.range(.cells(1, 1), .cells(lngMaxRow, lngMaxColumn)).Sort
Key1:="Date", Header:=xlYes
End With

but this returns "Run-time error '1004': Application defined or object
defined error" and appears to be to do with the .cells references.

Excel VBA Help gives Range(Cells(1, 1), Cells(5, 3)).Font.Italic =
True as an example, but I can't figure out how to adapt this to
late-bound code. It needs to be late-bound as it's running under
Access.

Any ideas?

Ian








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
Dealing with range of numbers in a cell Penny Excel Worksheet Functions 5 May 4th 06 08:20 PM
sum a range of numbers entered into a cell paphos2006 New Users to Excel 3 March 2nd 06 10:38 PM
Range of numbers in a single cell DRB Excel Discussion (Misc queries) 2 January 12th 06 12:18 AM
occurance of numbers in cell range Dillenger Excel Worksheet Functions 14 February 21st 05 06:45 PM
Sum the first 10 numbers in a 26 cell range jester[_2_] Excel Programming 6 June 19th 04 04:33 PM


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