Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 143
Default <Method 'Range' of object '_Global' failed error

When I run this macro,

Sub addLeadType(leadType As Integer)
Dim totalrows As Integer
Dim typeCol As Integer

typeCol = 11

' The following line generates the error
Range(Cells(1, typeCol)).Value = "Lead Type"


totalrows = ActiveSheet.UsedRange.Rows.count
Range(Cells(2, typeCol), Cells(totalrows, typeCol)).Value = leadType
End Sub

I get a "<Method 'Range' of object '_Global' failed error at the line
indicated above.

I tried hard coding the "11" in place of typeCol and get the same error.

Can someone tell me what I am doing wrong here?

Thanks,
Ken Loomis




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default <Method 'Range' of object '_Global' failed error

Invalid syntax

Range(Cells(1, typeCol)).Value = "Lead Type"

should be

Cells(1, typeCol).Value = "Lead Type"


Range(rng reference, rng reference) works, but Range(rng reference)
doesn't (and is redundant).

--
Regards,
Tom Ogilvy

"Ken Loomis" wrote in message
...
When I run this macro,

Sub addLeadType(leadType As Integer)
Dim totalrows As Integer
Dim typeCol As Integer

typeCol = 11

' The following line generates the error
Range(Cells(1, typeCol)).Value = "Lead Type"


totalrows = ActiveSheet.UsedRange.Rows.count
Range(Cells(2, typeCol), Cells(totalrows, typeCol)).Value = leadType
End Sub

I get a "<Method 'Range' of object '_Global' failed error at the line
indicated above.

I tried hard coding the "11" in place of typeCol and get the same error.

Can someone tell me what I am doing wrong here?

Thanks,
Ken Loomis






  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default <Method 'Range' of object '_Global' failed error

Hi
You can use Cells without Range like this

Cells(1, typeCol).Value = "Lead Type"

This refers to cells on the active sheet, so to be safe you might want
to use

ActiveSheet.Cells(1, typeCol).Value = "Lead Type"

or if your sheet is not active use

Worksheets("MySheetName").Cells(1, typeCol).Value = "Lead Type"

You can also use Cells to reference a location in a Range like this

MyRange.Cells(1, typeCol).Value = "Lead Type"

this will now refer to the (1, typeCol) entry in MyRange.

Your further syntax is OK

Range(Cells(2, typeCol), Cells(totalrows, typeCol)).Value = leadType

which again refers to a Range on the Active Sheet. Qualification is
then most efficiently done with With...End With
With ActiveSheet
Range(.Cells(2, typeCol), .Cells(totalrows, typeCol)).Value =
leadType
End With

regards
Paul


"Ken Loomis" wrote in message ...
When I run this macro,

Sub addLeadType(leadType As Integer)
Dim totalrows As Integer
Dim typeCol As Integer

typeCol = 11

' The following line generates the error
Range(Cells(1, typeCol)).Value = "Lead Type"


totalrows = ActiveSheet.UsedRange.Rows.count
Range(Cells(2, typeCol), Cells(totalrows, typeCol)).Value = leadType
End Sub

I get a "<Method 'Range' of object '_Global' failed error at the line
indicated above.

I tried hard coding the "11" in place of typeCol and get the same error.

Can someone tell me what I am doing wrong here?

Thanks,
Ken Loomis

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
Method 'Range' of object '_global' failed higherlimits Excel Discussion (Misc queries) 3 June 23rd 06 06:16 PM
runtime error 1004 method range of object '_global failed valdesd Excel Discussion (Misc queries) 2 October 6th 05 07:26 PM
Method 'Range' of object '_Global' failed Brian Morris[_2_] Excel Programming 1 December 10th 03 11:04 PM
"Run-time error '1004'" Method 'Range' of object '_global' failed. haisat[_2_] Excel Programming 0 October 20th 03 12:13 PM
Method 'Range' of object '_Global' failed Mohanasundaram[_2_] Excel Programming 1 August 25th 03 01:43 PM


All times are GMT +1. The time now is 02:31 PM.

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"