› Sviluppare funzionalita su Microsoft Office con VBA › Valori Array in una cella
-
AutoreArticoli
-
Ciao a tutti,
Mi trovo in difficoltà con un array in vba, sto cercando di mettere in una sola cella di excel tutti i valori dell'array, separati da un punto e virgola.
Ho provato a scrivere il codice con la poca conoscenza che ho del VBA ma non funziona e mi printa solo il primo valore dell'array
Dim sez() As Variant Dim i As Integer sez = Array("Valore1", "Valore2", "Valore3") For i = 0 To UBound(sez) Debug.Print i, sez(i) Next i .Range("D1").Value = Split(sez, ";")
Grazie a chi mi aiuta
Prova cosi
Sub h() Dim sez() As Variant Dim i As Integer Dim totale as string sez = Array("Valore1", "Valore2", "Valore3") For i = 0 To UBound(sez) Debug.Print i, sez(i) totale = sez(i) & ";" & totale Next i Range("D1").Value = totale End Sub
Qual è il punto di avere gusti diversi, se non mostrare che i cervelli lavorano diversamente, che pensiamo diversamente? ( Alan Turing)
Sempre il mare, uomo libero, amerai!
( Charles Baudelaire )Grazie mille tutte e due i metodi funzionano.
A questo punto però ho una serie di ; nelle variabili che non sono valorizzate.
Vi allego l'esempio
Dim censimp As String Dim sezione As String Dim sapr As String Dim up As String Dim dee As String Dim tensione As String Dim potenza As String Dim istat As String Dim cod() As Variant With ws1 If .Range("A2") = "" Then riga = 2 Else riga = .Range("A" & Rows.Count).End(xlUp).Row + 1 End If If ToggleButton6.Value = True Then censimp = "censimp" Else censimp = "" End If If ToggleButton7.Value = True Then sezione = "sezione" Else sezione = "" End If If ToggleButton8.Value = True Then sapr = "sapr" Else sapr = "" End If If ToggleButton9.Value = True Then dee = "Data Esercizio" Else dee = "" End If If ToggleButton10.Value = True Then tensione = "tensione" Else tensione = "" End If If ToggleButton11.Value = True Then potenza = "potenza" Else potenza = "" End If If ToggleButton12.Value = True Then istat = "istat" Else istat = "" End If If ToggleButton13.Value = True Then up = "up" Else up = "" End If cod = Array(censimp, sezione, sapr, up, dee, tensione, potenza, istat) .Range("D" & riga).Value = Join(cod, ";") End With Unload Me Set ws1 = Nothing
Se per esempio seleziono solo due dei bottoni il risultato sara tipo questo:
;sezione;;;;;potenza;
C'è un modo per pulire i punti e virgola di troppo?
prova questo esempio
Sub trim3() s = ";sezione;;;;;potenza;" For i = 1 To Len(s) c = Mid(s, i, 1) If c = Mid(s, i + 1, 1) And c = ";" Then Else output = output & c End If Next MsgBox output End Sub
oppure questo
Sub trim2() s = ";sezione;;;;;potenza;" For i = 1 To 3 s = Replace(s, ";;", ";") Next MsgBox s End Sub
prova cosi, ridimensioni la tua array dinamicamente, inserendo i vari valore in coda a quelli che sono nella tua matrice
`Option Explicit Sub matrici() Dim censimp As String Dim sezione As String Dim sapr As String Dim up As String Dim dee As String Dim tensione As String Dim potenza As String Dim istat As String Dim cod() As Variant Dim riga As Integer, X As Integer Dim ws1 As Worksheet With ws1 '<====== non è inizializzato If .Range("A2") = "" Then riga = 2 Else riga = .Range("A" & Rows.Count).End(xlUp).Row + 1 End If If ToggleButton6.Value = True Then ReDim Preserve cod(X) cod(X) = "censimp" X = X + 1 End If If ToggleButton7.Value = True Then ReDim Preserve cod(X) cod(X) = "sezione" X = X + 1 End If If ToggleButton8.Value = True Then ReDim Preserve cod(X) cod(X) = "sapr" X = X + 1 End If If ToggleButton9.Value = True Then ReDim Preserve cod(X) cod(X) = "Data Esercizio" X = X + 1 End If If ToggleButton10.Value = True Then ReDim Preserve cod(X) cod(X) = "tensione" X = X + 1 End If If ToggleButton11.Value = True Then ReDim Preserve cod(X) cod(X) = "potenza" X = X + 1 End If If ToggleButton12.Value = True Then ReDim Preserve cod(X) cod(X) = "istat" X = X + 1 End If If ToggleButton13.Value = True Then ReDim Preserve cod(X) cod(X) = "up" X = X + 1 End If .Range("D" & riga).Value = Join(cod, ";") End With Unload Me Set ws1 = Nothing End Sub `
Qual è il punto di avere gusti diversi, se non mostrare che i cervelli lavorano diversamente, che pensiamo diversamente? ( Alan Turing)
Sempre il mare, uomo libero, amerai!
( Charles Baudelaire ) -
AutoreArticoli