Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 2
Default how to find used region with macro

Hi,

sorry if this obvious or has been asked recently.

I have a macro that goes through all rows and all columns in a
selection. If a row or a column is hidden, the font of that row or
column is changed.

A friend wants to use the macro in a Workbook with many sheets. He
asked if I could change it so that he does not need to do a selection
on every sheet before he runs it.

I imagine that a nested loop through all rows and all columns will
take a long time?

I´ve tried to figure out how to get the range defined by the last row
and the last column filled with data.

First I tried with CurrentRegion but then I tried End(xlUP) but the
code I´ve come up with is not reliable. I get strange results.

I would be most thankful for any hint.

BR,
Oerjan Skogloesa
=============

Sub Makro1()
Dim Sh As Worksheet
Dim myColumn As Range
Dim myRange As Range
Dim A, B, C, D, HiRow, HiCol

For Each Sh In ActiveWorkbook.Worksheets

A = Sh.Rows.Count
HiRow = 1

For Each myColumn In Sh.Columns

B = myColumn.Column
C = (Cells(A, B).Address())

D = Range(C).End(xlUp).Row

If D HiRow Then HiRow = D

If D 1 Then HiCol = B

Next myColumn

'this is just for seeing the result
With Sh
..Activate
..Range(.Cells(1, 1), .Cells(HiRow, HiCol)).Select
End With

MsgBox (HiRow & " " & HiCol)

Next Sh

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 2,510
Default how to find used region with macro

Your quote "I tried with CurrentRegion". I feel sure that it should work.
Perhaps you were not using it correctly.

Try the following:-

Range("C8").CurrentRegion.Select

You can use any cell in lieu of Cell C8 and it can be any cell within the
range that is not bound by blank cells and it will select all the range
around it until it comes to blank rows and columns surrounding the data.

--
Regards,

OssieMac


"Oerjan" wrote:

Hi,

sorry if this obvious or has been asked recently.

I have a macro that goes through all rows and all columns in a
selection. If a row or a column is hidden, the font of that row or
column is changed.

A friend wants to use the macro in a Workbook with many sheets. He
asked if I could change it so that he does not need to do a selection
on every sheet before he runs it.

I imagine that a nested loop through all rows and all columns will
take a long time?

I´ve tried to figure out how to get the range defined by the last row
and the last column filled with data.

First I tried with CurrentRegion but then I tried End(xlUP) but the
code I´ve come up with is not reliable. I get strange results.

I would be most thankful for any hint.

BR,
Oerjan Skogloesa
=============

Sub Makro1()
Dim Sh As Worksheet
Dim myColumn As Range
Dim myRange As Range
Dim A, B, C, D, HiRow, HiCol

For Each Sh In ActiveWorkbook.Worksheets

A = Sh.Rows.Count
HiRow = 1

For Each myColumn In Sh.Columns

B = myColumn.Column
C = (Cells(A, B).Address())

D = Range(C).End(xlUp).Row

If D HiRow Then HiRow = D

If D 1 Then HiCol = B

Next myColumn

'this is just for seeing the result
With Sh
..Activate
..Range(.Cells(1, 1), .Cells(HiRow, HiCol)).Select
End With

MsgBox (HiRow & " " & HiCol)

Next Sh

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 5,934
Default how to find used region with macro

Try the worksheet's UsedRange.

--
Rick (MVP - Excel)


"Oerjan" wrote in message
...
Hi,

sorry if this obvious or has been asked recently.

I have a macro that goes through all rows and all columns in a
selection. If a row or a column is hidden, the font of that row or
column is changed.

A friend wants to use the macro in a Workbook with many sheets. He
asked if I could change it so that he does not need to do a selection
on every sheet before he runs it.

I imagine that a nested loop through all rows and all columns will
take a long time?

I´ve tried to figure out how to get the range defined by the last row
and the last column filled with data.

First I tried with CurrentRegion but then I tried End(xlUP) but the
code I´ve come up with is not reliable. I get strange results.

I would be most thankful for any hint.

BR,
Oerjan Skogloesa
=============

Sub Makro1()
Dim Sh As Worksheet
Dim myColumn As Range
Dim myRange As Range
Dim A, B, C, D, HiRow, HiCol

For Each Sh In ActiveWorkbook.Worksheets

A = Sh.Rows.Count
HiRow = 1

For Each myColumn In Sh.Columns

B = myColumn.Column
C = (Cells(A, B).Address())

D = Range(C).End(xlUp).Row

If D HiRow Then HiRow = D

If D 1 Then HiCol = B

Next myColumn

'this is just for seeing the result
With Sh
.Activate
.Range(.Cells(1, 1), .Cells(HiRow, HiCol)).Select
End With

MsgBox (HiRow & " " & HiCol)

Next Sh

End Sub



  #4   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 2
Default how to find used region with macro

On Fri, 12 Dec 2008 02:37:19 -0500, "Rick Rothstein"
wrote:

Try the worksheet's UsedRange.


Perfect.

Thank you very much.

Oerjan Skogloesa
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
Additional raws only in some region antares Excel Discussion (Misc queries) 10 November 30th 08 01:23 AM
Counting a Region Paul D Byrne. Excel Worksheet Functions 2 November 27th 08 03:01 AM
VBA determine computer region Viperv10 Setting up and Configuration of Excel 5 May 27th 07 01:39 AM
Region Zaahir Excel Discussion (Misc queries) 2 November 14th 06 02:22 PM
Is it possible to count if within a number region? themax16 Excel Discussion (Misc queries) 3 May 22nd 05 06:46 PM


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