VBA: Create Array Using Selection

zero269

Active Member
Joined
Jan 16, 2023
Messages
302
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm having some trouble figuring out how to create an array using values from a selection of cells.

One thing to note, is that I'm not storing the values for the selected cells themselves. Depending on who's selecting the cells, it could be any column, so I'm telling Excel to store the values in column A instead, based on the row number for the selected cell... using…
VBA Code:
myArray(i) = Range("A" & cell.Row)
I'm getting a Type Mismatch error for the Array portion, thus preventing me from even storing a single value at this point.
I tried storing the Quiz number into a variable (v2) thinking Long values were the issue, but that resulted in the same error.
VBA Code:
'Create Array from Selection
Sub WIP_Selection_Array()

  'Select visible cells only if selection is > 1
  If Selection.Cells.Count > 1 Then
    Selection.SpecialCells(xlCellTypeVisible).Select
  Else
  End If
  
  'Declarations
  Dim myArray As Variant
  Dim cell As Range
  Dim i As Long: i = 1
  Dim Quiz As Long
  
  'LOOP Selection
  For Each cell In Selection
  
    'v1
    myArray(i) = Range("A" & cell.Row)

    'v2
'    Quiz = Range("A" & cell.Row)
'    myArray(i) = Quiz

    i = i + 1 'index counter
  
  Next cell

End Sub
For example, If I select the four cells in column Q that show "on hold", then my array would be populated with the four associated Quiz numbers in column A.
VBA Testing.xlsm
ABDIJKLOPQ
1QuizTitleAuthorF/NFAR PointsBook LevelWord CountDue DatePickup DateBook Status
214383620,000 Leagues Under the SeaHutchinson, EmilyF2.04.212,22530-Sepon hold
3154A Bear Called PaddingtonBond, MichaelF4.04.724,560
4518852A Christmas CarolHutchison, PatriciaF1.02.86,14730-Sepon hold
541535A Christmas CarolHutchinson, EmilyF2.04.112,16230-Sepon hold
641536A Tale of Two CitiesLorimer, JanetF2.03.812,15230-Sepon hold
Books

Any help would be greatly appreciated…
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Code:
Sub Try_So_Maybe()
Dim ws As Worksheet
Dim colArr
Dim c As Range
Set ws = ThisWorkbook.Worksheets("Sheet1")    '<---- Change required
    For Each c In Selection
        colArr = colArr & "|" & ws.Cells(c.Row, 1).Value
    Next c
colArr = Split(Mid(colArr, 2), "|")
ws.Cells(ws.Rows.Count, 20).End(xlUp).Offset(1).Resize(UBound(colArr) + 1).Value = Application.Transpose(colArr)    '<---- Change required
End Sub
 
Upvote 0
For example, If I select the four cells in column Q that show "on hold", then my array would be populated with the four associated Quiz numbers in column A.
Try this then

VBA Code:
Sub Fill_Array()
  Dim myArray As Variant
  
  myArray = Evaluate("let(v,textsplit(textjoin(""|"",," & Intersect(Selection.EntireRow, Columns("A")).Address & "),""|""),iferror(--v,v))")
End Sub

Here are my results for the sample data and selection scenario you gave.

1737549976309.png
 
Upvote 0

Forum statistics

Threads
1,225,741
Messages
6,186,761
Members
453,370
Latest member
juliewar

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