Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default What is wrong with this function?

Hello, I am using VBA in Excel XP with Windows 2000

The function below is designed to locate the last used column in the sheet. If the sheet is blank, the function should return the integer zero. The problem is when the sheet is blank it generates the error "Object variable or with block not set." I've looked at it so long I can't see what is wrong. NOTE: Lines 2 and 3 should be on the same line

Dim intColumn As Intege
intColumn = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Colum
If IsEmpty(intColumn) Then ColumnLastFilled =
If Not IsEmpty(intColumn) Then ColumnLastFilled = intColum

Thanks in advance

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 88
Default What is wrong with this function?

Hi Manasses,

Manasses wrote:
The function below is designed to locate the last used column in the sheet. If the sheet is blank, the function should return
the integer zero. The problem is when the sheet is blank it generates the error "Object variable or with block not set." I've
looked at it so long I can't see what is wrong. NOTE: Lines 2 and 3 should be on the same line.

Dim intColumn As Integer
intColumn = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByColumns,
SearchDirection:=xlPrevious).Column
If IsEmpty(intColumn) Then ColumnLastFilled = 0
If Not IsEmpty(intColumn) Then ColumnLastFilled = intColumn


try this:

On Error Resume Next
intColumn = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
ColumnLastFilled = IIf(Err 0, 0, intColumn)

--
Mit freundlichen Grüssen

Melanie Breden
- Microsoft MVP für Excel -

http://excel.codebooks.de (Das Excel-VBA Codebook)

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default What is wrong with this function?

found here
http://homepages.paradise.net.nz/~robree/excel/

Sub lastused()
Dim lngLastRow As Long, lngLastCol As Long

On Error Resume Next
lngLastRow = 1: lngLastCol = 1
With ActiveSheet.UsedRange
lngLastRow = .Find("*", .Cells(1), xlFormulas, _
xlWhole, xlByRows, xlPrevious).Row
lngLastCol = .Find("*", .Cells(1), xlFormulas, _
xlWhole, xlByColumns, xlPrevious).Column
.Cells(lngLastRow, lngLastCol).Select
End With
MsgBox lngLastRow
MsgBox lngLastCol
End Sub

--
Don Guillett
SalesAid Software

"Manasses" wrote in message
...
Hello, I am using VBA in Excel XP with Windows 2000.

The function below is designed to locate the last used column in the

sheet. If the sheet is blank, the function should return the integer zero.
The problem is when the sheet is blank it generates the error "Object
variable or with block not set." I've looked at it so long I can't see what
is wrong. NOTE: Lines 2 and 3 should be on the same line.

Dim intColumn As Integer
intColumn = ActiveSheet.UsedRange.Find(What:="*",

SearchOrder:=xlByColumns,
SearchDirection:=xlPrevious).Column
If IsEmpty(intColumn) Then ColumnLastFilled = 0
If Not IsEmpty(intColumn) Then ColumnLastFilled = intColumn

Thanks in advance.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default What is wrong with this function?

1.An Integer data type = 0 when initialized so it can't be Empty
2.Find returns a range object, if it not found, it returns Nothing , so your code is literally saying 'intColumn = Nothing.Column
test the Find first

Dim intColumn As Integer, x as rang
Set x =ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByColumns,SearchDirection:=xlPrevio us
If Not x Is Nothing Then intColumn = x.Colum
If intColumn= 0 Then ColumnLastFilled =
If Not intColumn= 0 Then ColumnLastFilled = intColum

----- Manasses wrote: ----

Hello, I am using VBA in Excel XP with Windows 2000

The function below is designed to locate the last used column in the sheet. If the sheet is blank, the function should return the integer zero. The problem is when the sheet is blank it generates the error "Object variable or with block not set." I've looked at it so long I can't see what is wrong. NOTE: Lines 2 and 3 should be on the same line

Dim intColumn As Intege
intColumn = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Colum
If IsEmpty(intColumn) Then ColumnLastFilled =
If Not IsEmpty(intColumn) Then ColumnLastFilled = intColum

Thanks in advance

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
what's wrong with this function? DogmaDot Excel Discussion (Misc queries) 1 February 24th 10 04:00 PM
What's wrong with this IF function. GEM Excel Discussion (Misc queries) 5 July 24th 09 11:51 PM
What is Wrong with this function?????????? TaraD Excel Worksheet Functions 2 September 8th 06 07:47 PM
What am I doing wrong with PMT function? Marc Excel Discussion (Misc queries) 6 May 6th 06 10:35 PM
What's Wrong With This Function? Ron Rosenfeld Excel Programming 3 October 23rd 03 04:40 PM


All times are GMT +1. The time now is 04:08 AM.

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"