Results 1 to 3 of 3

Import data from source to target

Threaded View

  1. #2
    Registered User
    Join Date
    04-10-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Import data from source to target

    Hello All,
    My Goal is to import data from another workbook (Source) to Active Workbook (Target) while reading through all the "filtered" data on the Source Workbook (Row 13 and beyond to say 5000 rows max) while only importing the Product # (column A on both sheets) and the ordered Qty from the Source Sheet (Column M) to Ordered Qty on the Target (Column D). I've got a limit of occurrences that can occur on the Target Workbook of 501 possible lines of items that can be ordered. Max should be around 300 but, 501 is a safe range for future. Initially everything worked great and brought everything across throughout the entire range of 501 but, it even brought the filtered out blanks data as well (which is what it was spose to do). I need to read through each Product and Qty Ordered from the Source Workbook while skipping that row and going to next if the Ordered Qty is "" (blank)....and most likely filtered not to show. My initial code above does not take into account possible filtered blanks rows in between the ones I tested and I thought of that as I was typing this response so, I'd need to add that in as well. I'm having current issues where noted Error and ****** to note it.
    TIA, khleisure


     
    Private Sub ImportExternalDataToOrderForm_Click()
    '*******Exit Sub - Used to disable command button till sub written/executes properly
         ' Get customer workbook...
    Dim customerBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim customerFilename As String
    Dim customerWorkbook As Workbook
    Dim targetWorkbook As Workbook
     
    
         ' Active workbook is the Target Workbook
    Set targetWorkbook = Application.ActiveWorkbook
     
         ' get the customer workbook to use as Source WorkBook
    filter = "XLS files (*.xls),*.xls"
    caption = "Please Select an input file "
    customerFilename = Application.GetOpenFilename(filter, , caption)
     
    Set customerWorkbook = Application.Workbooks.Open(customerFilename)
     
         ' Ranges vary in Source Workbook to Target Workbook but, applicable data to import to Order Form
         'Import data from customer(source) to target workbook(active Order Form)
    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(2)
    Dim sourceSheet As Worksheet
    Set sourceSheet = customerWorkbook.Worksheets(1)
     
    targetSheet.Range("B4").Value = sourceSheet.Range("C2").Value ' Works Great
    targetSheet.Range("B9").Value = sourceSheet.Range("C8").Value ' Works Great
    targetSheet.Range("G9").Value = sourceSheet.Range("C9").Value 'Works Great
    targetSheet.Range("N4:N6").Value = sourceSheet.Range("N2:N4").Value ' Works Great
    targetSheet.Range("J18:J20").Value = sourceSheet.Range("K7:K9").Value ' Works Great
     
         ' Below 2 lines work great however, the Source Workbook is filtered to eliminate
         ' blank lines and the 2 lines of code below bring over everything in the overall range
         ' of 501 lines regardless if it's filtered or not.  Blank Qty fields that have
         ' been filtered should not be imported.  Max lines to import is defined by range
         ' of 501 max
     
         'below xfers the Part Number from A column range of Source to A column Range of Target
    'targetSheet.Range("A27:A527").Value = sourceSheet.Range("A13:A513").Value ' Do what their spose to
         'below xfers the Ordered Qty from M column range of Source to D column Range of Target
    'targetSheet.Range("D27:D527").Value = sourceSheet.Range("M13:M513").Value ' Do what their spose to
         '*****
         'Need loop to read through each row and import Source Range "A" to Target Range "A" along with
         'associated Source Range "M" to Target Range "D".  Max 501 lines
     
         '*****
         ' Need to use loop for Part Number and associated Order Qty
    Dim t As Long
    Dim s As Long
    Dim i As Long
    '*****
     
    t = 27     ' row number on target where Product # (Col A) and Order Qty (Col D) start
    s = 13     ' row number on Source where Product # (Col A) and Order Qty (Col M) start
    i = 1        ' set counter for total of 501 potential import occurrences Max
                  ' Need to establish reading potential Source rows (filtered or not) at 5000 max rows (most likely range of 3500)
                  ' for most factories and their offerings.  (Have not established this portion yet)
     
    For i = 1 To 501 Step 1
       If sourceSheet.Range("M(s)").Value = "" Then ' Error Here ****************
                                                     'Method 'Range' of object '_Worksheet' failed
         Next i
         Exit Sub
       Else
         targetSheet.Range("A(t)").Value = sourceSheet.Range("A(s)").Value ' xfer Part #
         targetSheet.Range("D(t)").Value = sourceSheet.Range("M(s)").Value ' xfer Order Qty
       End If
       t = t + 1
       s = s + 1
    Next i
     
        ' Close Customer(Source) workbook[/COLOR]
    customerWorkbook.Close
     
    End Sub
    Last edited by khleisure; 05-23-2012 at 04:09 PM.

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