A Microsoft Excel forum. ExcelBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » ExcelBanter forum » Excel Newsgroups » Excel Programming
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

"Run-time error '1004'



 
 
Thread Tools Display Modes
  #1  
Old June 13th 12, 02:09 PM posted to microsoft.public.excel.programming
icystorm
external usenet poster
 
Posts: 20
Default "Run-time error '1004'

Greetings:

In Excel 2007, I wrote a macro to insert a line between all visible
rows. However, one portion of the code fails and I am unable to
isolate the reason.

The specific error is:

Microsoft Visual Basic "Run-time error '1004':
Insert method of Range class failed

The section of the following code that fails is
"Selection.EntireRow.Insert". Any suggestions would be appreciated.
Thank you.

' select range for unique filter and insert rows

Range("A1").Select
Application.CutCopyMode = False
Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
ActiveSheet.ShowAllData


Ads
  #2  
Old June 13th 12, 02:31 PM posted to microsoft.public.excel.programming
GS[_2_]
external usenet poster
 
Posts: 2,276
Default "Run-time error '1004'

icystorm wrote :
> Greetings:
>
> In Excel 2007, I wrote a macro to insert a line between all visible
> rows. However, one portion of the code fails and I am unable to
> isolate the reason.
>
> The specific error is:
>
> Microsoft Visual Basic "Run-time error '1004':
> Insert method of Range class failed
>
> The section of the following code that fails is
> "Selection.EntireRow.Insert". Any suggestions would be appreciated.
> Thank you.
>
> ' select range for unique filter and insert rows
>
> Range("A1").Select
> Application.CutCopyMode = False
> Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
> Range("A2").Select
> Range(Selection, Selection.End(xlDown)).Select
> Selection.SpecialCells(xlCellTypeVisible).Select
> Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
> ActiveSheet.ShowAllData


You're trying to insert rows using a non-contiguous selection. The rows
must be contiguous to do as you're attempting. Try using a loop and
inserting rows one at a time, and specify 'shift:=xlDown' to place
the new row above.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #3  
Old June 13th 12, 05:50 PM posted to microsoft.public.excel.programming
icystorm
external usenet poster
 
Posts: 20
Default "Run-time error '1004'

On Jun 13, 8:31*am, GS > wrote:
> icystorm wrote :
>
>
>
>
>
> > Greetings:

>
> > In Excel 2007, I wrote a macro to insert a line between all visible
> > rows. However, one portion of the code fails and I am unable to
> > isolate the reason.

>
> > The specific error is:

>
> > Microsoft Visual Basic "Run-time error '1004':
> > Insert method of Range class failed

>
> > The section of the following code that fails is
> > "Selection.EntireRow.Insert". Any suggestions would be appreciated.
> > Thank you.

>
> > ' select range for unique filter and insert rows

>
> > * * Range("A1").Select
> > * * Application.CutCopyMode = False
> > * * Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
> > * * Range("A2").Select
> > * * Range(Selection, Selection.End(xlDown)).Select
> > * * Selection.SpecialCells(xlCellTypeVisible).Select
> > * * Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
> > * * ActiveSheet.ShowAllData

>
> You're trying to insert rows using a non-contiguous selection. The rows
> must be contiguous to do as you're attempting. Try using a loop and
> inserting rows one at a time, and specify *'shift:=xlDown' *to place
> the new row above.
>
> --
> Garry
>
> Free usenet access athttp://www.eternal-september.org
> ClassicVB Users Regroup!
> * * comp.lang.basic.visual.misc
> * * microsoft.public.vb.general.discussion


Thank you for your suggestion, Garry. I found a procedure that almost
accomplishes what I need, as shown below. However, it requires input
from the user which is unnecessary. How would I modify/define the
strKey section to simply insert a new row between all visible cells in
a single column? The column is already filtered and the visible cells
are selected. Thank you.

Sub AddRows()
Dim strKey As String
Dim lRows As Long

strKey = Application.InputBox(Prompt:="What keyword/number?",
Title:="Add Rows", Type:=1 + 2)
If strKey = vbNullString Then Exit Sub

For lRows = Selection.Columns(1).Cells.Count To 1 Step -1
If Selection.Columns(1).Cells(lRows, 1) = strKey Then
Selection.Columns(1).Cells(lRows, 1).EntireRow.Insert
End If
Next lRows

End Sub
  #4  
Old June 13th 12, 11:18 PM posted to microsoft.public.excel.programming
Don Guillett[_2_]
external usenet poster
 
Posts: 1,506
Default "Run-time error '1004'

