Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Problem with "Range"

Using Excel 2002 SP3
This code works:
Sub Zloaddata()
phyfile = ThisWorkbook.Name
phypath = ThisWorkbook.Path & "\"
Workbooks(phyfile).Sheets("PHY").Activate
Workbooks(phyfile).Sheets("PHY").Range(Columns(1), Columns(250)).Delete

This code results in "Run-time error 1004 Application-defined or
Object-defined error"
Sub CommandButton3_Click()
phyfile = ThisWorkbook.Name
phypath = ThisWorkbook.Path & "\"
Workbooks(phyfile).Worksheets("PHY").Activate
ActiveWorkbook.Sheets("PHY").Range(Columns(1), Columns(250)).Delete

Why does the 2nd example result in the run-time error ?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Problem with "Range"

Because when you move the code into a sheet module, the unqualified

columns(1) and Columns(250) refer to the sheet containing the code rather
than the activesheet. You can fix it with this

Sub CommandButton3_Click()
phyfile = ThisWorkbook.Name
phypath = ThisWorkbook.Path & "\"
With Workbooks(phyfile).Worksheets("PHY")
.Range(.Columns(1), .Columns(250)).Delete
End With
End Sub

or

Sub CommandButton3_Click()
phyfile = ThisWorkbook.Name
phypath = ThisWorkbook.Path & "\"
With Workbooks(phyfile).Worksheets("PHY")
.Columns(1).Resize(,250).Delete
' or .columns("A:IP").Delete
End With
End Sub
--
Regards,
Tom Ogilvy


"Baruche" wrote in message
...
Using Excel 2002 SP3
This code works:
Sub Zloaddata()
phyfile = ThisWorkbook.Name
phypath = ThisWorkbook.Path & "\"
Workbooks(phyfile).Sheets("PHY").Activate
Workbooks(phyfile).Sheets("PHY").Range(Columns(1), Columns(250)).Delete

This code results in "Run-time error 1004 Application-defined or
Object-defined error"
Sub CommandButton3_Click()
phyfile = ThisWorkbook.Name
phypath = ThisWorkbook.Path & "\"
Workbooks(phyfile).Worksheets("PHY").Activate
ActiveWorkbook.Sheets("PHY").Range(Columns(1), Columns(250)).Delete

Why does the 2nd example result in the run-time error ?




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
Problem viewing back the "Unfiltered range" challa prabhu Excel Discussion (Misc queries) 4 August 14th 08 11:07 AM
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
problem with Linking workbooks via "copy" and "paste link" Arkitek Excel Discussion (Misc queries) 0 December 19th 06 10:03 PM
Using "Cells" to write "Range("A:A,H:H").Select" Trip Ives[_2_] Excel Programming 3 June 5th 04 03:13 PM


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