+ Reply to Thread
Results 1 to 4 of 4

Going Backwards: How Not to go past beginning of file

  1. #1
    Registered User
    Join Date
    03-14-2005
    Posts
    21

    Going Backwards: How Not to go past beginning of file

    I have a macro that works from the bottom of the file towards the top. A series of for/next loops copy and paste data in 100 line chuncks starting at the bottom. Works great until it gets 99 lines from top of file then it crashes.

    Here's the VBA that crashes:

    ActiveCell.Offset(-101, 0).Range("A1").Select

    I tried
    If BOF = true then end
    elseif
    ActiveCell.Offset(-101, 0).Range("A1").Select
    end if

    but that didn't work.

    any ideas?

    Rob

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Rob

    It's not suprising it crashes - if you are on row 99 and offset -101 then you'll be trying to select row -2 (or something like that).

    Why not check the row of the ActiveCell before the offset?
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    03-14-2005
    Posts
    21

    didn't work because of position in for/next

    thanks for the idea. i cannot get it to work because of the position of the activecell.offset statement in the for next loop. here's the whole macro.

    Sub Macro3()
    ' Macro3 Macro
    ActiveCell.Columns("A:B").EntireColumn.Select
    rowcnt = Worksheets("sheet1").Range("A:B").Rows.Count
    Selection.SpecialCells(xlCellTypeLastCell).Select
    ActiveCell.Rows("1:1").EntireRow.Select
    Selection.Copy
    For i = 1 To 100
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveSheet.Paste
    Next
    ActiveCell.Offset(-101, 0).Range("A1").Select
    For j = 1 To rowcnt
    ActiveCell.Rows("1:1").EntireRow.Select
    Selection.Copy
    For k = 1 To 100
    ActiveCell.Range("A1:B1").Select
    Selection.Copy
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.Insert Shift:=xlDown
    Next
    ActiveCell.Offset(-101, 0).Range("A1").Select
    Next
    End Sub

    Any variation i could think of on an if activecell=100 caused a loop at that point that seemed infinite.

    Suggestions?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Can you explain exactly what the code is meant to do?

    By the way you don't need to select/activate cells and ranges to work with them.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1