Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.newusers
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.newusers
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.newusers
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.newusers
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Additional raws only in some region | Excel Discussion (Misc queries) | |||
Counting a Region | Excel Worksheet Functions | |||
VBA determine computer region | Setting up and Configuration of Excel | |||
Region | Excel Discussion (Misc queries) | |||
Is it possible to count if within a number region? | Excel Discussion (Misc queries) |