+ Reply to Thread
Results 1 to 3 of 3

Macro to insert row if cell(i,1) notequal cell(i,1)+1 and insert certain values in row.

  1. #1
    Registered User
    Join Date
    10-15-2013
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    2

    Macro to insert row if cell(i,1) notequal cell(i,1)+1 and insert certain values in row.

    Hi,
    I have data with 20000 observations over time. Time span is 1969 to 2011 for different categories. Not for every category there are observations for every year, so I would like to insert a row for the missing years, containing the value 0 in the "observation" cell.

    Example:

    Year Age Observation
    1969 0 4
    1970 0 17
    1972 0 6
    ...
    ...
    1969 1 5
    1970 1 22
    ....

    You see for example that for age 0 the year 1971 is missing, as there are no observations. I would like to insert a row here, indicating 0 obervations.

    My macro looks like this so far:

    Sub macro1()
    Dim i As Long
    i = 3
    Do Until Trim(Cells(i, 1)) = ""
    If Cells(i, 1) <> Cells(i - 1, 1)+1 Then
    Cells(i, 1).EntireRow.Insert
    Cell(i + 1, 1) = Cell(i, 1)+1
    Cell(i + 1, 2) = Cell(i, 2)
    Cell(i + 1, 3) = 0
    i = i + 2
    Else
    i = i + 1
    End If
    Loop
    End Sub


    It is not working...what am I doing wrong?


    Thanks a lot!!
    Last edited by mareike; 10-15-2013 at 10:59 AM.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro to insert row if cell(i,1) notequal cell(i,1)+1 and insert certain values in row

    Please use code tags in the future when posting codes.

    Try this:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    10-15-2013
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Macro to insert row if cell(i,1) notequal cell(i,1)+1 and insert certain values in row

    Great, thank you so much for the super quick help, millz!!

    The code works very well and without errors now! I edited it a little though, as it counted the years on to infinity, instead of starting after year 2011 again at 1969 for the next age class. In case somebody ever needs sth like this again, here the final version.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to insert cell values in another sheet depending on value and format
    By adw223 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-27-2013, 03:57 PM
  2. Replies: 1
    Last Post: 07-11-2012, 01:10 PM
  3. After insert new record, insert values in row based on first cell in row
    By frankienap in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2012, 11:36 AM
  4. Replies: 1
    Last Post: 08-06-2005, 01:05 AM
  5. Macro to insert formula result into range with zero values in cell
    By JPS in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-06-2005, 01:05 AM

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