PC Review


Reply
Thread Tools Rate Thread

Cell range excluding blank returns

 
 
=?Utf-8?B?Q29uc3RhbnRseSBBbWF6ZWQ=?=
Guest
Posts: n/a
 
      19th Nov 2007
Hi

I have some code which is returning a starting point and end point for cells
which contain data or formulas in a spreadsheet. I want to limit this to
only cells which display data. My formulas concatenate data from an input
sheet but display "" on lines in which no data has been entered on the input
sheet.

With ActiveSheet.UsedRange
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With

I believ it is the UsedRange element that needs changing but I'm unsure what
it needs to be replaced with.

Thanks
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Nov 2007
What you are looking for is a bit more difficult than I think you anticipate.
What you need to do is to create a range that is the union of the constants
and formulas... Give this a try...

Public Sub test() 'Run me
Dim rng As Range

Set rng = GetNonBlanks(Sheets("Sheet1"))
If Not rng Is Nothing Then rng.Select

End Sub

Public Function GetNonBlanks(ByVal wks As Worksheet) As Range
Dim rngConstants As Range
Dim rngFormulas As Range

With wks.Cells
On Error Resume Next
Set rngConstants = .SpecialCells(xlCellTypeConstants)
Set rngFormulas = .SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
End With

If rngConstants Is Nothing And rngFormulas Is Nothing Then
Set GetNonBlanks = Nothing
ElseIf Not rngConstants Is Nothing And rngFormulas Is Nothing Then
Set GetNonBlanks = rngConstants
ElseIf rngConstants Is Nothing And Not rngFormulas Is Nothing Then
Set GetNonBlanks = rngFormulas
Else
Set GetNonBlanks = Union(rngConstants, rngFormulas)
End If

End Function
--
HTH...

Jim Thomlinson


"Constantly Amazed" wrote:

> Hi
>
> I have some code which is returning a starting point and end point for cells
> which contain data or formulas in a spreadsheet. I want to limit this to
> only cells which display data. My formulas concatenate data from an input
> sheet but display "" on lines in which no data has been entered on the input
> sheet.
>
> With ActiveSheet.UsedRange
> StartRow = .Cells(1).Row
> StartCol = .Cells(1).Column
> EndRow = .Cells(.Cells.Count).Row
> EndCol = .Cells(.Cells.Count).Column
> End With
>
> I believ it is the UsedRange element that needs changing but I'm unsure what
> it needs to be replaced with.
>
> Thanks

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Nov 2007
Sorry after re-reading your post I am left wondering if you don't want
formulas which return blank to be included??? What about Zero? Let me know...
--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

> What you are looking for is a bit more difficult than I think you anticipate.
> What you need to do is to create a range that is the union of the constants
> and formulas... Give this a try...
>
> Public Sub test() 'Run me
> Dim rng As Range
>
> Set rng = GetNonBlanks(Sheets("Sheet1"))
> If Not rng Is Nothing Then rng.Select
>
> End Sub
>
> Public Function GetNonBlanks(ByVal wks As Worksheet) As Range
> Dim rngConstants As Range
> Dim rngFormulas As Range
>
> With wks.Cells
> On Error Resume Next
> Set rngConstants = .SpecialCells(xlCellTypeConstants)
> Set rngFormulas = .SpecialCells(xlCellTypeFormulas)
> On Error GoTo 0
> End With
>
> If rngConstants Is Nothing And rngFormulas Is Nothing Then
> Set GetNonBlanks = Nothing
> ElseIf Not rngConstants Is Nothing And rngFormulas Is Nothing Then
> Set GetNonBlanks = rngConstants
> ElseIf rngConstants Is Nothing And Not rngFormulas Is Nothing Then
> Set GetNonBlanks = rngFormulas
> Else
> Set GetNonBlanks = Union(rngConstants, rngFormulas)
> End If
>
> End Function
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Constantly Amazed" wrote:
>
> > Hi
> >
> > I have some code which is returning a starting point and end point for cells
> > which contain data or formulas in a spreadsheet. I want to limit this to
> > only cells which display data. My formulas concatenate data from an input
> > sheet but display "" on lines in which no data has been entered on the input
> > sheet.
> >
> > With ActiveSheet.UsedRange
> > StartRow = .Cells(1).Row
> > StartCol = .Cells(1).Column
> > EndRow = .Cells(.Cells.Count).Row
> > EndCol = .Cells(.Cells.Count).Column
> > End With
> >
> > I believ it is the UsedRange element that needs changing but I'm unsure what
> > it needs to be replaced with.
> >
> > Thanks

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Nov 2007
If you want to avoid formulas that return blanks give this a try...