On Wednesday, June 13, 2012 8:09:08 AM UTC-5, icystorm wrote:
> Greetings:
>
> In Excel 2007, I wrote a macro to insert a line between all visible
> rows. However, one portion of the code fails and I am unable to
> isolate the reason.
>
> The specific error is:
>
> Microsoft Visual Basic "Run-time error '1004':
> Insert method of Range class failed
>
> The section of the following code that fails is
> "Selection.EntireRow.Insert". Any suggestions would be appreciated.
> Thank you.
>
> ' select range for unique filter and insert rows
>
> Range("A1").Select
> Application.CutCopyMode = False
> Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
> Range("A2").Select
> Range(Selection, Selection.End(xlDown)).Select
> Selection.SpecialCells(xlCellTypeVisible).Select
> Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
> ActiveSheet.ShowAllData


something like this

for each c in range("filteredrange").specialcells(xlvisible)
c.entirerow.insert
next c
  #5  
Old June 13th 12, 11:22 PM posted to microsoft.public.excel.programming
GS[_2_]
external usenet poster
 
Posts: 2,276
Default "Run-time error '1004'

icystorm wrote :
> On Jun 13, 8:31*am, GS > wrote:
>> icystorm wrote :
>>
>>
>>
>>
>>
>>> Greetings:

>>
>>> In Excel 2007, I wrote a macro to insert a line between all visible
>>> rows. However, one portion of the code fails and I am unable to
>>> isolate the reason.

>>
>>> The specific error is:

>>
>>> Microsoft Visual Basic "Run-time error '1004':
>>> Insert method of Range class failed

>>
>>> The section of the following code that fails is
>>> "Selection.EntireRow.Insert". Any suggestions would be appreciated.
>>> Thank you.

>>
>>> ' select range for unique filter and insert rows
>>> * * Range("A1").Select
>>> * * Application.CutCopyMode = False
>>> * * Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
>>> * * Range("A2").Select
>>> * * Range(Selection, Selection.End(xlDown)).Select
>>> * * Selection.SpecialCells(xlCellTypeVisible).Select
>>> * * Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
>>> * * ActiveSheet.ShowAllData

>>
>> You're trying to insert rows using a non-contiguous selection. The rows
>> must be contiguous to do as you're attempting. Try using a loop and
>> inserting rows one at a time, and specify *'shift:=xlDown' *to place
>> the new row above.
>>
>> --
>> Garry
>>
>> Free usenet access athttp://www.eternal-september.org
>> ClassicVB Users Regroup!
>> * * comp.lang.basic.visual.misc
>> * * microsoft.public.vb.general.discussion

>
> Thank you for your suggestion, Garry. I found a procedure that almost
> accomplishes what I need, as shown below. However, it requires input
> from the user which is unnecessary. How would I modify/define the
> strKey section to simply insert a new row between all visible cells in
> a single column? The column is already filtered and the visible cells
> are selected. Thank you.
>
> Sub AddRows()
> Dim strKey As String
> Dim lRows As Long
>
> strKey = Application.InputBox(Prompt:="What keyword/number?",
> Title:="Add Rows", Type:=1 + 2)
> If strKey = vbNullString Then Exit Sub
>
> For lRows = Selection.Columns(1).Cells.Count To 1 Step -1
> If Selection.Columns(1).Cells(lRows, 1) = strKey Then
> Selection.Columns(1).Cells(lRows, 1).EntireRow.Insert
> End If
> Next lRows
>
> End Sub


I believe the intent of this code is to obviate the need for filtering,
and so strKey would be the same as your filter criteria. In this case
it could be input OR user can select any cell containing the criteria.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #6  
Old June 14th 12, 01:08 AM posted to microsoft.public.excel.programming
icystorm
external usenet poster
 
Posts: 20
Default "Run-time error '1004'

On Jun 13, 5:18*pm, Don Guillett > wrote:
> On Wednesday, June 13, 2012 8:09:08 AM UTC-5, icystorm wrote:
> > Greetings:

>
> > In Excel 2007, I wrote a macro to insert a line between all visible
> > rows. However, one portion of the code fails and I am unable to
> > isolate the reason.

>
> > The specific error is:

>
> > Microsoft Visual Basic "Run-time error '1004':
> > Insert method of Range class failed

>
> > The section of the following code that fails is
> > "Selection.EntireRow.Insert". Any suggestions would be appreciated.
> > Thank you.

>
> > ' select range for unique filter and insert rows

>
> > * * Range("A1").Select
> > * * Application.CutCopyMode = False
> > * * Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
> > * * Range("A2").Select
> > * * Range(Selection, Selection.End(xlDown)).Select
> > * * Selection.SpecialCells(xlCellTypeVisible).Select
> > * * Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
> > * * ActiveSheet.ShowAllData

>
> something like this
>
> for each c in range("filteredrange").specialcells(xlvisible)
> c.entirerow.insert
> next c


