PC Review


Reply
Thread Tools Rate Thread

Can you do IsEmpty(Range)?

 
 
Sharkbait
Guest
Posts: n/a
 
      8th Aug 2006
As I progress in VBA, I'm trying to make my macros more elegant and
less heavy-handed. One of those problems is how I end Do loops with
embedded If loops. I was previously just inserting a stop point in the
column. Something like 'Do Until ActiveCell = "End"'.
That works ok, until the range I'm working on is much shorter than my
stop range.

Is it possible to use IsEmpty to test a 15 cell block, and if they're
all empty to end, or insert my end point?

I've tried using the following code, but it doesn't work.

Range("C1").Select
Do Until ActiveCell = "ZZZ"
Range(ActiveCell, ActiveCell.Offset(15, 0)).Select
If IsEmpty(Selection) = True Then
ActiveCell.Offset(10, 0).Select
ActiveCell.FormulaR1C1 = "ZZZ"
Else: ActiveCell.Offset(5, 0).Select
End If
Loop

Thanks

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      8th Aug 2006
Isempty() will work with one cell--not multiple cells, but maybe you could use:

if application.counta(yourrangehere) = 0 then
'all the cells are empty


Sharkbait wrote:
>
> As I progress in VBA, I'm trying to make my macros more elegant and
> less heavy-handed. One of those problems is how I end Do loops with
> embedded If loops. I was previously just inserting a stop point in the
> column. Something like 'Do Until ActiveCell = "End"'.
> That works ok, until the range I'm working on is much shorter than my
> stop range.
>
> Is it possible to use IsEmpty to test a 15 cell block, and if they're
> all empty to end, or insert my end point?
>
> I've tried using the following code, but it doesn't work.
>
> Range("C1").Select
> Do Until ActiveCell = "ZZZ"
> Range(ActiveCell, ActiveCell.Offset(15, 0)).Select
> If IsEmpty(Selection) = True Then
> ActiveCell.Offset(10, 0).Select
> ActiveCell.FormulaR1C1 = "ZZZ"
> Else: ActiveCell.Offset(5, 0).Select
> End If
> Loop
>
> Thanks


--

Dave Peterson
 
Reply With Quote
 
Sharkbait
Guest
Posts: n/a
 
      8th Aug 2006
Thanks.

One other quick question though.

I have a macro that runs very fast in one workbook. I also need to use
it in another workbook. So, I inserted a new module and pasted the
macro into the other workbook. For some reason, it runs very slow in
the new workbook.

The first workbook only has that one macro, while the other has 5. Not
sure if that matters. Also the first workbook is about 590kb while the
other is about 2.3mb. Again, not sure if that matters.

Any help would be appreciated.

Dave Peterson wrote:
> Isempty() will work with one cell--not multiple cells, but maybe you could use:
>
> if application.counta(yourrangehere) = 0 then
> 'all the cells are empty
>
>
> Sharkbait wrote:
> >
> > As I progress in VBA, I'm trying to make my macros more elegant and
> > less heavy-handed. One of those problems is how I end Do loops with
> > embedded If loops. I was previously just inserting a stop point in the
> > column. Something like 'Do Until ActiveCell = "End"'.
> > That works ok, until the range I'm working on is much shorter than my
> > stop range.
> >
> > Is it possible to use IsEmpty to test a 15 cell block, and if they're
> > all empty to end, or insert my end point?
> >
> > I've tried using the following code, but it doesn't work.
> >
> > Range("C1").Select
> > Do Until ActiveCell = "ZZZ"
> > Range(ActiveCell, ActiveCell.Offset(15, 0)).Select
> > If IsEmpty(Selection) = True Then
> > ActiveCell.Offset(10, 0).Select
> > ActiveCell.FormulaR1C1 = "ZZZ"
> > Else: ActiveCell.Offset(5, 0).Select
> > End If
> > Loop
> >
> > Thanks

>
> --
>
> Dave Peterson


 
Reply With Quote
 
Sharkbait
Guest
Posts: n/a
 
      8th Aug 2006
Here's the code btw.

