Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default problem with offset -how to re-write line?

The following very simple code generates a 1004 error (* indicates line).
Help required! Thanks. I think the problem may be due to the fact that he
usedrange starts from row 1, therefore during the AND logical test offset(-1)
refers to a row that does not exist. However I am unsure of the best way to
re-write this. The purpose of the AND test is to ignore rows that have just
been inserted, otherwise the macro inserts multiple rows above cll.


subxy()

dim cll as range
dim sht as worksheet

'for each sheet in workbook

for each sht in thisworkbook.worksheets

'for each cell in column 2 of the usedrange


For Each cll In sht.UsedRange.Columns(2).Cells

' if the value of the cell is "xy", and the cell above it is not 0, insert a
row.
If cll.Value = "xy" And cll.Offset(-1, 0).value < 0 Then*

cll.EntireRow.Insert

Else: End If
Next cll

next sht

end sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default problem with offset -how to re-write line?

Various ways -

For Each cll In sht.UsedRange.Columns(2).Cells
if cll.row = 2 then

In theory the UR might start in row 2 or lower and I don't suppose you'd
want to process the first row, even if it doesn't fail

for each sht in thisworkbook.worksheets
bFlag = True
For Each cll In sht.UsedRange.Columns(2).Cells
If bFlag then
bFlag = false
else
' code to check and insert etc
End if

Another thing, do you want to process sheets in which the UR is only one
column, eg an empty sheet. If not, skip the sheet if
sht.UsedRange.Columns.Count < 2

Regards,
Peter T


"PBcorn" wrote in message
...
The following very simple code generates a 1004 error (* indicates line).
Help required! Thanks. I think the problem may be due to the fact that he
usedrange starts from row 1, therefore during the AND logical test
offset(-1)
refers to a row that does not exist. However I am unsure of the best way
to
re-write this. The purpose of the AND test is to ignore rows that have
just
been inserted, otherwise the macro inserts multiple rows above cll.


subxy()

dim cll as range
dim sht as worksheet

'for each sheet in workbook

for each sht in thisworkbook.worksheets

'for each cell in column 2 of the usedrange


For Each cll In sht.UsedRange.Columns(2).Cells

' if the value of the cell is "xy", and the cell above it is not 0, insert
a
row.
If cll.Value = "xy" And cll.Offset(-1, 0).value < 0 Then*

cll.EntireRow.Insert

Else: End If
Next cll

next sht

end sub



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
How do i write an offset code ? Corey Excel Programming 1 January 4th 07 01:01 AM
How do i write an offset code ? Corey Excel Programming 12 January 4th 07 12:18 AM
To Draw A Line Parallel To A Curve At Particular Offset vijay4u Charts and Charting in Excel 1 August 9th 06 04:13 AM
Write Line problem when writing simplified chinese characters KI LEE Excel Programming 7 August 17th 05 05:41 AM
Macro problem on, Yellowed line - previous line or next line. Ed Excel Programming 7 March 29th 05 09:37 PM


All times are GMT +1. The time now is 03:13 AM.

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

About Us

"It's about Microsoft Excel"