Need help in rewriting a 'merge columns' macro to run more efficiently

Shellecj

New Member
Joined
Aug 3, 2013
Messages
32
Could someone please examine this could code and advise if it could be rewritten more efficiently?

I've been using this same macro for over a year now but lately it's been taking much longer to run it's course as the number of records grows. The spreadsheet I'm using this macro is not really a huge worksheet of records (currently up to 480 rows). I also have several different worksheets for different purposes where I use a similar macro (each has a diff sub name to call with a different named range) and have a button setup on the main worksheet that when clicked, calls all 7 of these macros. This macro works just fine, however altogether to run through all of them is taking sometimes up to nearly 15 minutes.

Basically the purpose of this macro is to look down a certain column and if a cell is blank, I want it to copy the date from a previous column (for this particular macro, the date I want to copy is 6 rows back). I have the specific range of cells named with an offset formula with in the Names Manager, so refer to the column in the code with the name of the range instead (range("updated_Date")).

I do have quite a bit of records where I don't have a deadline date in either column, so am thinking that is in part what slows down this macro because it is essentially copying a blank value over to the destination column (range) but I don't know how to modify this code to account for this.

Below is an example of one of these macros. All 7 are pretty much the same except on some, the offset is only back one row or I resize the range, etc (e.g.: icell.Resize(1, 2).Value = icell.Resize(1, 2).Offset(0, -2).Value).

Any help would be greatly appreciated! Thanks!


Code:
 Sub Merge_Date_Columns()
Dim icell As Range
Application.ScreenUpdating = False
Sheets("Compliance").Select
    For Each icell In Range("Updated_Date")
        
        If icell.Value = "" Then
            icell.Value = icell.Offset(0, -6).Value
    
        End If
        
    Next icell
    
Application.ScreenUpdating = True
    
MsgBox "Macro Complete"
& vbLf _
& "Please press OK to continue."
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I only ran this on a test data set just one but this did work much faster! Time to complete only a little over 5 minutes! Thank you so much Mike! :cool:
 
Upvote 0
Shellecj,

Can we have a screenshot of worksheet Compliance?

To post your data, you can download and install one of the following two programs:
Excel Jeanie
MrExcel HTML Maker

Or, when using Internet Explorer, just put borders around your data in Excel and copy those cells into your post.
See reply #2 the BLUE text in the following link:
http://www.mrexcel.com/forum/about-board/444901-how-create-table-like-aladin.html#post2198045


Or even better, can we have your workbook for testing?

You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
Try also:

Code:
Sub Merge_Date_Columns2()
Dim icell As Range
Application.ScreenUpdating = False
On Error GoTo endnice
For Each icell In Sheets("Compliance").Range("Updated_Date").SpecialCells(xlCellTypeBlanks)
    icell.Value = icell.Offset(0, -6).Value
Next icell
MsgBox "Macro Complete in "  _
    & vbLf _
    & "Please press OK to continue."
endnice:
Application.ScreenUpdating = True
End Sub
By using .SpecialCells with xlCellTypeBlanks (blank cells only) you reduce the amount of looping that you do. I'd expect a 10-100 time speed increase.
 
Upvote 0
Agree with hiker that a screenshot would help, and maybe more description generally.

It sounds like you might be able to avoid the looping and just have something like

with sheet_reference.range_reference.specialcells(xlcelltypeblanks)
.formular1c1 = "=rc-6"
.value = .value
end with

If so, that and some other tweaks might dramatically reduce execution time.
 
Upvote 0
Post Script

To see the idea I refer to on a worksheet, select a range with some cells used and some blank, Then select blank cells using : CTRL-G, ALT-S, k, return

Macro recorder will give code, of course
 
Upvote 0
Sure, here is a snapshot of the worksheet that the macro above refers to...

clip_image001.png
</SPAN>
 
Upvote 0
LOL!! Well that didn't work out too well!!

Yeah I have no clue how to insert a screenshot here! LOL! Sorry!
 
