last day (15 days later) » 

4:04 PM
@EliGreenberg :
"Dictionaries are proving to be very confusing for me compared to collections (which are also confusing haha). I have a sanitized data file. Could I send it you somehow along with the data structure I'm hoping to achieve and you provide some tips?"
Sure, I can have a look. Can you provide a link to download the file?
Also, if you feel like collection are easier to deal with and you don't mind the small limitation they have, feel free to use them instead. There is a list of differences between them here: stackoverflow.com/questions/32479842/…
The list is a bit long, so don't worry if you don't understand everything, but if you look at the image version of the comparison table, you can see that the Dictionary has fewer squares in red/pink which means it is more flexible to work with in general, but for some use cases, a collection can do the job.
 
OK, I will take a peek at that. I'm going to find a place to upload the Excel file.
The Excel file will include the data in the form in which we receive it, as well as my current conversion code in Module1. packersOrder is the class module.
Password is packers
So let me give you a little overview of what I'm trying to do. Essentially, we get orders in a certain format and I am trying to create a macro that automatically converts their data into the format our order management software understands. That's what OrderImporter() does. It mostly works right now, but I'm trying to figure out a way where I don't have to create all these different variables for each sku/qty per order
Let me know when you've looked at the file so I can explain what's going on I suppose
 
4:35 PM
I have no idea how to operate UML, so I'm sorry if that diagram makes no sense. But it does give the layout of what I'm trying to make for my class.
Once I've create a class object for each order number, the second part of the OrderImporter() sub transfers that information into the correct column on a new sheet.
I'm going to lunch but I'll be back in a few. Let me know if you have any questions.
 
Ok, I was able to open the file and run the code. So you confirm that the code is giving you the right results right now, right? It's only a matter of making this code more "scalable" for the case where there is more than SKU/Qty combinations right?
You class diagram makes it clear of want you are after, so thanks for making it. The issue I see is that SKUList is not just a string (key) and a long (value). The fact that you have more than a key value pair meant that you'll need to create another custom type/class that take those values as fields/properties.
For instance, you could create another class named skuDetails that takes :
- Sku
- Qty
- Description
- Cost
Then SKUList inside the packageOrder would be a Collection/Dictionary of those individual skuDetails object. Does that make sense?
 
5:02 PM
@DecimalTurn Yes, exactly! It does a pretty good job. I just can't figure out how to make it more efficient. I'm often guilty of writing code as non-pseudo pseudocode, lol. Very step by step, while often missing out on potential efficiencies. It doesn't help that I'm relatively newish to VBA.
OK, so I could make another class for the SKUList items, that makes sense. And I actually did do that, but I couldn't figure out how to iterate over only the SKUs for each unique order number and add it to that order number in the main collection.
The first collection is iterating over all the order numbers and creating a class item for each of them. Now I need to do that for all SKUs for a unique order number and match that with the order number of the main collection.
 
5:31 PM
So would the SKUList class module be something like this:
Option Explicit

Public SKU As String
Public Qty As Long
Public Description As String
Public Cost As Currency
Public SkuList As collection
Private Sub Class_Initialize()
SKU = vbNullString
Qty = 0
Description = vbNullString
Cost = 0
Set SkuList = New collection
End Sub
 
What I'm suggesting is to have SkuList as a collection inside packageOrder.
SkuList could then be a collection of skuDetails objects.
In this approach, Public SkuList As collection would not appear in the new class module for skuDetails (or whatever you want to call it)
 
Oh, OK. Would I add SkuList As collection to the packersOrder class then?
 
Yes, exactly
 
Alright, so how would could I get it to collect the SKUs for each unique order number (the key in the main collection)?
 
Then when creating your order, you could create your skuDetails objects like this and add them to the SkuList like this:
```vba
dim details as skuDetails
set details = new skuDetails
details.SKU = ...
...
order.SkuList.Add details
```
 
5:42 PM
Because I'm using the SKU as the key in the skulist collection
Ok, let me try that now.
 
Actually, if you want to use SKU as keys, you have to pass it to the collection when creating it like this:
order.SkuList.Add details, details.SKU
But if you plan to use those keys for referral later, a dictionary is probably easier to use
 
Yeah, I'm going to need to grab each sku and its properties in the transfer process to the new sheet
I get an object variable or with block variable not set error when I put order.skuList.Add skuList, skuList.SKU into the code
Dim coll As collection
Dim order As packersOrder
Dim orderNumber As String
Dim skuList As packersSkuList
Dim ws As Worksheet
Dim r As Long

Set ws = originalSheet
Set coll = New collection
Set skuList = New packersSkuList

For r = 2 To lastRow

'Retrieve Order Number for key assignment
orderNumber = CStr(ws.Cells(r, "C").Value2)

'Check if key already exists
Set order = Nothing: On Error Resume Next
Set order = coll(orderNumber): On Error GoTo 0
coll is the main collection. I suppose I have to make a skuList collection too?
 
The fact that you are trying to add skuLlist to skuList makes it look a bit confusing to me. How did you name the new class module that you created that contains SKU, Qty, Desc and Cost?
 
The class module is packersSkuList
Option Explicit

Public SKU As String
Public Qty As Long
Public Description As String
Public Cost As Currency
Private Sub Class_Initialize()
SKU = vbNullString
Qty = 0
Description = vbNullString
Cost = 0
End Sub
The order class module is packersOrder
Option Explicit

