array values to worksheet

mcranmoss

Board Regular
Joined
Dec 13, 2011
Messages
165
I have checked that the array is populated but the following code is failing to tranfer the array values to column A of the 'Category' sheet.

Any advice welcome.


Code:
For Each x In tmpArray
 
 Cells(Sheets("Category").Range("A2").End(xlDown).Row, 1).Value = x
 
Next x

thanks
mark
 
Is the entire intention for this routine to pull out unique product codes? Or will there be more to it?
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Sub DumpArray()

    Dim tmpArray As Variant
    Dim ProdCode As Variant
    
    'Dump the range into a 2D array
    LastRow = Sheets("Matrix Codes").Range("A2").End(xlDown).Row
    lastCol = Sheets("Matrix Codes").Range("A2").End(xlToRight).Column
    
    With Sheets("Matrix Codes")
      tmpArray = .Range(.Cells(2, 1), .Cells(LastRow, lastCol))
    End With

    'Convert 2D to 1D
    ReDim Preserve tmpArray(1 To UBound(tmpArray, 1), 1)
    
    Sheets("Category").Range("A2").Resize(UBound(tmpArray, 1)) = tmpArray

End Sub
 
Upvote 0
If it's just unique products that you are after:
Code:
Sub DedupeProducts()
    Dim LastRow As Long
      
    LastRow = Sheets("Matrix Codes").Range("A2").End(xlDown).Row
    
    With Sheets("Matrix Codes").Range("A2:A" & LastRow)
        Call .AdvancedFilter(Action:=xlFilterCopy, CopyToRange:=Sheets("Category").Range("A2"), Unique:=True)
    End With
End Sub
 
Upvote 0
Your code works great. There appears to be an issue with my attempt to initially read in a 2D range. Only the first column of the worksheet is read into the array.

Code:
LastRow = Sheets("Matrix Codes").Range("A2").End(xlDown).Row
[B]LastCol = Sheets("Matrix Codes").Range("A2").End(xlToRight).Column
[/B]catLastCell = Sheets("Category").Range("A2").End(xlDown).Row
 
With Sheets("Matrix Codes")
  tmpArray = .Range(.Cells(2, 1), .Cells(LastRow, [B]LastCol[/B])).Value
End With


In term of the number of rows. There should never be more than 30 or so.

thanks a great deal for your help

mark
 
Upvote 0
The problem with autofilter is the fact that I will be working with a 2D range. At least that's the theory.

:)
mark
 
Upvote 0
Sektors code also demonstrates another method. One can only resize the last dimension of a multi-diemnsional array, and this can be used to your advantage.

However, if you are just needing a list of unique product codes then see my last advanced filter suggestion. :)
 
Upvote 0
The problem with autofilter is the fact that I will be working with a 2D range. At least that's the theory.

:)
mark

I'm losing the plot a bit, sorry... Why do you need to handle a 2d range if you are after a list of unique product codes?
 
Upvote 0
you are quite right. For no good reason I thought your autofilter suggestion applied to the Matrix Codes sheet, which of course makes no sense. Applying an autofilter to the destination column at the end of the procedure is a perfect solution.

I'm struggling work out why my code is only picking up the first column from the Matrix Codes sheet.

thanks again. sorry it's been a bit of a messy thread.

mark
 
Upvote 0
There's a method of Range called RemoveDuplicates. Just dump array to sheet and call this method on dumped cells.
 
Upvote 0
thanks Sektor, I'll give it a go.

Looping through my 1D array with MsgBox demonstrates that values from both column A and B have been included in the array. When this array is dumped into the first column of the category sheet, only the values from column A of the source worksheet are copied. This doesn't seem to be the expected behaviour of a 1D array.

anyway. Thanks again.

mark
 
Upvote 0

Forum statistics

Threads
1,216,000
Messages
6,128,202
Members
449,433
Latest member
mwegter95

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top