Cells.Select
Application.CutCopyMode = False
With Selection
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Columns("C:E").Select
Selection.Delete Shift:=xlToLeft
Columns("D:F").Select
Selection.Delete Shift:=xlToLeft
Columns("E:F").Select
Selection.Delete Shift:=xlToLeft
Columns("F:G").Select
Selection.Delete Shift:=xlToLeft
Columns("G:H").Select
Selection.Delete Shift:=xlToLeft
Range("I1").Select
Selection.Cut Destination:=Range("J1")
Columns("H:I").Select
Selection.Delete Shift:=xlToLeft
Columns("I:I").Select
Selection.Delete Shift:=xlToLeft
Range("I5:AA5").Select
Selection.Copy
Range("I1").Select
ActiveSheet.Paste
Columns("J:J").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("K:M").Select
Selection.Delete Shift:=xlToLeft
Columns("L:L").Select
Selection.Delete Shift:=xlToLeft
Columns("M:N").Select
Selection.Delete Shift:=xlToLeft
Columns("N:N").Select
Selection.Delete Shift:=xlToLeft
Columns("P:P").Select
Selection.Delete Shift:=xlToLeft
ActiveWindow.SmallScroll ToRight:=3
ActiveWindow.ScrollColumn = 1
Columns("A:A").Select
ActiveCell.Replace What:="WANT DATE:", Replacement:="", LookAt:=xlPart,
_
SearchOrder:=xlByRows, MatchCase:=False
Columns("A:A").Select
Selection.Replace What:="Want Date:", Replacement:="", LookAt:=xlPart,
_
SearchOrder:=xlByRows, MatchCase:=False

Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Range("A1:A614").Select
Selection.Copy
Range("B2").Select
ActiveSheet.Paste
Range("A600").Select
ActiveCell.FormulaR1C1 = "ZZZ"
Range("C600").Select
ActiveCell.FormulaR1C1 = "ZZZ"
Range("A3").Select
Do Until ActiveCell = "ZZZ"
If CellColorIndex(ActiveCell, False) = 15 Then
ActiveCell.Offset(1, 0).Select
Else: ActiveCell.Offset(-1, 0).Select
Range(Selection, Selection.Offset(1, 0)).Select
Selection.FillDown
End If
Loop
Range("C6").Select
Do Until ActiveCell = "ZZZ"
If IsEmpty(ActiveCell) = True Then
ActiveCell.Offset(1, 0).Select
Else: ActiveCell.Offset(0, -1).Select
If IsEmpty(ActiveCell) = True Then
ActiveCell.Offset(-1, 0).Select
Range(Selection, Selection.Offset(1, 0)).Select
Selection.FillDown
ActiveCell.Offset(2, 1).Select
Else: ActiveCell.Offset(1, 1).Select
End If
End If
Loop
Range("A2", "S600").Select
Selection.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A2").Select
Columns("E:I").EntireColumn.AutoFit
Columns("H:H").Select
Selection.NumberFormat = "#,##0"
Columns("J:S").Select
Selection.NumberFormat = "0.0"
Columns("J:S").Select
Selection.ColumnWidth = 5.5
Range("A1:S1").Select
With Selection
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Sharkbait wrote:
> Thanks.
>
> One other quick question though.
>
> I have a macro that runs very fast in one workbook. I also need to use
> it in another workbook. So, I inserted a new module and pasted the
> macro into the other workbook. For some reason, it runs very slow in
> the new workbook.
>
> The first workbook only has that one macro, while the other has 5. Not
> sure if that matters. Also the first workbook is about 590kb while the
> other is about 2.3mb. Again, not sure if that matters.
>
> Any help would be appreciated.
>
> Dave Peterson wrote:
> > Isempty() will work with one cell--not multiple cells, but maybe you could use:
> >
> > if application.counta(yourrangehere) = 0 then
> > 'all the cells are empty
> >
> >
> > Sharkbait wrote:
> > >
> > > As I progress in VBA, I'm trying to make my macros more elegant and
> > > less heavy-handed. One of those problems is how I end Do loops with
> > > embedded If loops. I was previously just inserting a stop point in the
> > > column. Something like 'Do Until ActiveCell = "End"'.
> > > That works ok, until the range I'm working on is much shorter than my
> > > stop range.
> > >
> > > Is it possible to use IsEmpty to test a 15 cell block, and if they're
> > > all empty to end, or insert my end point?
> > >
> > > I've tried using the following code, but it doesn't work.
> > >
> > > Range("C1").Select
> > > Do Until ActiveCell = "ZZZ"
> > > Range(ActiveCell, ActiveCell.Offset(15, 0)).Select
> > > If IsEmpty(Selection) = True Then
> > > ActiveCell.Offset(10, 0).Select
> > > ActiveCell.FormulaR1C1 = "ZZZ"
> > > Else: ActiveCell.Offset(5, 0).Select
> > > End If
> > > Loop
> > >
> > > Thanks