I tried that, but I still need a way to select all the visible cells.
Next, I modified a macro from a similar problem reported years ago,
but the way I have structured it still does not add rows. I think the
problem is where I set rngVisible. Is the syntax incorrect? Thanks.

---

Sub AddRows()

Dim rngVisible As Range
Dim rng As Range

Range("A1").Select
Application.CutCopyMode = False
Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select

On Error Resume Next
Set rngVisible = Selection.SpecialCells(xlCellTypeVisible).Select
On Error GoTo 0

If Not rngVisible Is Nothing Then
For Each rng In rngVisible.Areas
rng.EntireRow.Insert
Next rng
End If

End Sub
  #7  
Old June 15th 12, 01:14 AM posted to microsoft.public.excel.programming
icystorm
external usenet poster
 
Posts: 20
Default "Run-time error '1004'

On Jun 13, 7:08*pm, icystorm > wrote:
> On Jun 13, 5:18*pm, Don Guillett > wrote:
>
>
>
>
>
> > On Wednesday, June 13, 2012 8:09:08 AM UTC-5, icystorm wrote:
> > > Greetings:

>
> > > In Excel 2007, I wrote a macro to insert a line between all visible
> > > rows. However, one portion of the code fails and I am unable to
> > > isolate the reason.

>
> > > The specific error is:

>
> > > Microsoft Visual Basic "Run-time error '1004':
> > > Insert method of Range class failed

>
> > > The section of the following code that fails is
> > > "Selection.EntireRow.Insert". Any suggestions would be appreciated.
> > > Thank you.

>
> > > ' select range for unique filter and insert rows

>
> > > * * Range("A1").Select
> > > * * Application.CutCopyMode = False
> > > * * Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
> > > * * Range("A2").Select
> > > * * Range(Selection, Selection.End(xlDown)).Select
> > > * * Selection.SpecialCells(xlCellTypeVisible).Select
> > > * * Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
> > > * * ActiveSheet.ShowAllData

>
> > something like this

>
> > for each c in range("filteredrange").specialcells(xlvisible)
> > c.entirerow.insert
> > next c

>
> I tried that, but I still need a way to select all the visible cells.
> Next, I modified a macro from a similar problem reported years ago,
> but the way I have structured it still does not add rows. I think the
> problem is where I set rngVisible. Is the syntax incorrect? Thanks.
>
> ---
>
> Sub AddRows()
>
> Dim rngVisible As Range
> Dim rng As Range
>
> *Range("A1").Select
> *Application.CutCopyMode = False
> *Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
> *Range("A2").Select
> *Range(Selection, Selection.End(xlDown)).Select
>
> *On Error Resume Next
> *Set rngVisible = Selection.SpecialCells(xlCellTypeVisible).Select
> *On Error GoTo 0
>
> If Not rngVisible Is Nothing Then
> *For Each rng In rngVisible.Areas
> *rng.EntireRow.Insert
> *Next rng
> *End If
>
> End Sub


Thanks to Garry and Don again for your ideas. I have now answered my
own question and the issue is resolved. I do not know if my solution
(below) is "by the book", but it works perfectly for me.

Again, what this does is filters column A for unique values, selects
all visible cells in range A2:A50000, inserts a single row between all
visible cells, then clears the unique filter. I chose row 50000 as the
bottom of the range because I know I will never have that much data in
the column. Ideally, I would be able to select the entire column, but
I could not make it work that way. Regardless, it works perfectly for
me, as is. :-)

' select range, apply unique filter, select visible cells only

Range("A1").Select
Application.CutCopyMode = False
Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select

' add row between unique filtered, visible cells

Dim rngVisible As Range
Dim rng As Range

On Error Resume Next
Set rngVisible =
Range("A2:A50000").SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If Not rngVisible Is Nothing Then
For Each rng In rngVisible.Areas
rng.EntireRow.Insert
Next rng
End If

' remove unique filter to display all data

ActiveSheet.ShowAllData



 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time-error 1004 "ListIndex method of listbox class failed" Fan924 Excel Programming 1 October 19th 10 10:56 PM
how do I correct a "run time error 1004-unable to open" problem? BillR Excel Programming 1 August 26th 08 07:21 PM
"run-time error '1004' - select method of range failed" maemi weirdoke Excel Programming 2 March 5th 08 03:15 PM
Run-time error "1004" Select method of range class failed Tallan Excel Discussion (Misc queries) 3 March 7th 07 05:22 PM
"Run-time error '1004'" Method 'Range' of object '_global' failed. haisat[_2_] Excel Programming 0 October 20th 03 12:13 PM


All times are GMT +1. The time now is 02:47 AM.


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