Ciao Federico,
avrei bisogno del vostro aiuto.
Nel mio foglio excel ho delle righe, ovvero:
ricavi dalla vendita 1
ricavi dalla vendita 2
ricavi dalla vendita 3
Avrei bisogno di un bottone che, ogni qualvolta venga pigiato, mi aggiunga una nuova riga sotto l'ultima con la scritta "ricavi dalla vendita 4", "ricavi dalla vendita 5" etc.
- Alt+F11 per aprire l'editor di VBA
- Alt+IM per inserire un nuovo modulo di codice
- Nel nuovo modulo vuoto, incolla il seguente codice:
'=========>>
Option Explicit
'--------->>
Public Sub Tester()
Dim SH As Worksheet
Dim Rng As Range
Dim LRow As Long
Dim vNumero_Progessivo As Variant
Const sPrefisso As String = "ricavi dalla vendita
"
Set SH = ActiveSheet
With SH
LRow = LastRow(SH, .Columns("A:A"))
Set rng = .Range("A" & LRow)
End With
With Rng
vNumero_Progessivo = DigitsOnly(.Value)
If vNumero_Progessivo = vbNullString Then
vNumero_Progessivo = 1
Else
vNumero_Progessivo = vNumero_Progessivo + 1
End If
.Offset(1).Value = sPrefisso & vNumero_Progessivo
End With
End Sub
'--------->>
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
'--------->>
Public Function DigitsOnly(sStr As String) As String
Dim oRegExp As Object
Set oRegExp = CreateObject("VBScript.RegExp")
With oRegExp
.IgnoreCase = True
.Global = True
oRegExp.Pattern = "\D"
DigitsOnly = .Replace(sStr, vbNullString)
End With
End Function
'<<=========
- Alt+Q per chiudere l'editor di VBA e tornare a Excel
- Salva il file con l’estensione xlsm
Potresti scaricare il mio file di prova Federico20190202.xlsm
===
Regards,
Norman