> >
> > --
> >
> > Dave Peterson


 
Reply With Quote
 
=?Utf-8?B?U2NvdHQ=?=
Guest
Posts: n/a
 
      8th Aug 2006
with all those deletes you should use
Application.Calculation = xlCalculationManual
at the begining

and
Application.Calculation = xlCalculationAutomatic

(or at least until all the deletes are done).

to speed things up you could also use
application.screenupdating = false

and

application.screenupdating = true
at the end.

On top of things, you don't need this
> Columns("D:F").Select
> Selection.Delete Shift:=xlToLeft


just use columns("D:F").delete

(this goes the same for most of the other things you do where you first
select the columns and then do something to them. just use
columns("...").whatever)

these are some simple things you could use to speed up your code.

Cheers,

Scott


"Sharkbait" wrote:

> Here's the code btw.
>
> Cells.Select
> Application.CutCopyMode = False
> With Selection
> .WrapText = False
> .Orientation = 0
> .AddIndent = False
> .ShrinkToFit = False
> .MergeCells = False
> End With
> Columns("C:E").Select
> Selection.Delete Shift:=xlToLeft
> Columns("D:F").Select
> Selection.Delete Shift:=xlToLeft
> Columns("E:F").Select
> Selection.Delete Shift:=xlToLeft
> Columns("F:G").Select
> Selection.Delete Shift:=xlToLeft
> Columns("G:H").Select
> Selection.Delete Shift:=xlToLeft
> Range("I1").Select
> Selection.Cut Destination:=Range("J1")
> Columns("H:I").Select
> Selection.Delete Shift:=xlToLeft
> Columns("I:I").Select
> Selection.Delete Shift:=xlToLeft
> Range("I5:AA5").Select
> Selection.Copy
> Range("I1").Select
> ActiveSheet.Paste
> Columns("J:J").Select
> Application.CutCopyMode = False
> Selection.Delete Shift:=xlToLeft
> Columns("K:M").Select
> Selection.Delete Shift:=xlToLeft
> Columns("L:L").Select
> Selection.Delete Shift:=xlToLeft
> Columns("M:N").Select
> Selection.Delete Shift:=xlToLeft
> Columns("N:N").Select
> Selection.Delete Shift:=xlToLeft
> Columns("P:P").Select
> Selection.Delete Shift:=xlToLeft
> ActiveWindow.SmallScroll ToRight:=3
> ActiveWindow.ScrollColumn = 1
> Columns("A:A").Select
> ActiveCell.Replace What:="WANT DATE:", Replacement:="", LookAt:=xlPart,
> _
> SearchOrder:=xlByRows, MatchCase:=False
> Columns("A:A").Select
> Selection.Replace What:="Want Date:", Replacement:="", LookAt:=xlPart,
> _
> SearchOrder:=xlByRows, MatchCase:=False
>
> Columns("B:B").Select
> Selection.Insert Shift:=xlToRight
> Range("A1:A614").Select
> Selection.Copy
> Range("B2").Select
> ActiveSheet.Paste
> Range("A600").Select
> ActiveCell.FormulaR1C1 = "ZZZ"
> Range("C600").Select
> ActiveCell.FormulaR1C1 = "ZZZ"
> Range("A3").Select
> Do Until ActiveCell = "ZZZ"
> If CellColorIndex(ActiveCell, False) = 15 Then
> ActiveCell.Offset(1, 0).Select
> Else: ActiveCell.Offset(-1, 0).Select
> Range(Selection, Selection.Offset(1, 0)).Select
> Selection.FillDown
> End If
> Loop
> Range("C6").Select
> Do Until ActiveCell = "ZZZ"
> If IsEmpty(ActiveCell) = True Then
> ActiveCell.Offset(1, 0).Select
> Else: ActiveCell.Offset(0, -1).Select
> If IsEmpty(ActiveCell) = True Then
> ActiveCell.Offset(-1, 0).Select
> Range(Selection, Selection.Offset(1, 0)).Select
> Selection.FillDown
> ActiveCell.Offset(2, 1).Select
> Else: ActiveCell.Offset(1, 1).Select
> End If
> End If
> Loop
> Range("A2", "S600").Select
> Selection.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess,
> _
> OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
> Range("A2").Select
> Columns("E:I").EntireColumn.AutoFit
> Columns("H:H").Select
> Selection.NumberFormat = "#,##0"
> Columns("J:S").Select
> Selection.NumberFormat = "0.0"
> Columns("J:S").Select
> Selection.ColumnWidth = 5.5
> Range("A1:S1").Select
> With Selection
> .WrapText = True
> .Orientation = 0
> .AddIndent = False
> .ShrinkToFit = False
> .MergeCells = False
> End With
> Sharkbait wrote:
> > Thanks.
> >
> > One other quick question though.
> >
> > I have a macro that runs very fast in one workbook. I also need to use
> > it in another workbook. So, I inserted a new module and pasted the
> > macro into the other workbook. For some reason, it runs very slow in
> > the new workbook.
> >
> > The first workbook only has that one macro, while the other has 5. Not
> > sure if that matters. Also the first workbook is about 590kb while the
> > other is about 2.3mb. Again, not sure if that matters.
> >
> > Any help would be appreciated.
> >
> > Dave Peterson wrote:
> > > Isempty() will work with one cell--not multiple cells, but maybe you could use:
> > >
> > > if application.counta(yourrangehere) = 0 then
> > > 'all the cells are empty
> > >
> > >
> > > Sharkbait wrote:
> > > >
> > > > As I progress in VBA, I'm trying to make my macros more elegant and
> > > > less heavy-handed. One of those problems is how I end Do loops with
> > > > embedded If loops. I was previously just inserting a stop point in the
> > > > column. Something like 'Do Until ActiveCell = "End"'.
> > > > That works ok, until the range I'm working on is much shorter than my
> > > > stop range.
> > > >
> > > > Is it possible to use IsEmpty to test a 15 cell block, and if they're
> > > > all empty to end, or insert my end point?
> > > >
> > > > I've tried using the following code, but it doesn't work.
> > > >
> > > > Range("C1").Select
> > > > Do Until ActiveCell = "ZZZ"
> > > > Range(ActiveCell, ActiveCell.Offset(15, 0)).Select
> > > > If IsEmpty(Selection) = True Then
> > > > ActiveCell.Offset(10, 0).Select
> > > > ActiveCell.FormulaR1C1 = "ZZZ"
> > > > Else: ActiveCell.Offset(5, 0).Select
> > > > End If
> > > > Loop
> > > >
> > > > Thanks
> > >
> > > --
> > >
> > > Dave Peterson

