I've been searching for a method to convert the "Excellent Answer" (VBA Code) in the below answer from "JDH". (I did not feel it was appropriate to try and contact directly for further help with his answer below)
The VBA answer in the below response/answer is 90% perfect for what I need except that once I get to reading the data for my Part# and Order Qty in the Source Workbook (Source could be up to 5000 lines of products and has been filtered down to hide rows with blank Order Qty's), the below VBA will copy ALL data within the range whether it's been filtered or not.
(Below is 90+% close to what I need) https://stackoverflow.com/a/7878070/1413702
I've modified the code to work for my instance and everything works great until I get to having to read through the data for Part # & Order Qty's that are not blank. I only want to bring over the Part # and Order Qty if the Order Qty is not blank and realize I need to possibly read through the entire range of 5000 lines to insure getting all potentially ordered items. If it was a straight source range to target range, the above would be perfect however, since there's the possibility the Source has been filtered, there's hidden rows within the range that need to be checked for Order Qty Blank. Also, there's an overall LIMIT to the number of import occurrences that can occur at this point because the order form is only set up for 501 max lines. 300 is a general rule with 501 being the safeguard. My revision is below however, I have not accounted for reading through potentially 5000 potential lines as that was an afterthought and I'm getting an error right out of the shoot that's noted when I tried to check for a blank value. Please help if you can and, again, please advise if I've posted incorrectly. I will change whatever is necessary to conform to forum rules. Thank You, khleisure
My Revised Code of "JDH's" excellent answer as follows:
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
' blanks in Order Qty Field (Starting Source M13) and the 2 lines of code below bring
' over everything in the overall range of 501 possible occurrences regardless if it's
' filtered or not. Blank Order 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 and works great except no function to check for blanks in Order Qty
' Below works exactly how it's written to work
'targetSheet.Range("A27:A527").Value = sourceSheet.Range("A13:A513").Value
'below xfers the Ordered Qty from M column range of Source to D column Range of
'Target and this is where I need to check if a qty has been ordered (or not =
'blank) in order to perform the above import and this import. The 2 are
'relational to one another
' Below works exactly how it's written to work but, needs to 1st check for blank
'targetSheet.Range("D27:D527").Value = sourceSheet.Range("M13:M513").Value
'*****My attempt to modify further to account for blank value
'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