#1   Report Post  
matt
 
Posts: n/a
Default simplify code

Hi,

I'm pretty new to writing code, so I wanted to ask for some advice. I've got
a code which checks to see if cells in two columns are equal, and if they are
not, it inserts blank cells in three columns. It works, but it is pretty
slow, as I have written three separate IF statements for inserting blank
cells. Is there any way that I can condense this into one IF statement? the
code I am using so far is:

LastRow = ActiveSheet.Cells(Rows.Count, "b").End(xlUp).Row
Range("E1").Select
For j=1 To LastRow
If Range("b" & j) < Range("a" & j) Then Range("b" & j).Select
Selection.Insert Shift:=xlDown
If Range("b" & j) < Range("a" & j) Then Range("c" & j).Select
Selection.Insert Shift:=xlDown
If Range("b" & j) < Range("a" & j) Then Range("d" & j).Select
Selection.Insert Shift:=xlDown
Range("e" & j).Select
Next j

Any help would be greatly appreciated.
  #2   Report Post  
Dave O
 
Posts: n/a
Default

Looks like you can condense it like this:

LastRow = ActiveSheet.Cells(Rows.Count, "b").End(xlUp).Row
Range("E1").Select
For j=1 To LastRow
If Range("b" & j) < Range("a" & j) Then
Range("b" & j).Select
Selection.Insert Shift:=xlDown
Range("c" & j).Select
Selection.Insert Shift:=xlDown
Range("d" & j).Select
Selection.Insert Shift:=xlDown
Range("e" & j).Select
Next j

I suspect the length of time required to run your code is not due to
the quantity of IF statements, however. More likely the duration is
due to the spreadsheet recalculating itself every time one of the
insert / shift down operations is performed. You might try the
following code at the beginning of your macro to turn calculation from
auto to manual:

With Application
.Calculation = xlManual
.MaxChange = 0.001
End With

.... and then this code at the end to turn calculation back to auto:
With Application
.Calculation = xlAutomatic
.MaxChange = 0.001
End With

  #3   Report Post  
matt
 
Posts: n/a
Default

Dave,

I don't think that the automatic/manual setting can speed up the program at
all, because this data has not yet been used in any calculations, there are
only a few columns of data at this point. However, I am performing this on
835 rows, so maybe it won't get any faster with less IF statements. However,
for whatever reason, when I tried the code you suggested, I got a an error
about the Next, saying

Error: Next without For.

Does anyone know what this means, and how to bypass the problem. The code
works fine using three separate IF statements, it just seems like there
should be a way to condense them into one.


  #4   Report Post  
David McRitchie
 
Posts: n/a
Default

Hi Matt,
Suggest you try turning off both Calculation and ScreenUpdating
and yes you do need both. You are inserting cells or rows so you
have to recalculate the sheet, and you are changing your selection,
so both are needed. It is best to write your code if possible to
avoid changing a selection, but regardless that simple change
can run circles around bad coding (that works). Combine good
coding with that and you will run much faster.
http://www.mvps.org/dmcritchie/excel...htm#slowmacros

A means of timing your macro is provided on that same page.

When inserting or deleting rows or in your case cells you want to start from
the bottom and work your way up with Step -1 see some examples in
http://www.mvps.org/dmcritchie/excel...nsertBlankRows

---
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

"matt" wrote in message ...
Dave,

I don't think that the automatic/manual setting can speed up the program at
all, because this data has not yet been used in any calculations, there are
only a few columns of data at this point. However, I am performing this on
835 rows, so maybe it won't get any faster with less IF statements. However,
for whatever reason, when I tried the code you suggested, I got a an error
about the Next, saying

Error: Next without For.

Does anyone know what this means, and how to bypass the problem. The code
works fine using three separate IF statements, it just seems like there
should be a way to condense them into one.




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
Change case...help please Terry Excel Worksheet Functions 14 October 2nd 05 12:29 PM
Macro for changing text to Proper Case JPriest Excel Worksheet Functions 3 August 8th 05 09:31 PM
Using other workbooks.. DavidMunday Excel Worksheet Functions 2 July 1st 05 07:35 AM
Make Change Case in Excel a format rather than formula Kevin Excel Worksheet Functions 1 March 18th 05 08:53 PM
Opening a file with code without a set file name jenkinspat Excel Discussion (Misc queries) 1 March 4th 05 10:50 AM


All times are GMT +1. The time now is 08:40 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"