If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > question on script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-23-09, 21:24
clinel clinel is offline
Registered User
 
Join Date: Oct 2002
Location: Western part of Georgia, USA
Posts: 123
question on script

Can someone help me understand why I get error 1004 from this script?
I am trying to find the first empty cell in colum A and select it so that I can paste additional data. It fails at the Range(KSB1_Hdr).Select

Dim KSB1_lastHdr As Integer
KSB1_lastHdr = ThisWorkbook.Worksheets(1).UsedRange.Rows.Count + 1
Dim KSB1_Hdr As String
KSB1_Hdr = "A" & KSB1_lastHdr & ""
Worksheets("KSB1").Select
Range(KSB1_Hdr).Select
Thanks,
Lee
Reply With Quote
  #2 (permalink)  
Old 04-24-09, 05:57
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi Lee,

Selecting objects can be very buggy in Excel. My first guess would be that your code is in a worksheet's class module (not the "KSB1" worksheet) which means that your unqualified range property will refer to the range on that worksheet rather than the "KSB1" worksheet. The range object is by design strictly 2 dimensional in the Excel Object Model, so if the named range "KSB1_Hdr" is on the KSB1 worksheet, but your range property is implicitly qualified to a different worksheet, you'll get an error.


If I've successfully identified the problem there are 3 workarounds:

1. You can qualify your range property:
Code:
Worksheets("KSB1").Select
Worksheets("KSB1").Range(KSB1_Hdr).Select
2. Leave the code the same but move your subroutine to a standard code module so the unqualified range property will reference the activesheet.


3. Best option!
The bottom line is that 99% of the time we don't need to select ranges - this normally just slows your code down and makes it prone to errors unless you code it very carefully. What do you intend to do with the range, we should be able to show you a better way of doing this?


Hope that helps....
Reply With Quote
  #3 (permalink)  
Old 04-30-09, 14:10
savbill savbill is offline
Registered User
 
Join Date: Feb 2004
Posts: 533
I think it is likely you are exceeding the limit of the "Integer" Data Type by the number of rows. So to start with you should Type "KBS1_hdr" varible as a Long number vs the integer.

Now you can do something like this to work better:
Code:
Dim nlastHdr As Long

' Set the number of the first blank row
nlastHdr = Worksheets("KSB1").UsedRange.Rows.Count + 1

Worksheets("KSB1").Activate

' Use the 'Cells' Method with the row/column value
Worksheets("KSB1").Cells(1, nlastHdr).Select
__________________
~

Bill
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On