Public Sub test()
Dim rng As Range

Set rng = GetNonBlanks(Sheets("Sheet1"))
If Not rng Is Nothing Then rng.Select

End Sub

Public Function GetNonBlanks(ByVal wks As Worksheet) As Range
Dim rngConstants As Range
Dim rngFormulas As Range
Dim rngTemp As Range
Dim rng As Range

With wks.Cells
On Error Resume Next
Set rngConstants = .SpecialCells(xlCellTypeConstants)
Set rngFormulas = .SpecialCells(xlCellTypeFormulas)
On Error GoTo 0

If Not rngFormulas Is Nothing Then
For Each rng In rngFormulas
If Trim(rng.Value) <> "" Then
If rngTemp Is Nothing Then
Set rngTemp = rng
Else
Set rngTemp = Union(rng, rngTemp)
End If
End If
Next rng
Set rngFormulas = rngTemp
End If

If rngConstants Is Nothing And rngFormulas Is Nothing Then
Set GetNonBlanks = Nothing
ElseIf Not rngConstants Is Nothing And rngFormulas Is Nothing Then
Set GetNonBlanks = rngConstants
ElseIf rngConstants Is Nothing And Not rngFormulas Is Nothing Then
Set GetNonBlanks = rngFormulas
Else
Set GetNonBlanks = Union(rngConstants, rngFormulas)
End If
End With
End Function
--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

> Sorry after re-reading your post I am left wondering if you don't want
> formulas which return blank to be included??? What about Zero? Let me know...
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Jim Thomlinson" wrote:
>
> > What you are looking for is a bit more difficult than I think you anticipate.
> > What you need to do is to create a range that is the union of the constants
> > and formulas... Give this a try...
> >
> > Public Sub test() 'Run me
> > Dim rng As Range
> >
> > Set rng = GetNonBlanks(Sheets("Sheet1"))
> > If Not rng Is Nothing Then rng.Select
> >
> > End Sub
> >
> > Public Function GetNonBlanks(ByVal wks As Worksheet) As Range
> > Dim rngConstants As Range
> > Dim rngFormulas As Range
> >
> > With wks.Cells
> > On Error Resume Next
> > Set rngConstants = .SpecialCells(xlCellTypeConstants)
> > Set rngFormulas = .SpecialCells(xlCellTypeFormulas)
> > On Error GoTo 0
> > End With
> >
> > If rngConstants Is Nothing And rngFormulas Is Nothing Then
> > Set GetNonBlanks = Nothing
> > ElseIf Not rngConstants Is Nothing And rngFormulas Is Nothing Then
> > Set GetNonBlanks = rngConstants
> > ElseIf rngConstants Is Nothing And Not rngFormulas Is Nothing Then
> > Set GetNonBlanks = rngFormulas
> > Else
> > Set GetNonBlanks = Union(rngConstants, rngFormulas)
> > End If
> >
> > End Function
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "Constantly Amazed" wrote:
> >
> > > Hi
> > >
> > > I have some code which is returning a starting point and end point for cells
> > > which contain data or formulas in a spreadsheet. I want to limit this to
> > > only cells which display data. My formulas concatenate data from an input
> > > sheet but display "" on lines in which no data has been entered on the input
> > > sheet.
> > >
> > > With ActiveSheet.UsedRange
> > > StartRow = .Cells(1).Row
> > > StartCol = .Cells(1).Column
> > > EndRow = .Cells(.Cells.Count).Row
> > > EndCol = .Cells(.Cells.Count).Column
> > > End With
> > >
> > > I believ it is the UsedRange element that needs changing but I'm unsure what
> > > it needs to be replaced with.
> > >
> > > Thanks

 
Reply With Quote
 
=?Utf-8?B?Q29uc3RhbnRseSBBbWF6ZWQ=?=
Guest
Posts: n/a
 
      20th Nov 2007
Hi Jim

Thank you very much. The code seems to work just fine. My formulas
concatenate data (if any exists) and therefore 0 is not returned at any point.

Once again thanks

G

"Jim Thomlinson" wrote:

