Declare your variables Dim x#, y#
correctly. That will fix your Mismatch error, but will present you with another error, since y(j - 1)
expects an array. Add the multiplication sign so that it is y * (j-1)
and you will avoid that error, but you may get an overflow in the event that Selection.Rows.Count > 2
, so you might also want to add a check for that.
Sub multRowsTo1Row()
Dim inputRange As Variant
Dim outputRange As Variant
Dim y#, x#
If selection.Rows.Count > 2 Then
MsgBox "Invalid Selection!", vbCritical
Exit Sub
End If
inputRange = Selection
y = UBound(inputRange, 1)
x = UBound(inputRange, 2)
ReDim outputRange(1 To x * y)
For j = 1 To y
For i = 1 To x
outputRange(i + y * (j - 1)) = inputRange(j, i)
Next i
Next j
Selection.Offset(0, x).Select
End Sub
As always, much pain & troubleshooting can be avoided with use of Option Explicit
and also dimensioning your variables to a specific Type
:)