Penso una semplice Userform soddisfi le tue esigenze.
Ciao Alberto,
Mi rendo conto che con una mia risposta del genere potrebbe provocare una risposta del tipo:
Senza offesa ma se avessi saputo già come si faceva :-))
Pertanto, come uno spinto prova come segue:
Sul foglio di interesse,****inserisci un CommandButton. Fai Clic dx sul pulsante e sostituisci il codice visualizzato con:
'=========>>
Option Explicit
'--------->>
Private Sub CommandButton1_Click()
UserForm1.Show vbModeless
End Sub
'<<=========
Alt-IU per inserire una Userform nel progetto.
Fai clic dx sulla Userform | Visualizza Codice per aprire il suo modulo di codice e incolla il seguente codice:
'==========>>
Option Explicit
Dim myButton As New Class1 '<<===== Eventualmente, modifica: vedi ^[1]^ **** di sotto
'--------->>
Private Sub UserForm_Initialize()
Dim TBox As Control
Dim MyLabel As Control
Dim aLabel As Control
Dim cButton As Control
Dim i As Long
Dim iTop As Long
Dim arrCaptions As Variant
Const sCaptions As String = "Ordine, Codice,Macchina"
arrCaptions = Split(sCaptions, ",")
With Me
.Height = 150
.Width = 150
.Caption = "Maschera Alberto"
End With
Set aLabel = Me.Controls.Add("Forms.Label.1", "titleLabel")
With aLabel
.Left = Me.Left + Me.Width / 3
.Top = Me.Top + 2
.Height = 15
.Width = 80
.Caption = "Inserisci"
.Font.Size = 15
End With
iTop = aLabel.Height
For i = 1 To 3
Set TBox = Me.Controls.Add("Forms.Textbox.1", "myTbox" & i)
iTop = iTop + 15
With TBox
.Left = 45
.Top = iTop
.Width = 80
.Height = 15
End With
Set MyLabel = Me.Controls.Add("Forms.Label.1", "myLabel" & i)
With MyLabel
.Left = 5
.Top = iTop + 5
.Height = 20
.Width = 35
.Caption = arrCaptions(i - 1)
End With
iTop = iTop + 10
Next i
Set cButton = Me.Controls.Add("Forms.CommandButton.1")
With cButton
.Height = 20
.Top = iTop + 10
.Width = 60
.Left = Me.Width - .Width - 10
.Caption = " Inserisci dati"
End With
Set myButton.myButtonEvents = cButton
End Sub
'==========>>
Alt-IC per inserire un modulo del tipo Class (nominato Class1); Nel caso il modulo avessi un nome diverso, modifica Class1, al punto
^[1]^ di sopra, al nome di questo modulo.
Nel modulo, incolla il seguente codice:
'==========>>
Option Explicit
Public WithEvents myButtonEvents As MSForms.CommandButton
'--------->>
Private Sub myButtonEvents_Click()
Call myButtonCode
End Sub
'<<==========
Alt-IM per inserire un nuovo modulo di codice
Nel nuovo modulo vuoto, incolla il seguente codice:
'==========>>
Option Explicit
'---------->>
Public Sub myButtonCode()
Dim SH As Worksheet
Dim Rng As Range
Dim iRow As Long
Dim Ctrl As MSForms.Control
Set SH = ActiveSheet
With SH
iRow = LastRow(SH, Range("A:A"))
Set Rng = .Range("A" & iRow + 1)
End With
With UserForm1.Controls
Rng.Cells(1, 1).Value = .Item("myTbox1").Value
.Item("myTbox1").Value = vbNullString
With .Item("myTbox2")
If IsDate(.Value) Then
Rng(1, 2).Value = CDate(.Value)
Else
Rng(1, 2).Value = .Value
End If
.Value = vbNullString
End With
Rng(1, 3).Value = .Item("myTbox3").Value
.Item("myTbox3").Value = vbNullString
End With
End Sub
'---------->>
Function LastRow(SH As Worksheet, _
Optional Rng As Range)
If Rng Is Nothing Then
Set Rng = SH.Cells
End If
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
End Function
'<<==========
Alt-Q per chiudere l'editor di VBA e tornare a Excel.
Ora, prova il pulsante.
Come è scritto il codice, i valori inseriti nelle tre caselle di testo della Userform form verranno inseriti nelle prime celle vuote nelle colonne A:C del foglio.
In questo esempio, la Userform, i suoi controlli e il codice evento sono creati a runtime (in fase di esecuzione). Più normalmente, la Userform , i controlli e il codice sarebbero progettati \ creati in anticipo, ma spero che questo almeno possa fornirti
alcune idee.
===
Regards,
Norman