> If you want to avoid formulas that return blanks give this a try...
>
> Public Sub test()
> Dim rng As Range
>
> Set rng = GetNonBlanks(Sheets("Sheet1"))
> If Not rng Is Nothing Then rng.Select
>
> End Sub
>
> Public Function GetNonBlanks(ByVal wks As Worksheet) As Range
> Dim rngConstants As Range
> Dim rngFormulas As Range
> Dim rngTemp As Range
> Dim rng As Range
>
> With wks.Cells
> On Error Resume Next
> Set rngConstants = .SpecialCells(xlCellTypeConstants)
> Set rngFormulas = .SpecialCells(xlCellTypeFormulas)
> On Error GoTo 0
>
> If Not rngFormulas Is Nothing Then
> For Each rng In rngFormulas
> If Trim(rng.Value) <> "" Then
> If rngTemp Is Nothing Then
> Set rngTemp = rng
> Else
> Set rngTemp = Union(rng, rngTemp)
> End If
> End If
> Next rng
> Set rngFormulas = rngTemp
> End If
>
> If rngConstants Is Nothing And rngFormulas Is Nothing Then
> Set GetNonBlanks = Nothing
> ElseIf Not rngConstants Is Nothing And rngFormulas Is Nothing Then
> Set GetNonBlanks = rngConstants
> ElseIf rngConstants Is Nothing And Not rngFormulas Is Nothing Then
> Set GetNonBlanks = rngFormulas
> Else
> Set GetNonBlanks = Union(rngConstants, rngFormulas)
> End If
> End With
> End Function
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Jim Thomlinson" wrote:
>
> > Sorry after re-reading your post I am left wondering if you don't want
> > formulas which return blank to be included??? What about Zero? Let me know...
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "Jim Thomlinson" wrote:
> >
> > > What you are looking for is a bit more difficult than I think you anticipate.
> > > What you need to do is to create a range that is the union of the constants
> > > and formulas... Give this a try...
> > >
> > > Public Sub test() 'Run me
> > > Dim rng As Range
> > >
> > > Set rng = GetNonBlanks(Sheets("Sheet1"))
> > > If Not rng Is Nothing Then rng.Select
> > >
> > > End Sub
> > >
> > > Public Function GetNonBlanks(ByVal wks As Worksheet) As Range
> > > Dim rngConstants As Range
> > > Dim rngFormulas As Range
> > >
> > > With wks.Cells
> > > On Error Resume Next
> > > Set rngConstants = .SpecialCells(xlCellTypeConstants)
> > > Set rngFormulas = .SpecialCells(xlCellTypeFormulas)
> > > On Error GoTo 0
> > > End With
> > >
> > > If rngConstants Is Nothing And rngFormulas Is Nothing Then
> > > Set GetNonBlanks = Nothing
> > > ElseIf Not rngConstants Is Nothing And rngFormulas Is Nothing Then
> > > Set GetNonBlanks = rngConstants
> > > ElseIf rngConstants Is Nothing And Not rngFormulas Is Nothing Then
> > > Set GetNonBlanks = rngFormulas
> > > Else
> > > Set GetNonBlanks = Union(rngConstants, rngFormulas)
> > > End If
> > >
> > > End Function
> > > --
> > > HTH...
> > >
> > > Jim Thomlinson
> > >
> > >
> > > "Constantly Amazed" wrote:
> > >
> > > > Hi
> > > >
> > > > I have some code which is returning a starting point and end point for cells
> > > > which contain data or formulas in a spreadsheet. I want to limit this to
> > > > only cells which display data. My formulas concatenate data from an input
> > > > sheet but display "" on lines in which no data has been entered on the input
> > > > sheet.
> > > >
> > > > With ActiveSheet.UsedRange
> > > > StartRow = .Cells(1).Row
> > > > StartCol = .Cells(1).Column
> > > > EndRow = .Cells(.Cells.Count).Row
> > > > EndCol = .Cells(.Cells.Count).Column
> > > > End With
> > > >
> > > > I believ it is the UsedRange element that needs changing but I'm unsure what
> > > > it needs to be replaced with.
> > > >
> > > > Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel: average range of cells including zeros and excluding blank across sheets Netskie General Software 0 6th Dec 2010 11:05 PM
counting unique items within categories - but EXCLUDING blank cell Btaylor64 Microsoft Excel Worksheet Functions 5 9th Oct 2008 05:01 PM
formula for a cell which is blank returns populated cell =?Utf-8?B?R3JhY2V5MQ==?= Microsoft Excel Misc 1 2nd Feb 2007 09:17 AM
Range.Find returns cell outside of range when range set to single cell Frank Jones Microsoft Excel Programming 12 10th Jun 2004 04:22 AM
VB- If first cell with formula is blank, all cells in column returns blank. mnhesh Microsoft Excel Misc 2 12th May 2004 05:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:54 AM.