'List of class properties
Public poNumber As String
Public poDate As Date
Public poOrderNumber As String
Public poShipToName As String
Public poPhone As String
Public poAddress1 As String
Public poAddress2 As String
Public poAddress3 As String
Public poZIP As String
Public poCity As String
Public poState As String
Public poCountry As String
Public skuList As collection
 
5:57 PM
Well if you want to keep that naming, you'll need to replace Public skuList As collection by Public skuLists As collection (inside packersOrder). The additional "s" is to indicate that your collection contains multiple skuList objects. Otherwise, it's a bit confusing IMO.
 
Yeah, I can see where the confusion would be.
 
And then yes, you'll need to instanciate skuLists inside the order.
`set order.skuLists = new Collection`.
You can do that inside the module or inside the Class_Initialize method.
 
I still don't see how to add the skus to the skulist though
oh ok
Do I have to define order in the class module as well?
 
What do you mean by define?
 
I get a variable not defined error for:
Set order.skuLists = New collection
in the skulist class module
in class_initialize
 
6:05 PM
You don't need the 'order." part if you are inside the class module because the class module has direct access.
 
ah ok
Still get variable not defined for
Set skuLists = New collection
 
Do you have the line Public skuLists As Collection somewhere in the class module?
 
I just added it
Now a different error. The naming in my code is horrible
I have skuList, skuLists, packersSkuList
And not 100% sure what each of them is haha
 
Honestly, I've seen worse dont' worry XD
 
variable not set error for
order.skuLists.Add skuList, skuList.SKU
Here's the code again:

Public Sub PackersDropShipOrderImporter()
'Create a new sheet named "Drop Ship Order Importer" in current workbook
Dim newSheet As Worksheet
Dim originalSheet As Worksheet

Set originalSheet = ActiveSheet
Set newSheet = ActiveWorkbook.Sheets.Add(After:= _
ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count))
newSheet.Name = "Drop Ship Order Importer"

'Copy data from original PO to import file
Dim lastRow As Long
lastRow = originalSheet.Cells(Rows.Count, "A").End(xlUp).Row
The error is on the second to last line in the if block
 
6:15 PM
Hum, what's in packersOrder?
 
Option Explicit

'List of class properties
Public poNumber As String
Public poDate As Date
Public poOrderNumber As String
Public poShipToName As String
Public poPhone As String
Public poAddress1 As String
Public poAddress2 As String
Public poAddress3 As String
Public poZIP As String
Public poCity As String
Public poState As String
Public poCountry As String
Public skuLists As collection
 
Where is the Class_Initialize method?
 
It doesn't have one. Was Set skuLists = New collection supposed to go in packersOrder class module or packersSkuList class module?
Well that fixed it sort of haha
The code ran at least
However, theres only 1 SKU for the first order number when there should be 2
 
To answer that you can ask yourself what skuLists is a member of :)
 
Yeah that makes sense XD
 
6:21 PM
Yeah, I think you removed the else part of your code. That's where you would add the order skuList elements
 
Oh yeah, duh.
Now every order has the same SKU for all items lol
Which is the last sku on the list
Let me step through this and see if I can figure out whats going on
OK, so on the second loop through the first order number, it added the second SKU AND changed the first SKU to the second SKU
If order Is Nothing Then
Set order = New packersOrder
order.poOrderNumber = CStr(ws.Cells(r, "C").Value2)
order.poNumber = CStr(ws.Cells(r, "A").Value2)
order.poDate = CDate(ws.Cells(r, "B").Value2)
order.poShipToName = CStr(ws.Cells(r, "D").Value2)
order.poPhone = CStr(ws.Cells(r, "E").Value2)
order.poAddress1 = CStr(ws.Cells(r, "F").Value2)
order.poAddress2 = CStr(ws.Cells(r, "G").Value2)
order.poAddress3 = CStr(ws.Cells(r, "H").Value2)
order.poZIP = CStr(ws.Cells(r, "I").Value2)
order.poCity = CStr(ws.Cells(r, "J").Value2)
So obviously I'm missing something
Do I still have to keep count of the skus and use a select statement like in the original file?
 
I think part of the issue comes from the fact that Set skuList = New packersSkuList is outside of your loop. You should have it inside the loop because you want a new one for each row.
I think otherwise the code to generate the values in skuLists looks OK. Maybe the error comes from when you are writting the values to the new sheet
 
I disabled the write to new sheet aspect for now, since I need to get this figured out before that makes sense.
I will try moving it outside the loop
BINGO
Must be nice being a genius :/
 
6:37 PM
Moving what outside the loop? I suggested to move the line inside the loop instead.
 
Sorry thats what I meant, move inside the loop
 
Ok, good
Nice to know it works
 
Now to figure out how to reference that for the transfer process
coll.item(i).skuLists.SKU
Would that work?
 
No, you'd need a for each loop like discussed here : https://stackoverflow.com/questions/36270212/vba-looping-through-a-collection

I'll have to head out, but I'll have a look at this later if you have more questions.
 
OK, thank you for your help! I'll let you know if I have any more. Have a great day
 
6:42 PM
Thanks you too!
 
 
1 hour later…
7:59 PM
Am I going to have to make another collection/dictionary to transpose these SKUs horizontally onto the receiving sheet? :|
 
8:42 PM
No need for another collection, just loop over the collection and write to the sheet. You'll probably need to use the Offset method to write to the right range, but that should work
 
8:56 PM
Mind helping me with some Let and Get class module stuff? You've been super helpful already, so I don't want to impose.
 
 
2 hours later…
10:57 PM
If you think you can make that another question on SO, let me know and I can take a look, but I can't promise to have time in the following days
 

last day (15 days later) »