Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default How to get the last valid (non blank) cell index?

I have data in 100 rows through 120 colunms. How can i get the index
100 and 120? I mean last valid data index.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default How to get the last valid (non blank) cell index?

Let's assume that your data is Activesheet.UsedRange:

Set r = ActiveSheet.UsedRange

nLastRow = r.Rows.Count + r.Row - 1
MsgBox ("last row " & nLastRow)

nLastColumn = r.Columns.Count + r.Column - 1
MsgBox ("last column " & nLastColumn)

nFirstRow = r.Row
MsgBox ("first row " & nFirstRow)

nFirstColumn = r.Column
MsgBox ("first column " & nFirstColumn)
--
Gary''s Student - gsnu2007d


" wrote:

I have data in 100 rows through 120 colunms. How can i get the index
100 and 120? I mean last valid data index.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default How to get the last valid (non blank) cell index?

you can return the adddress of the last used cell with this function

Function lastused()
lastused = Cells.Find(What:="*", After:=[A1], _
SearchDirection:=xlPrevious).Address
End Function

You call it with =lastused()

Mike

" wrote:

I have data in 100 rows through 120 colunms. How can i get the index
100 and 120? I mean last valid data index.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to get the last valid (non blank) cell index?

I have data in 100 rows through 120 colunms. How can i get
the index 100 and 120? I mean last valid data index.


Perhaps the subroutine listed after my signature is what you are after. You
would use it like this. Declare four variables (for the top row, bottom row,
left column and right column numbers). Here is a sample subroutine showing
how to use it...

Sub Test()
Dim TopRow As Long
Dim BottomRow As Long
Dim LeftColumn As Long
Dim RightColumn As Long
ActualUsedRange TopRow, BottomRow, LeftColumn, RightColumn
Debug.Print TopRow, BottomRow, LeftColumn, RightColumn
End Sub

Rick

Sub ActualUsedRange(StartRow, EndRow, StartColumn, EndColumn)
Dim X As Long
If VarType(StartRow) 6 Or VarType(EndRow) 6 Or _
VarType(StartColumn) 6 Or VarType(EndColumn) 6 Then
MsgBox "Numeric data types only!", vbCritical, "Data Type Error"
Exit Sub
End If
With ActiveSheet.UsedRange
StartRow = .Row + .Rows.Count - 1
StartColumn = .Column + .Columns.Count - 1
For X = .Row To .Row + .Rows.Count - 1
With ActiveSheet.Cells(X, Columns.Count)
If EndColumn < .End(xlToLeft).Column Then
EndColumn = .End(xlToLeft).Column
End If
End With
With ActiveSheet.Cells(X, 1)
If ActiveSheet.Cells(X, 1).Value < "" Then
StartColumn = 1
ElseIf StartColumn .End(xlToRight).Column Then
StartColumn = .End(xlToRight).Column
End If
End With
Next
For X = .Column To .Column + .Columns.Count - 1
With ActiveSheet.Cells(Rows.Count, X)
If EndRow < .End(xlUp).Row Then
EndRow = .End(xlUp).Row
End If
End With
With ActiveSheet.Cells(1, X)
If ActiveSheet.Cells(1, X).Value < "" Then
StartRow = 1
ElseIf StartRow .End(xlDown).Row Then
StartRow = .End(xlDown).Row
End If
End With
Next
End With
End Sub

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
Index/match - make blank cells return a blank value. diaare Excel Worksheet Functions 3 May 3rd 23 03:44 AM
Add if blank statement to index match Diddy Excel Worksheet Functions 6 November 3rd 09 09:56 PM
how to delete a row index when row is blank deepika :excel help[_2_] Excel Discussion (Misc queries) 3 February 6th 08 11:18 AM
Start Cell B1 then find first blank cell, insert subtotal, next non blank, then next blank, sutotal cells in between......... [email protected][_2_] Excel Programming 2 June 7th 07 09:27 PM
I want to index the first blank cell in a column of data Bruce Henson Excel Discussion (Misc queries) 3 February 23rd 06 08:07 PM


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