+ Reply to Thread
Results 1 to 5 of 5

Fill in missing data

Hybrid View

Jbm444 Fill in missing data 06-16-2010, 10:46 AM
snb Re: Fill in missing data 06-16-2010, 11:19 AM
Jbm444 Re: Fill in missing data 06-16-2010, 11:34 AM
snb Re: Fill in missing data 06-16-2010, 11:37 AM
Jbm444 Re: Fill in missing data 06-16-2010, 11:42 AM
  1. #1
    Registered User
    Join Date
    06-11-2010
    Location
    grinnell, iowa
    MS-Off Ver
    Excel 2007
    Posts
    79

    Fill in missing data

    Hey,
    I have a dataset filled from A1 to E35383. A contains dates, but only the first of each month, and it switches months every few thousand rows. There is a certain term in column B, let's say "Pretzels," that appears once in about 4 of these months in non-consecutive months. In other words, I have data for Pretzels for May, January, and December, but not any of the other months. I need to add in a line for Pretzels for the months where it is missing, and just have the corresponding C, D, and E cells say "0" to denote that there isn't data for Pretzels for that month. Here's what I've been thinking for the code so far, but it seems very inefficient and obviously it doesn't work, but it might help you get the idea for what I'm trying to accomplish:

    Sub pretzels()
    With Sheet1
    Dim founddates As Long
    Dim missingdates As Long
    Dim totaldates As Range
    Dim Rowcount As Long
    Dim Lastrow As Long
    totaldates = 6/1/2009 to 5/1/2010
    Lastrow = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Rowcount = 1
    For Rowcount = 1 To Lastrow
    If .Cells(Rowcount, 2).Value Like "*pretzels*" Then
    founddates = .Cells(Rowcount, 1).Value
    End If
    Next Rowcount
    End With
    missingdates = founddates - totaldates
    For Rowcount = 1 To Lastrow
    If .Cells(Rowcount, 1).Value Like "*missingdates*" And .Cells(Rowcount, 2).Value Like "*Muffins*" Then
    'Muffins is a value in B that appears once and only once for each month, so we can add in missing Pretzel data below it safely
    .Cells(Rowcount + 1, 2).Select
        Selection.EntireRow.Insert
    .Cells(Rowcount, 2).Offset(1, 0).Value = "pretzels"
    .Cells(Rowcount, 2).Offset(1, -1) = ActiveCell.Offset(-1, -1).Value
    .Cells(Rowcount, 2).Offset(1, 1) = "0"
    .Cells(Rowcount, 2).Offset(1, 2) = "0"
    .Cells(Rowcount, 2).Offset(1, 3) = "0"
    End If
    Next Rowcount
    End Sub
    If this is the most efficient way to go, then I'd need help getting the missingdates = totaldates - founddates piece to work, I think. If anyone has another solution (which I'm sure exists), please throw it out there. Thanks for the help.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Fill in missing data

    Take care that the last cell of the usedrange in column A has some value (if the last row in column B is 1277, then put a space in cell A1278).
    Sub tst()
        On Error Resume Next
        With Sheets(1).UsedRange.Columns(2)
            .AutoFilter 1, "String"
            c01 = .Offset(1).SpecialCells(xlCellTypeVisible).Address
            .AutoFilter
        End With
        
        With Sheets(1).UsedRange.Columns(1).SpecialCells(2)
            For j = .Areas.Count To 2 Step -1
               If Intersect(Sheets(1).Range(c01), .Areas(j - 1).Offset(, 1).Resize(.Areas(j).Row - .Areas(j - 1).Row)) Is Nothing Then
                 .Areas(j - 1).Offset(1).EntireRow.Insert
               End If
               Err.Clear
            Next
        End With
    End Sub

  3. #3
    Registered User
    Join Date
    06-11-2010
    Location
    grinnell, iowa
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: Fill in missing data

    snb,
    This looks very helpful, but I'm not familiar enough with the code you're using to know what to change for my worksheet (other than changing "string" to "pretzel," presumably). Can you add comments to explain how the code works so that I can go through it and figure out where it's going wrong? Thanks for the help.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Fill in missing data

    I 'avn't got the faintest.. of your sheet, so use F8 in the VBEditor.

    columns(1) = column A
    columns(2) = column B

  5. #5
    Registered User
    Join Date
    06-11-2010
    Location
    grinnell, iowa
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: Fill in missing data

    Your code runs without an error message, it simply doesn't create any new rows. If you explain how your code is supposed to work, I might be able to figure out what's going on. Sorry I can't post up my worksheet, but an explanation of the code should help out a lot.

+ 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