|
-
Aug 23rd, 2005, 09:09 PM
#1
Thread Starter
Junior Member
Need Help on selecting using vba on excel
Currently i uses the range to select all information in the excel file that i open.
But i realize that the range is always changing. I need to select all and not the range. Any Ideas?
Public Sub SortRange1()
Dim range1 As Object
range1 = xlSheet.Range("A2:AO872")
range1.Sort(Key1:=range1.Columns(1), order1:=Excel.XlSortOrder.xlAscending)
End Sub
-
Aug 23rd, 2005, 11:58 PM
#2
Re: Need Help on selecting using vba on excel
It's been a while, but I believe there is a usedrange property. If you don't need the data from every colomn, you can use .Range("A2:A" & usedrange.rows.count)
-
Aug 25th, 2005, 12:24 AM
#3
Thread Starter
Junior Member
Re: Need Help on selecting using vba on excel
I need the data from every column, what happen now is my counter in the main program.
For count = 2 To 437
it's correct that it start counting from 2 but the ending rows. is different because there is different file and has different ending rows so how can i let it detect the last row in excel.
-
Aug 25th, 2005, 01:07 AM
#4
Re: Need Help on selecting using vba on excel
Moved to VBA Forum
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 25th, 2005, 08:15 AM
#5
Frenzied Member
Re: Need Help on selecting using vba on excel
The inspiration for this came from a forum, but I've lost the reference. Thanks be to whoever posted the original code. I hope this helps, even if you just use the standard "UsedRange" function.
MODIFIED 8/25/2005 9:47 AM edt
Code:
'========================================================================
' **********************************************************************
'
' FUNCTION "ActualUsedRange"
'
'This function returns the ACTUAL 'UsedRange' of cells from "A1" to the
'cell intersection of the last non-empty Row and the last non-empty Column.
'If the Sheet is empty, the function returns "Nothing" as the range.
'This function does NOT count any formatted but otherwise empty cells.
'Cells with only non-printing characters appear blank, but are actually not.
'This is a replacement for the buggy Excel 'UsedRange' function, which
'occasionally returns a range bigger than the actual Used Range. NOTE: This
'could take a LONG time to run!
'
'The return of "Nothing" can be detected in the calling code with the following:
' Dim myReturnedRange as Range
' Dim mySheet as Worksheet
' Set mySheet = ActiveSheet
' Set myReturnedRange = ActualUsedRange(mySheet)
' If myReturnedRange Is Nothing Then ...
'
' **********************************************************************
Function ActualUsedRange(anySht As Worksheet) As Range
Dim i As Long ' Loop Index
Dim c As Integer ' Column number
Dim r As Long ' Row number
'Use Excel 'UsedRange' as an inclusive estimate
With anySht.UsedRange
'Find the total number of COLUMNS
i = .Cells(.Cells.Count).Column
if i < 256 then i = i + 1
'Starting from the RIGHT, eliminate empty columns
For c = i To 1 Step -1
If Application.CountA(anySht.Columns(c)) > 0 Then Exit For
Next c
'Find the total number of ROWS
i = .Cells(.Cells.Count).Row
if i < 65536 then i = i + 1
'Starting from the BOTTOM, eliminate empty rows
For r = i To 1 Step -1
If Application.CountA(anySht.Rows(r)) > 0 Then Exit For
Next r
End With
'Set the return value with the ACTUAL range from $A$1 to the REAL 'Last Cell'
If (r = 0 And c = 0) Then
'Return 'Nothing' for an empty sheet
Set ActualUsedRange = Nothing
Else
'Return the Actual Used Range determined for this sheet
Set ActualUsedRange = anySht.Range(anySht.Cells(1, 1), anySht.Cells(r, c))
End If
End Function
'
Last edited by Webtest; Aug 25th, 2005 at 08:50 AM.
Reason: Overflow Errors in Code
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Aug 25th, 2005, 08:51 AM
#6
Frenzied Member
Re: Need Help on selecting using vba on excel
ALERT: I modified the code (Bolded lines) in the previous post.
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Aug 28th, 2005, 09:43 PM
#7
Thread Starter
Junior Member
Re: Need Help on selecting using vba on excel
i put your code in my class, a few coding seem to me that it has error. like
both Application.CountA and anySht.Range(anySht.Cells(1, 1), anySht.Cells(r, c))
Thanks for the help, thanks alot
-
Aug 29th, 2005, 08:23 AM
#8
Frenzied Member
Re: Need Help on selecting using vba on excel
What version of Excel are you running? This works fine for me in Excel '97 and 2002.
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Aug 29th, 2005, 08:31 PM
#9
Thread Starter
Junior Member
Re: Need Help on selecting using vba on excel
The version of excel i am using is 2000
-
Aug 29th, 2005, 08:34 PM
#10
Thread Starter
Junior Member
Re: Need Help on selecting using vba on excel
i used your code in my class file
Function ActualUsedRange(ByVal anySht As Excel.Worksheet) As Excel.Range()
Dim i As Long ' Loop Index
Dim c As Integer ' Column number
Dim r As Long ' Row number
'Use Excel 'UsedRange' as an inclusive estimate
With anySht.UsedRange
'Find the total number of COLUMNS
i = .Cells(.Cells.Count).Column
If i < 256 Then i = i + 1
'Starting from the RIGHT, eliminate empty columns
For c = i To 1 Step -1
If xlSheet.Application.CountA(anySht.Columns(c)) > 0 Then Exit For
Next c
'Find the total number of ROWS
i = .Cells(.Cells.Count).Row
If i < 65536 Then i = i + 1
'Starting from the BOTTOM, eliminate empty rows
For r = i To 1 Step -1
If xlSheet.Application.CountA(anySht.Rows(r)) > 0 Then Exit For
Next r
End With
'Set the return value with the ACTUAL range from $A$1 to the REAL 'Last Cell'
If (r = 0 And c = 0) Then
'Return 'Nothing' for an empty sheet
ActualUsedRange = Nothing
Else
'Return the Actual Used Range determined for this sheet
ActualUsedRange = anySht.excel.Range(anySht.Cells(1, 1), anySht.Cells(r, c))
End If
End Function
The question is how can i call the function in my main class, there is an error when i call it. With this line. appExcel is the class file i write the function. It say must pass in argument but wat argument should i really pass in?
appExcel.ActualUsedRange()
-
Aug 30th, 2005, 08:21 AM
#11
Frenzied Member
Re: Need Help on selecting using vba on excel
Eddie:
A sample call using the currently Active Worksheet is right there in the original code ...
Code:
' Dim myReturnedRange as Range
' Dim mySheet as Worksheet
' Set mySheet = ActiveSheet
' Set myReturnedRange = ActualUsedRange(mySheet)
' If myReturnedRange Is Nothing Then ...
Also, PUT MY FUNCTION BACK THE WAY I ORIGINALLY PROVIDED IT!!! Play with this function in a new workbook. Record a new macro, and then just click the stop button. It should be Macro1 (or whatever). Edit the macro, taking everything out between the Sub() and End Sub lines, and insert the following:
Code:
Dim myReturnedRange as Range
Dim mySheet as Worksheet
Set mySheet = ActiveSheet
Set myReturnedRange = ActualUsedRange(mySheet)
If myReturnedRange Is Nothing Then
MsgBox "The Active Sheet is EMPTY!"
Else
MsgBox "The actual Used Range is " & myReturnedRange.Address
End If
Copy the ORIGINAL ActualUsedRange function right after the new Macro Sub, and at the very top of the Module, put "Option Explicit". Now play with it in a simple Macro to make sure it works BEFORE you try to cram it into your application.
This function runs correctly in Excel '97 and Excel 2002 ... I cut and pasted it right out of the Forum, but I had to back out some of your changes from the following 5 lines of code. Here are the CORRECTED lines:
Code:
Function ActualUsedRange(anySht As Excel.Worksheet) As Excel.Range
...
If Application.CountA(anySht.Columns(c)) > 0 Then Exit For '<<<
...
If Application.CountA(anySht.Rows(r)) > 0 Then Exit For '<<<
...
Set ActualUsedRange = Nothing '<<<
...
Set ActualUsedRange = anySht.Range(anySht.Cells(1, 1), anySht.Cells(r, c)) '<<<<
...
Bad Newbie Vibes ... see next post ...
With respect to your changes to the Function statement ... You can NOT pass a reference to a sheet "ByVal" ... think about it ... What is the 'Value' of a Sheet? You want to examine the ACTUAL Sheet, so you need a direct reference to it ... not the 'Value' of it (whatever that means?).
Keep plugging away, but try to test things in small increments until you understand what is going on. Hey, I'm a newbie too, believe it or not!
Good luck and good learning!
Last edited by Webtest; Aug 30th, 2005 at 08:41 AM.
Reason: Bad Newbie Comment!
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Aug 30th, 2005, 08:40 AM
#12
Frenzied Member
Re: Need Help on selecting using vba on excel
Hey ... I told you I was a Newbie! You CAN use a 'ByVal' reference to a Worksheet! The following works ...
Code:
Option Explicit
Sub Macro1()
JUNK(ActiveSheet).Select
End Sub
Function JUNK(ByVal aSheet As Worksheet) As Range
Set JUNK = aSheet.Range("C5:D7")
End Function
You'll have to get someone better than I to explain the difference, but I suspect that 'ByVal' operates on a Copy of the sheet, where without it you are referencing the original sheet ???
Humble Pie for lunch today for sure!
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Sep 4th, 2005, 11:31 PM
#13
Thread Starter
Junior Member
Re: Need Help on selecting using vba on excel
Sorry again, can i know what is the Application.CountA do? CountA issit a variable?
-
Sep 4th, 2005, 11:44 PM
#14
Re: Need Help on selecting using vba on excel
Actually countA is a worksheet function.
 Originally Posted by Excel Helpfile
Counts the number of cells that are not empty and the values within the list of arguments. Use COUNTA to count the number of cells that contain data in a range or array.
Syntax
COUNTA(value1,value2,...)
Value1, value2, ... are 1 to 30 arguments representing the values you want to count. In this case, a value is any type of information, including empty text ("") but not including empty cells. If an argument is an array or reference, empty cells within the array or reference are ignored. If you do not need to count logical values, text, or error values, use the COUNT function.
HTH
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|