+ Reply to Thread
Results 1 to 2 of 2

Copy rows with criteria to different sheet & automatically add formula

  1. #1
    Registered User
    Join Date
    08-10-2009
    Location
    Houston, TX
    MS-Off Ver
    Excel 2007
    Posts
    4

    Question Copy rows with criteria to different sheet & automatically add formula

    Hi! Thank you for having me on this Forum.
    I have no experience with VBA and very little with excel. So believe me, I really appreciate any help you can give me! I have gotten help in creating a code for an urgent project I need to complete.

    Here are the details:
    I have a workbook with various sheets. On the sheet named “Pulled Data”, where data is refreshed constantly from a database connection. I am pulling two sets of data Set 1 (Column A-N) and Set 2 (Columns P-AC). The VBA code below looks for values under Column M and matches them with Column AB. For each value, it copies the rows on set 1 (columns A-N) and below it the rows from Set 2 (columns P-AC) that have the same value in column AB as in column M, and colors the rows to form color coded groups with the rows that have the matching values (Recipe) and pastes them on the “Run” Sheet. This code only looks at the data on Set 1 if column N is blank, and does not copy these rows to the “Run” Sheet. Can the rows that have values in column N be copied to the bottom of the list that has been created on the Run Sheet? And also be color coded by the same criteria on column M?
    Another thing I need to do, is that when the data is updated from the database connection, the formulas that I have on the columns that are near the pulled data blank out. Can the code be modified so that after refreshing the data, the following formulas are added in the corresponding columns and copied to the last row containing data?

    column J --> =VLOOKUP(G2,'Recipe Data'!$A$1:$C$18,3,FALSE)
    column M --> =VLOOKUP(G2,'Recipe Data'!$A$1:$C$20,2,FALSE)
    column AB --> =VLOOKUP(V2,'Recipe Data'!$A$1:$C$17,2,FALSE)

    These are the formulas at row 2, so that G2 and V2 just roll down to correspond to each row.

    Oh!, another important thing, the numbers that are imported into the Run sheet from the Pulled data sheet are stored as text, not numbers, therefore my formulas don't work. Is there anything that can be added to the code so the numbers can be changed from being stored as text to number format?

    I hope I’ve explained myself well enough. Please let me know if more clarification is needed.

    Thank you for your help & time,
    Andrea.

    Here's The current code:
    (Sorry I just copy/past, since I don't know how to insert code in here)

    Option Explicit
    Sub Extract()
    Dim wsRun As Worksheet
    Dim wsData As Worksheet
    Dim cel As Range
    Dim Recipe As Range
    Dim c As Range
    Dim Data1 As Range
    Dim Data2 As Range
    Dim dic, a, i As Long
    Dim Rng As Range
    Application.ScreenUpdating = False
    Set wsRun = Sheets("Run")
    Set wsData = Sheets("Pulled Data")

    With wsData
    .AutoFilterMode = False
    Set Data1 = Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp)).Resize(, 14)
    Set Data2 = Range(.Cells(2, 16), .Cells(Rows.Count, 16).End(xlUp)).Resize(, 14)
    Set Recipe = Range(.Cells(2, 13), .Cells(Rows.Count, 13).End(xlUp))
    End With
    With Range(wsRun.Cells(2, 1), wsRun.Cells(Rows.Count, 14))
    .ClearContents
    .Interior.ColorIndex = xlNone
    End With

    Set Rng = Intersect(Recipe.Resize(, 2), Recipe.Offset(, 1).SpecialCells(xlCellTypeBlanks))
    Set Rng = Rng.Offset(, -1)
    'Get list of unique items
    Set dic = CreateObject("Scripting.Dictionary")
    On Error Resume Next
    For Each cel In Rng
    dic.Add cel.Text, cel.Text
    Next
    On Error GoTo 0
    a = dic.Items 'Get the items
    For i = 0 To dic.Count - 1 'Iterate the array
    With wsData.Columns("M:N")
    .AutoFilter Field:=1, Criteria1:=a(i)
    .AutoFilter Field:=2, Criteria1:="="
    Data1.SpecialCells(xlCellTypeVisible).Copy wsRun.Cells(Rows.Count, 1).End(xlUp).Offset(1)
    .Columns("M:N").AutoFilter
    End With
    Set c = wsData.Columns(28).Find(a(i), LookIn:=xlValues)
    If Not c Is Nothing Then
    wsData.Columns(28).AutoFilter Field:=1, Criteria1:=a(i)
    Data2.SpecialCells(xlCellTypeVisible).Copy wsRun.Cells(Rows.Count, 1).End(xlUp).Offset(1)
    wsData.Columns(28).AutoFilter
    End If
    Next
    'Get list of unique items
    Set dic = Nothing
    Set dic = CreateObject("Scripting.Dictionary")
    On Error Resume Next
    For Each cel In Range(wsData.Cells(2, 28), wsData.Cells(Rows.Count, 28).End(xlUp))
    dic.Add cel.Text, cel.Text
    Next
    On Error GoTo 0
    a = dic.Items 'Get the items
    For i = 0 To dic.Count - 1 'Iterate the array
    Set c = Rng.Find(a(i), LookIn:=xlValues)
    If c Is Nothing Then
    wsData.Columns(28).AutoFilter Field:=1, Criteria1:=a(i)
    Data2.SpecialCells(xlCellTypeVisible).Copy wsRun.Cells(Rows.Count, 1).End(xlUp).Offset(1)
    wsData.Columns(28).AutoFilter
    End If
    Next
    i = 0
    For Each cel In Range(wsRun.Cells(2, 13), wsRun.Cells(Rows.Count, 13).End(xlUp))
    If IsError(cel) Then cel.Value = "Error"
    If cel <> cel.Offset(-1) Then i = i + 1
    If i Mod 2 = 1 Then cel.Offset(, -12).Resize(, 13).Interior.ColorIndex = 35
    Next
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Registered User
    Join Date
    08-10-2009
    Location
    Houston, TX
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Copy rows with criteria to different sheet & automatically add formula

    Does anybody know if this is possible? If so, does anyone have any suggestions as to how I can achieve what I am trying to do?

    Thanks, Andrea.

+ 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