You are here:

Excel/Inserting lines every 5 columns

Advertisement


Question
Hello Jerry,

I have recently upgraded ( not by choice) from Excel 203 to 2013. I run one macro only, I pieced it together about 7 years ago searching online. This macro runs beautifully in 2003, but when I run it in 2013 I get "Excel cannot complete task with available resources. Choose less data or "

The macro inserts a dashed line " ------ " in each cell of every other 5 rows the same width as the row. It makes it far easier to read when printed. A quick example of what it looks like AFTER the macro is run on a typical worksheet:
   A     B     C     D       E         F       G
1 Smith  Matt  650  Redwood street  Oakville Wherever
2 Smith  Matt  650  Redwood street  Oakville Wherever
3 Smith  Matt  650  Redwood street  Oakville Wherever
4 Smith  Matt  650  Redwood street  Oakville Wherever
5 Smith  Matt  650  Redwood street  Oakville Wherever
6 -----  ----  ---  --------  -----  -------- -------
7 Smith  Matt  650  Redwood street  Oakville Wherever
8 Smith  Matt  650  Redwood street  Oakville Wherever
etc, etc

THE MACRO:


Sub InsertRows()

Application.ScreenUpdating = False
Dim numRows As Integer
Dim r As Long, c
numRows = 1
r = Cells(Rows.Count, "A").End(xlUp).Row
c = Columns.Count - 1
For i = r + 1 To 1 Step -5
Set h = ActiveSheet.Rows(i)
h.Resize(numRows).Insert
For j = 0 To c
 Set a = h.Cells(1, 1).Offset(-1, j)
 a.Value = WorksheetFunction.Rept("-", a.ColumnWidth * 1.6)
Next j
Next i
Application.ScreenUpdating = True
End Sub



Can you see any obvious reason why this would work in 2003 and not 2013. I have 6 GB of memory and an Intel i3 processor running Windows 8.

I appreciate any help you can give, I am truly very limited in macros and VBA.

Thank you
Jim

Answer
Try this instead:


Option Explicit

Sub InsertRows()
Dim LC As Long, LR As Long, Rw As Long, c As Long
Dim MyText As Variant

Application.ScreenUpdating = False

LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column

For c = 1 To LC
   With Cells(LR + 1, c)
       .Value = WorksheetFunction.Rept("-", .ColumnWidth * 1.6)
   End With
Next c

MyText = Application.WorksheetFunction.Transpose(Range("A" & LR + 1).Resize(, LC))
Range("A" & LR + 1).Resize(, LC).Value = ""

For Rw = (LR - (LR Mod 5)) To 1 Step -5
   Rows(Rw + 1).Insert xlShiftDown
   Range("A" & Rw + 1).Resize(, LC).Value = Application.WorksheetFunction.Transpose(MyText)
Next Rw

Application.ScreenUpdating = True
End Sub  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jerry Beaucaire

Expertise

Excel Formulas, macros, automation. Microsoft Excel MVP - 2010. Code site with free code snippets and techniques: http://sites.madrocketscientist.com/jerrybeaucaires-excelassistant/files

Experience

Microsoft Excel MVP - 2010. I have my own extensive Excel help/code site: http://sites.madrocketscientist.com/jerrybeaucaires-excelassistant/files ===================== I have been offering free assistance as an Excel aid on many web sites for many years: (http://www.excelforum.com - JBeaucaire) ======== (http://www.askmehelpdesk.com/spreadsheets - JBeaucaire) ======= (http://www.mrexcel.com/forum - jbeaucaire)

Education/Credentials
Bachelor's Degree from Azusa Pacific University in Mathematics and Music Composition

Awards and Honors
Microsoft Excel MVP 2010

©2016 About.com. All rights reserved.