Upvote 0
Does this help at all?
Initial HUD DeadlineFC Appr OrdFC Appr CompAppr OrderedAppr CompleteFinal 6mo ApprExtended
HUD Deadline
3/11/20131/17/20121/17/20123/6/20133/1/20133/1/20133/11/2013
6/19/20136/8/20126/8/20126/28/20137/8/20137/8/20137/19/2013
1/19/201411/1/201211/19/201312/27/2013
1/21/20144/23/201312/12/201312/30/2013
3/5/20145/13/20137/31/201311/15/201311/18/2013
3/8/20147/24/20137/24/201311/25/201312/2/2013
3/8/20148/21/20138/21/201311/26/201311/29/2013
4/1/20143/25/20133/25/201312/10/201312/11/2013
4/1/20141/17/20131/17/201311/12/201311/14/2013
4/2/20146/24/20136/24/201312/6/201312/10/2013
6/28/20149/10/20139/10/201311/7/201311/15/2013
7/6/20148/29/20128/29/201211/22/201312/2/2013
8/4/20136/25/20126/25/20127/9/20137/15/20137/15/2013
8/14/201312/10/201212/10/20127/18/20137/26/20137/26/20138/14/2013
10/27/20131/27/20121/27/201210/1/201310/3/201310/3/2013
10/29/20137/3/20129/10/201310/1/201310/9/201310/9/2013
11/9/201311/30/201211/30/201210/10/201310/15/201310/15/2013
11/11/20133/18/20133/18/201310/15/201310/21/201310/21/2013
11/25/20134/18/20134/18/201310/30/201311/1/201311/1/2013
12/2/201311/16/201211/16/201211/6/201311/14/201311/14/2013
12/7/20135/17/20135/17/201311/11/201311/14/201311/14/2013
12/7/20131/17/20131/17/201311/11/201311/14/201311/14/2013
12/8/20134/24/20134/24/201311/11/201311/15/201311/15/2013
12/13/201210/16/201311/12/201311/13/201311/13/2013
4/17/20134/17/201311/11/201311/14/201311/14/2013
2/7/20132/7/201311/15/201311/19/201311/19/2013
10/5/201210/5/201211/15/201311/25/201311/25/2013
3/7/20123/7/201211/16/201311/19/201311/19/2013
9/12/20129/12/201211/19/201311/21/201311/21/2013
4/22/20134/22/201311/21/201311/25/201311/25/2013
2/20/20132/20/201311/21/201311/25/201311/25/2013
2/6/20132/6/201312/3/201312/5/201312/5/2013
5/3/20135/3/201312/3/201312/6/201312/6/2013
6/26/20136/26/201312/9/201312/16/201312/16/2013
6/4/20136/4/201311/21/201312/10/201312/10/2013
3/20/20133/20/201312/10/201312/16/201312/16/2013
9/7/201211/13/201312/30/201312/31/201312/31/2013
5/30/20135/30/201312/17/201312/18/201312/18/2013
12/3/201212/3/201212/16/201312/18/201312/18/2013
2/15/201311/11/201312/16/201312/19/201312/19/2013
10/22/201212/7/201312/31/20131/6/20141/6/2014
2/7/201310/24/20131/2/2014
6/22/20138/14/201311/13/201311/14/2013
10/16/201212/16/201312/30/2013
3/5/20123/5/201210/15/201310/23/2013
5/28/20135/28/201310/14/201310/16/2013
6/16/201311/26/20131/3/2014
3/10/20133/10/20131/9/2014
10/10/20129/3/201311/13/201311/15/2013
2/19/20132/19/201311/6/201311/13/2013
4/20/20124/20/20129/4/20139/10/2013
2/20/20132/20/201312/19/201312/23/2013
2/12/20132/12/201312/2/201312/3/2013
7/22/20127/22/201212/5/201312/11/2013
2/1/20139/20/201312/31/2013
5/24/20135/24/201312/31/20131/4/2014
7/3/20137/3/201311/22/201311/25/2013
3/9/20123/9/20129/18/20139/25/2013
12/28/201212/28/201211/21/201311/26/2013
7/1/20138/8/201311/11/201311/18/2013
2/19/20139/9/201312/31/2013 2/19/2014
5/14/20139/26/201312/23/2013 2/22/2014
5/8/20135/8/201311/22/201311/30/2013
5/14/201311/25/201312/24/2013
7/17/20139/20/201312/31/2013
4/15/20134/15/201311/20/201311/24/2013
4/22/20139/27/201312/16/2013
7/10/20137/10/201312/17/201312/24/2013
5/24/20135/24/201311/22/201311/29/2013
5/27/20135/27/201310/28/201311/1/2013
4/23/20134/23/201310/15/201310/17/2013
7/24/20137/24/201310/30/201311/4/2013
7/22/20137/22/201311/25/201311/27/2013
3/18/20133/18/201312/2/201312/6/2013
5/13/20135/13/201310/7/201310/15/2013
6/6/201310/9/201312/12/2013
7/3/20137/3/201310/1/201310/4/2013
7/10/201310/1/201312/23/2013
3/20/20139/18/201311/19/201311/25/2013
7/20/20139/17/201312/31/2013
6/12/20136/12/201311/25/201312/4/2013 3/11/2014
1/18/20131/18/201311/22/201311/29/2013
7/2/201311/5/201312/27/2013
5/20/20135/20/201312/3/201312/6/2013
8/5/20138/5/201310/1/201310/7/2013
8/3/201310/1/201311/11/201311/18/2013
6/22/20136/22/201310/7/201310/14/2013
4/18/20134/18/201312/3/201312/5/2013
7/20/20137/20/201311/5/201311/13/2013
8/12/20138/12/20131/8/2014
6/4/20136/4/201312/17/201312/21/2013
6/20/20138/14/201311/13/201311/14/2013
2/8/20132/8/201310/21/201310/24/2013
9/2/20128/5/201310/22/201310/24/2013
6/2/20139/9/201312/27/2013
7/1/20137/1/201311/26/201312/3/2013
4/2/20134/2/201312/31/2013
2/14/201310/10/201312/16/201312/17/2013
8/23/20138/23/201310/1/201310/4/2013 3/29/2014
4/9/20134/9/201312/5/201312/9/2013
4/20/20134/20/201311/18/201311/22/2013
3/6/20133/6/201311/19/201311/19/2013
5/8/20135/8/201310/30/201311/1/2013
7/11/20127/11/201210/15/201310/18/2013
7/24/20137/24/201310/23/201310/29/2013
7/16/20137/16/201311/22/201311/27/2013
7/22/20137/22/20131/7/2014
8/20/20138/20/201310/17/201310/23/2013
3/15/20133/15/201312/5/201312/11/2013
7/18/20137/18/20139/26/201310/4/2013
9/6/20139/6/201311/22/201311/25/2013
3/28/20133/28/201310/29/201311/13/2013
7/3/20137/3/201311/26/201312/4/2013
7/29/20137/29/201310/24/201310/29/2013
8/2/20138/2/201310/9/201311/26/2013
2/21/20132/21/201311/11/201311/13/2013
7/1/20137/1/201311/7/201311/12/2013
7/18/20137/18/20131/8/2014
8/9/20138/9/201311/22/201311/26/2013
4/21/20134/21/201310/23/201311/6/2013
3/28/20133/28/201311/7/201311/5/2013 4/13/2014
8/18/20138/18/201311/26/201312/20/2013 4/13/2014
9/5/20139/5/201310/22/201311/8/2013
7/22/20137/22/201312/10/201312/13/2013
9/28/20129/28/201210/29/201311/5/2013
2/6/20133/6/201310/29/201311/4/2013
8/12/20138/12/201311/26/201311/29/2013
7/26/20137/26/201312/17/201312/18/2013
11/12/201311/12/201312/3/201312/9/2013
4/25/20134/25/201310/31/201311/8/2013
2/15/20132/15/201312/17/201312/23/2013
9/12/20139/12/201310/24/201311/2/2013
9/11/201311/27/201312/24/2013
9/26/20139/26/20139/26/20139/26/2013
5/7/20135/7/201312/10/201312/13/2013
9/3/201312/2/20131/3/2014
9/26/20139/26/201311/1/201311/2/2013

<TBODY>
</TBODY><COLGROUP><COL span=3><COL><COL><COL><COL></COLGROUP>
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top