Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao By EF,
Ciao GianMarco Zen,
grazie per la risposta che, però, mi fà sorgere il dubbio di non aver spiegato bene l'aiuto che necessito. Quindi, provo a semplificare il tutto:
la richiesta di aiuto, verte su come modificare la macro VBA per copiare un range dove attualmente copio una riga. Quindi, confido nell'immensa preparazione e disponibilità di Norman David Jones (che saluto) per arrivare a [RISOLTO].
Prova a sostituire il tuo codice con la seguente versione:
'=========>>
Option Explicit
'--------->>
Public Sub EstraiCelleDaElenco()
Dim WB As Workbook
Dim SH As Worksheet, SH_Test As Worksheet
Dim Rng As Range, destRng As Range
Dim oDic As Object
Dim arrSplit As Variant, arrTemp As Variant, arrTemp2() As Variant
Dim arrDomande() As Variant, arrOut() As Variant
Dim myCollection As Collection
Dim i As Long, j As Long, k As Long
Dim ii As Long, jj As Long, kk As Long
Dim iCtr As Long, UB2 As Long
Dim LRow As Long
Dim DA_ESTRARRE As Long
Const sFogliDomande As String = "STORIA,ITALIANO,Matematica"
Const sFoglioTest As String = "TEST"
Set WB = ThisWorkbook
With WB
Set SH_Test = .Sheets(sFoglioTest)
arrSplit = Split(sFogliDomande, ",")
If UBound(arrSplit) = 0 Then
Set SH = .Sheets(sFogliDomande)
With SH
LRow = LastRow(SH, .Columns("A:A"))
Set Rng = .Range("A2:E" & LRow)
End With
arrDomande = Rng.Value
Else
For i = LBound(arrSplit) To UBound(arrSplit)
Set SH = .Sheets(arrSplit(i))
With SH
LRow = LastRow(SH, .Columns("A:A"))
Set Rng = .Range("A2:E" & LRow)
If i = 0 Then
arrDomande = Application.Transpose(Rng.Value)
iCtr = UBound(arrDomande, 2)
UB2 = UBound(arrDomande)
Else
LRow = LastRow(SH, .Columns("A:A"))
Set Rng = .Range("A2:E" & LRow)
arrTemp = Rng.Value
ReDim Preserve arrDomande(1 To UB2, 1 To iCtr + UBound(arrTemp))
For j = 1 To UBound(arrTemp)
iCtr = iCtr + 1
For k = 1 To UB2
arrDomande(k, iCtr) = arrTemp(j, k)
Next k
Next j
End If
End With
Next i
End If
End With
iMax = UBound(arrDomande, 2)
With SH_Test
DA_ESTRARRE = .Range("K1").Value
.UsedRange.Offset(1).ClearContents
Set destRng = .Range("A2")
End With
ReDim arrTemp(1 To iCtr)
For j = 1 To iCtr
arrTemp(j) = j
Next j
arrTemp2 = ShuffleArray(arrTemp)
ReDim arrOut(1 To DA_ESTRARRE, 1 To UB2)
For jj = 1 To DA_ESTRARRE
For kk = 1 To UB2
arrOut(jj, kk) = arrDomande(kk, arrTemp2(jj))
Next kk
Next jj
On Error GoTo XIT
Application.ScreenUpdating = False
destRng.Resize(DA_ESTRARRE, UB2).Value = arrOut
Call MsgBox( _
Prompt:="Il Test ò pronto e comprende " _
& UBound(arrOut) & " domande!", _
Buttons:=vbInformation, _
Title:="REPORT")
XIT:
Application.ScreenUpdating = True
Set myCollection = Nothing
End Sub
'--------->>
Public Function ShuffleArray(InArray)
Dim Arr() As Variant
Dim vTemp As Variant
Dim i As Long, j As Long, k As Long
Randomize
i = UBound(InArray) - LBound(InArray) + 1
ReDim Arr(LBound(InArray) To UBound(InArray))
For j = LBound(InArray) To UBound(InArray)
Arr(j) = InArray(j)
Next j
For j = LBound(InArray) To UBound(InArray)
k = CLng(((UBound(InArray) - j) * Rnd) + j)
vTemp = Arr(j)
Arr(j) = Arr(k)
Arr(k) = vTemp
Next j
ShuffleArray = Arr
End Function
'--------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range, _
Optional minRow As Long = 1, _
Optional sPassword As String)
Dim bProtected As Boolean
With SH
If Rng Is Nothing Then
Set Rng = .Cells
End If
bProtected = .ProtectContents = True
If bProtected Then
Application.ScreenUpdating = False
.Unprotect Password:=sPassword
End If
End With
On Error Resume Next
LastRow = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
If LastRow < minRow Then
LastRow = minRow
End If
If bProtected Then
SH.Protect Password:=sPassword, _
UserInterfaceOnly:=True
End If
Application.ScreenUpdating = True
End Function
'<<=========
Potresti scaricare il mio file di prova ByEF20180122.xlsm
===
Regards,
Norman