>
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      8th Aug 2006
Another thing to do is to stop selecting ranges before you work with them.

And instead of doing them little bits at a time, you could get rid of whole
columns like:

Range("C1:e1,g1:i1,k1:m1,n11,q1:r1,t1:u1,w1").EntireColumn.Delete



Sharkbait wrote:
>
> Here's the code btw.
>
> Cells.Select
> Application.CutCopyMode = False
> With Selection
> .WrapText = False
> .Orientation = 0
> .AddIndent = False
> .ShrinkToFit = False
> .MergeCells = False
> End With
> Columns("C:E").Select
> Selection.Delete Shift:=xlToLeft
> Columns("D:F").Select
> Selection.Delete Shift:=xlToLeft
> Columns("E:F").Select
> Selection.Delete Shift:=xlToLeft
> Columns("F:G").Select
> Selection.Delete Shift:=xlToLeft
> Columns("G:H").Select
> Selection.Delete Shift:=xlToLeft
> Range("I1").Select
> Selection.Cut Destination:=Range("J1")
> Columns("H:I").Select
> Selection.Delete Shift:=xlToLeft
> Columns("I:I").Select
> Selection.Delete Shift:=xlToLeft
> Range("I5:AA5").Select
> Selection.Copy
> Range("I1").Select
> ActiveSheet.Paste
> Columns("J:J").Select
> Application.CutCopyMode = False
> Selection.Delete Shift:=xlToLeft
> Columns("K:M").Select
> Selection.Delete Shift:=xlToLeft
> Columns("L:L").Select
> Selection.Delete Shift:=xlToLeft
> Columns("M:N").Select
> Selection.Delete Shift:=xlToLeft
> Columns("N:N").Select
> Selection.Delete Shift:=xlToLeft
> Columns("P:P").Select
> Selection.Delete Shift:=xlToLeft
> ActiveWindow.SmallScroll ToRight:=3
> ActiveWindow.ScrollColumn = 1
> Columns("A:A").Select
> ActiveCell.Replace What:="WANT DATE:", Replacement:="", LookAt:=xlPart,
> _
> SearchOrder:=xlByRows, MatchCase:=False
> Columns("A:A").Select
> Selection.Replace What:="Want Date:", Replacement:="", LookAt:=xlPart,
> _
> SearchOrder:=xlByRows, MatchCase:=False
>
> Columns("B:B").Select
> Selection.Insert Shift:=xlToRight
> Range("A1:A614").Select
> Selection.Copy
> Range("B2").Select
> ActiveSheet.Paste
> Range("A600").Select
> ActiveCell.FormulaR1C1 = "ZZZ"
> Range("C600").Select
> ActiveCell.FormulaR1C1 = "ZZZ"
> Range("A3").Select
> Do Until ActiveCell = "ZZZ"
> If CellColorIndex(ActiveCell, False) = 15 Then
> ActiveCell.Offset(1, 0).Select
> Else: ActiveCell.Offset(-1, 0).Select
> Range(Selection, Selection.Offset(1, 0)).Select
> Selection.FillDown
> End If
> Loop
> Range("C6").Select
> Do Until ActiveCell = "ZZZ"
> If IsEmpty(ActiveCell) = True Then
> ActiveCell.Offset(1, 0).Select
> Else: ActiveCell.Offset(0, -1).Select
> If IsEmpty(ActiveCell) = True Then
> ActiveCell.Offset(-1, 0).Select
> Range(Selection, Selection.Offset(1, 0)).Select
> Selection.FillDown
> ActiveCell.Offset(2, 1).Select
> Else: ActiveCell.Offset(1, 1).Select
> End If
> End If
> Loop
> Range("A2", "S600").Select
> Selection.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess,
> _
> OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
> Range("A2").Select
> Columns("E:I").EntireColumn.AutoFit
> Columns("H:H").Select
> Selection.NumberFormat = "#,##0"
> Columns("J:S").Select
> Selection.NumberFormat = "0.0"
> Columns("J:S").Select
> Selection.ColumnWidth = 5.5
> Range("A1:S1").Select
> With Selection
> .WrapText = True
> .Orientation = 0
> .AddIndent = False
> .ShrinkToFit = False
> .MergeCells = False
> End With
> Sharkbait wrote:
> > Thanks.
> >
> > One other quick question though.
> >
> > I have a macro that runs very fast in one workbook. I also need to use
> > it in another workbook. So, I inserted a new module and pasted the
> > macro into the other workbook. For some reason, it runs very slow in
> > the new workbook.
> >
> > The first workbook only has that one macro, while the other has 5. Not
> > sure if that matters. Also the first workbook is about 590kb while the
> > other is about 2.3mb. Again, not sure if that matters.
> >
> > Any help would be appreciated.
> >
> > Dave Peterson wrote:
> > > Isempty() will work with one cell--not multiple cells, but maybe you could use:
> > >
> > > if application.counta(yourrangehere) = 0 then
> > > 'all the cells are empty
> > >
> > >
> > > Sharkbait wrote:
> > > >
> > > > As I progress in VBA, I'm trying to make my macros more elegant and
> > > > less heavy-handed. One of those problems is how I end Do loops with
> > > > embedded If loops. I was previously just inserting a stop point in the
> > > > column. Something like 'Do Until ActiveCell = "End"'.
> > > > That works ok, until the range I'm working on is much shorter than my
> > > > stop range.
> > > >
> > > > Is it possible to use IsEmpty to test a 15 cell block, and if they're
> > > > all empty to end, or insert my end point?
> > > >
> > > > I've tried using the following code, but it doesn't work.
> > > >
> > > > Range("C1").Select
> > > > Do Until ActiveCell = "ZZZ"
> > > > Range(ActiveCell, ActiveCell.Offset(15, 0)).Select
> > > > If IsEmpty(Selection) = True Then
> > > > ActiveCell.Offset(10, 0).Select
> > > > ActiveCell.FormulaR1C1 = "ZZZ"
> > > > Else: ActiveCell.Offset(5, 0).Select
> > > > End If
> > > > Loop
> > > >
> > > > Thanks
> > >
> > > --
> > >
> > > Dave Peterson


--

Dave Peterson
 
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
Use IsEmpty on a range without a loop? XL03 ker_01 Microsoft Excel Programming 3 16th Feb 2010 10:34 PM
Can you do IsEmpty(Range)? Sharkbait Microsoft Excel Programming 1 8th Aug 2006 05:01 PM
Either an "IsEmpty" or a Range issue.... Celt Microsoft Excel Programming 1 7th Apr 2006 12:10 AM
Re: IsEmpty on a range/array Norman Jones Microsoft Excel Programming 3 7th Sep 2004 08:35 PM
Re: IsEmpty on a range/array JE McGimpsey Microsoft Excel Programming 0 7th Sep 2004 07:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:25 AM.