+ Reply to Thread
Results 1 to 6 of 6

Using Named Ranges to Transfer data from two worksheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-29-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    143

    Using Named Ranges to Transfer data from two worksheets

    Attached is a sample of the problem that I'm working on. I'm trying to create a dashboard of my inventory system. The dashboard will display Sales for the day (category 1), Purchases that need to be made (Category 2), Purchase Orders that are to be expected (Cateogry 3), and Work in Process (Category 4). For this question, I'm only going to focus on category 2.

    I'm trying to transfer all the data from Worksheets("Purchasing") to the Dashboard under category 2. I'm trying to use named ranges to do this, because the range of each category will fluctuate as items are added/deleted. This is what I have so far:

    Option Explicit
    
    Sub purchPull()
    
    Dim Dashboard As Worksheet
    Dim Purchasing As Worksheet
    Dim PM As Range, D As Range, Rng As Range
    Dim purchName As Range
    
    Set Purchasing = Worksheets("Purchasing")
    Set Dashboard = Worksheets("Dashboard")
    
    
    ' Go through each Item in Purchasing and check to see if it's anywhere within the named range "PurchaseStart"
    ' In this case it should be "A8:A9" - as there is nothing in the dasboard yet
    For Each PM In Purchasing.Range(Purchasing.Cells(1, 1), Purchasing.Cells(Purchasing.Rows.Count, 1).End(xlUp))
        With Dashboard.Range("PurchaseStart", Dashboard.Cells(Dashboard.Rows.Count, 1))
            Set Rng = .Find(What:=PM.Offset(0, 1), _
                After:=.Cells(.Cells.Count), _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False)
            If Not Rng Is Nothing Then
                ' Do nothing, as we don't want duplicates
            Else
                ' From the start of the named range, transfer data over - THIS IS THE PROBLEM AREA
                With Dashboard.Range("PurchaseStart", Dashboard.Cells(.Rows.Count, 1)).End(xlUp)
                    .Offset(1, 1) = PM.Offset(0, 0) ' Order Number
                    .Offset(1, 2) = PM.Offset(0, 1) ' SKU
                    .Offset(1, 3) = PM.Offset(0, 3) ' Qty
                    .Offset(1, 4) = PM.Offset(0, 4) ' Date
                End With
            End If
        End With
    Next
    
    End Sub
    For some reason the Range("PurchaseStart"), which is Cell $A$8, starts at A:1. I don't know how to only select the named range that I'm looking for. I added "End #" statements at the end of each row to signify a cutoff and hope to trick excel into only selecting a the range of the particular category. Any help would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    45,056

    Re: Using Named Ranges to Transfer data from two worksheets

    Maybe:

    ?Range("PurchaseStart").resize(Dashboard.Rows.Count-7, 1).address
    $A$8:$A$1048576
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Using Named Ranges to Transfer data from two worksheets

    Hello Blackhawks,

    How about a VBA free approach as per the attached sample Workbook? It will also eliminate the need to insert Rows in your Dashboard Sheet.

    Regards.
    Attached Files Attached Files
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Forum Contributor
    Join Date
    05-29-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    143

    Re: Using Named Ranges to Transfer data from two worksheets

    I figured it out. If anyone can think of a better way, I'd love to hear it. Thanks for your help everyone!

    Option Explicit
    
    Sub purchPull()
    
    Dim Dashboard As Worksheet
    Dim Purchasing As Worksheet
    Dim PM As Range, D As Range, Rng As Range
    Dim purchName As Range
    Dim lastRow As Long
    Dim firstRow As Long
    
    Set Purchasing = Worksheets("Purchasing")
    Set Dashboard = Worksheets("Dashboard")
    
    ' first row of named range "PurchaseStart"
    firstRow = Dashboard.Range("PurchaseStart").Row + Dashboard.Range("PurchaseStart").Rows.Count
    
    
    
    ' Go through each Item in Purchasing and check to see if it's anywhere within the named range "PurchaseStart"
    With Purchasing
        For Each PM In Purchasing.Range(Purchasing.Cells(2, 1), Purchasing.Cells(Purchasing.Rows.Count, 1).End(xlUp))
            With Dashboard.Range("PurchaseStart", Dashboard.Cells(Dashboard.Rows.Count, 1))
                Set Rng = .Find(What:=PM.Offset(0, 0), _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)
                If Not Rng Is Nothing Then
                    ' Do nothing, as we don't want duplicates
                Else      
                    ' Identify the last row within the named range "PurchaseStart"
                    lastRow = Dashboard.Range("PurchaseStart").Cells(1, 1).End(xlDown).Row
                    ' Transfer the data over
                    With Dashboard.Cells(lastRow, 1).End(xlUp)
                        .Offset(1, 0).EntireRow.Insert
                        .Offset(1, 0) = PM.Offset(0, 0)  ' Order Number
                        .Offset(1, 1) = PM.Offset(0, 1) ' SKU
                        .Offset(1, 2) = PM.Offset(0, 2) ' Qty
                        .Offset(1, 3) = PM.Offset(0, 3) ' Date
                    End With
                End If
            End With
        Next
    End With
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    05-29-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    143

    Re: Using Named Ranges to Transfer data from two worksheets

    Thank you to both of you. Winon, I'm surprised i never thought of putting them horizontally. However, I figured out how to do it. But I'm still stuck on one part of this problem and that's when it comes to making sure duplicates aren't passed to the Dashboard. Right now, it's just overwriting the first row (.Offset(1, 0)). I would like it to go to the last available cell instead.

    I was thinking I could just delete the entire range and re-calculate, but that seems unecessary. Here's the code I have:

    Option Explicit
    
    Sub purchPull()
    
    Dim Dashboard As Worksheet
    Dim Purchasing As Worksheet
    Dim PM As Range, D As Range, Rng As Range
    Dim purchName As Range
    Dim lastRow As Long
    Dim firstRow As Long
    
    Set Purchasing = Worksheets("Purchasing")
    Set Dashboard = Worksheets("Dashboard")
    
    firstRow = Dashboard.Range("PurchaseStart").Row + Dashboard.Range("PurchaseStart").Rows.Count
    
    lastRow = Dashboard.Range("PurchaseStart").Cells(1, 1).End(xlDown).Row
    
    
        MsgBox lastRow
        MsgBox firstRow
    
    ' Go through each Item in Purchasing and check to see if it's anywhere within the named range "PurchaseStart"
    ' In this case it should be "A8:A9" - as there is nothing in the dasboard yet
    For Each PM In Purchasing.Range(Purchasing.Cells(2, 1), Purchasing.Cells(Purchasing.Rows.Count, 1).End(xlUp))
        With Dashboard.Range("PurchaseStart", Dashboard.Cells(Dashboard.Rows.Count, 1))
            Set Rng = .Find(What:=PM.Offset(0, 0), _
                After:=.Cells(.Cells.Count), _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False)
            If Not Rng Is Nothing Then
                ' Do nothing, as we don't want duplicates
            Else
                ' From the start of the named range, transfer data over
          ' Rows(firstRow & ":" & lastRow).EntireRow.Delete
    
                With Dashboard.Cells(lastRow, 1).End(xlUp)
                    .Offset(1, 0) = PM.Offset(0, 0) ' Order Number
                    .Offset(1, 1) = PM.Offset(0, 1) ' SKU
                    .Offset(1, 2) = PM.Offset(0, 2) ' Qty
                    .Offset(1, 3) = PM.Offset(0, 3) ' Date
                    .Offset(2, 1).EntireRow.Insert
                End With
            End If
        End With
    Next
    
    End Sub

  6. #6
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Using Named Ranges to Transfer data from two worksheets

    Nice work Blackhawks, you did good. Thank you for the "Rep", much appreciated!

    Regards.

+ 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. Replies: 5
    Last Post: 12-17-2014, 12:23 AM
  2. [SOLVED] Named Ranges on Multlple Worksheets
    By ptmuldoon in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-28-2014, 11:15 PM
  3. Replies: 4
    Last Post: 02-06-2014, 11:13 PM
  4. Replies: 7
    Last Post: 05-27-2013, 01:40 PM
  5. Excel 2007 : sum named ranges in different worksheets
    By joethengineer in forum Excel General
    Replies: 1
    Last Post: 08-26-2011, 02:44 PM
  6. Accessing Named Ranges on different worksheets
    By gladst_j in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-10-2011, 09:55 AM
  7. Copy data in named ranges to a newer version of the same template to identical ranges
    By handstand in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-21-2006, 10:51 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