XL 2016 VBA; InputBox

Aiko

XLDnaute Nouveau
Bonsoir,

Comment changer la macro (vba.xlsm!Módulo1.AFH2) pour qu'elle demande à l'utilisateur combien de fois il désire que le mot "font" s'affiche, et faire afficher en fonction de sa demande?
Comment utiliser ici l'InputBox?

Merci par avance
 

Pièces jointes

  • vba.xlsm
    14 KB · Affichages: 8

JBARBE

XLDnaute Barbatruc
Bonjour à tous,
Peut-être ceci : ( important : il faut supprimer la macro Sub AFH2() )
Bonne soirée
VB:
Sub test()
Dim i As Long, Message As String
Range("B2:D2")=""
For i = 2 To 4
Message = InputBox("Combien voulez-vous de mot FONT")
If Message = 1 Then
Cells(2, 2) = 1
ElseIf Message = 2 Then
Cells(2, 2) = 1
Cells(2, 3) = 1
ElseIf Message = 3 Then
Cells(2, 2) = 1
Cells(2, 3) = 1
Cells(2, 4) = 1
End If
Exit For
Next i
End Sub
 

patricktoulon

XLDnaute Barbatruc
bonsoir
VB:
Sub AFH2_()
    x = InputBox("entrez le nombre de fois", "entrez un nombre", 0)
    If x > 0 Then
    Cells(1, 1) = "Ainsi"
    For i = 2 To 2 + x
        Cells(1, i) = "font"
    Next i
    Cells(1, 3 + x) = "HEY"
      Application.ActiveSheet.Columns.AutoFit
End If
End Sub
et evite de donner une adresse de ceelule comme non de macro tu t'en portera pas plus mal

on aurait plus l'ecrire eventuellement comme ça sans boucle
VB:
Sub AFH2_()
    Dim x, arr
    x = InputBox("entrez le nombre de fois", "entrez un nombre", 0)
    If x > 0 Then
        arr = Split("Ainsi" & Application.Rept(" font", x) & " HEY", " ")
        Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
        Application.ActiveSheet.Columns.AutoFit
    End If
End Sub
 
Dernière édition:

JBARBE

XLDnaute Barbatruc
Re,
En obligeant à mettre un nombre de 1 à 3 :
Code:
Option Explicit



Sub test()
Dim i As Long, Message
Range("B2:D2") = ""
For i = 2 To 4
Message = InputBox("Combien voulez-vous de mot FONT")
If Message = 1 Then
Cells(2, 2) = 1
If IsNumeric(Cells(i, 2)) = False Then
Exit Sub
End If
ElseIf Message = 2 Then
Cells(2, 2) = 1
Cells(2, 3) = 1
If IsNumeric(Cells(i, 2)) = False  Then
Exit Sub
End If
ElseIf Message = 3 Then
Cells(2, 2) = 1
Cells(2, 3) = 1
Cells(2, 4) = 1
If IsNumeric(Cells(i, 2)) = False Then
Exit Sub
End If
Else
MsgBox "Veuillez mettre un nombre entre 1 et 3"
End If
Exit For
Next i
End Sub
 
Dernière édition:

patricktoulon

XLDnaute Barbatruc
autant pour moi c'est x-1
VB:
Sub AFH2_()
    x = InputBox("entrez le nombre de fois", "entrez un nombre", 0)
    If x > 0 Then
    Cells(1, 1) = "Ainsi"
    For i = 2 To 2 + x - 1
        Cells(1, i) = "font"
    Next i
    Cells(1, 3 + x) = "HEY"
      Application.ActiveSheet.Columns.AutoFit
End If
End Sub
End Sub
 

Aiko

XLDnaute Nouveau
autant pour moi c'est x-1
VB:
Sub AFH2_()
    x = InputBox("entrez le nombre de fois", "entrez un nombre", 0)
    If x > 0 Then
    Cells(1, 1) = "Ainsi"
    For i = 2 To 2 + x - 1
        Cells(1, i) = "font"
    Next i
    Cells(1, 3 + x) = "HEY"
      Application.ActiveSheet.Columns.AutoFit
End If
End Sub
End Sub

Oui, j'avais vu après :)
et au lieu de Cells(1, 3 + x) = "HEY" j'ai mis Cells(1, 3 + x-1) = "HEY" aussi.
Merci beaucoup et bonne soirée à tous
 

Discussions similaires

Statistiques des forums

Discussions
312 103
Messages
2 085 317
Membres
102 862
dernier inscrit
Emma35400