Hi Rodgy,
. Alan S already had you "Sort ed" as I was in the middle writing this....
.....so I finished it anyway as an alternative...
.. it is the typical "Array capture" type alternative method to Alan S#s "Spreadsheet" type method (Which i actually prefer as it is easier as a beginner to follow). But theoretically mine is faster for lots of data (I think?) - (It captures all data in one go, does all sorting in VBA as it were, then outputs end results in one go)
... here it is (and I modified it for your new requirements. ). (Try to get your requirements right first time as it is not always easy to modify and sometime means re- writing everything again (I was lucky here, and the modifications were easy)
Here then the code:
Sub ArrayCaptureMehthod_Spose()
Dim wks1 As Worksheet: Set wks1 = ThisWorkbook.Worksheets("Sheet1") 'Give abbreviations all methods and properties..
Dim wks2 As Worksheet: Set wks2 = ThisWorkbook.Worksheets("Sheet2") '.. of worrkshhets onbjrct
Dim irws As Long, iclms As Long, orws As Long: Let orws = 0 'variable counts for use in looping as Array indicies and Bound variable count in loops
'Dim InArray() As Variant: Let InArray() = wks1.Range("A1").CurrentRegion.Value 'Effectively "Captures the current region )Note perohery of range must be empty, or that will also be captured!! >> http://www.excelforum.com/excel-new-users-basics/1058406-range-dimensioning-range-and-value-referencing-and-referring-to-arrays.html
Dim InArray() As Variant: Let InArray() = wks1.UsedRange.Value 'Used range will capture evarything on sheet1 SO DO NOT have anything else on sheet or it will be captured.
Dim OutArray() As Variant: ReDim OutArray(1 To (UBound(InArray, 1) * UBound(InArray, 2)), 1 To 1) 'Outpu Array can now be given dimensions of maximum size required, that is ( RowsMaximum * ColumnsMaximum ) of Input "Capture" Array
For irws = 1 To UBound(InArray, 1) Step 1 ' go along each row in Input Array, (equivalent to each row in sheet 1.. and
For iclms = 1 To UBound(InArray, 2) Step 1 '... for each row go through each column in Array, ((equivalent to each cloumn in sheet 1)
'If InArray(irws, iclms) <> "" Then ' If data is there in next cell in Range, that is to say in captured Array of that Range cell values
Let orws = orws + 1 'Go to next row in Array (next free place in Array)
Let OutArray(orws, 1) = InArray(irws, iclms)
'Else 'Do nothing if empty cells
'End If
Next iclms
Next irws
wks2.Range("C1").Resize(UBound(OutArray, 1), 1) = OutArray 'Neast way to paste output in one go - resize C1 in second sheet to dimension of Output Array and make it equal to outpit Array
End Sub 'ArrayCaptureMehthod_Spose()
……. And just in case you want empty cells to be ignored, a second version
Sub ArrayCaptureMehthod_Spose2()
Dim wks1 As Worksheet: Set wks1 = ThisWorkbook.Worksheets("Sheet1") 'Give abbreviations all methods and properties..
Dim wks2 As Worksheet: Set wks2 = ThisWorkbook.Worksheets("Sheet2") '.. of worrkshhets onbjrct
Dim irws As Long, iclms As Long, orws As Long: Let orws = 0 'variable counts for use in looping as Array indicies and Bound variable count in loops
'Dim InArray() As Variant: Let InArray() = wks1.Range("A1").CurrentRegion.Value 'Effectively "Captures the current region )Note perohery of range must be empty, or that will also be captured!! >> http://www.excelforum.com/excel-new-users-basics/1058406-range-dimensioning-range-and-value-referencing-and-referring-to-arrays.html
Dim InArray() As Variant: Let InArray() = wks1.UsedRange.Value 'Used range will capture evarything on sheet1 SO DO NOT have anything else on sheet or it will be captured.
Dim OutArray() As Variant: ReDim OutArray(1 To (UBound(InArray, 1) * UBound(InArray, 2)), 1 To 1) 'Outpu Array can now be given dimensions of maximum size required, that is ( RowsMaximum * ColumnsMaximum ) of Input "Capture" Array
For irws = 1 To UBound(InArray, 1) Step 1 ' go along each row in Input Array, (equivalent to each row in sheet 1.. and
For iclms = 1 To UBound(InArray, 2) Step 1 '... for each row go through each column in Array, ((equivalent to each cloumn in sheet 1)
If InArray(irws, iclms) <> "" Then ' If data is there in next cell in Range, that is to say in captured Array of that Range cell values
Let orws = orws + 1 'Go to next row in Array (next free place in Array)
Let OutArray(orws, 1) = InArray(irws, iclms)
Else 'Do nothing if empty cells
End If
Next iclms
Next irws
wks2.Range("C1").Resize(UBound(OutArray, 1), 1) = OutArray 'Neast way to paste output in one go - resize C1 in second sheet to dimension of Output Array and make it equal to outpit Array
End Sub 'ArrayCaptureMehthod_Spose2()
…
Hope that helps.
Alan E
P.s. (By the way, I expect a formula could do this for you.. unfortunately I am not very good with those..)
Bookmarks