VBA - Allows the user to select the sheet where to paste values

Danoob

New Member
Joined
May 31, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm trying to put a combobox or something in a userform to allow the user to select the sheet where he wants to paste the values he put in the form. As of right now, I modified the sheet manually which isn't ideal.
The closest thing I saw to what I'm looking for is in this thread : VBA - Allow User to Select Which Worksheet to Copy but it's for copying and I don't know how to adapt it to the code below.
The following code is only execute once the user filled the forms with his data and click the "Add" button. Also to precise, all sheets have the same presentation.

Thanks to anyone that can help me or provide ideas.

VBA Code:
 Private Sub BtnAdd_Click()

    Sheets("DATA 20220516").Activate 'Sheet where the data is added
    Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    'Positionnement sur la dernière ligne
    'Décalage d'une ligne vers le bas
    ActiveCell = Txtid.Value
    ActiveCell.Offset(0, 1).Value = cmbsoc
    ActiveCell.Offset(0, 2).Value = TxtCV
    ActiveCell.Offset(0, 3).Value = Cmbdiv
    ActiveCell.Offset(0, 4).Value = CmbVend
    ActiveCell.Offset(0, 5).Value = Txtref
    ActiveCell.Offset(0, 6).Value = Txtpur
    ActiveCell.Offset(0, 8).Value = Txtam
    ActiveCell.Offset(0, 9).Value = txtdtd
    ActiveCell.Offset(0, 10).Value = txtPdate
    ActiveCell.Offset(0, 16).Value = CmbPO
    ActiveCell.Offset(0, 18).Value = Txtcomm
    ActiveCell.Offset(0, 19).Value = TxtCons
    ActiveCell.Offset(0, 20).Value = Txtact
    ActiveCell.Offset(0, 21).Value = TxtRes
    ActiveCell.Offset(0, 22).Value = Txtissu
    
    MsgBox "Nouvelle entrée prise en compte", vbOKOnly + vbInformation, "CONFIRMATION"
    
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi,
untested but try this update to your code & see if does what you want

Place combobox on your userforform & name it cbSheetNames

VBA Code:
 Private Sub BtnAdd_Click()
    Dim ws  As Worksheet
    Dim rng As Range
    
    Set ws = ThisWorkbook.Worksheets(Me.cbSheetNames.Value)
    Set rng = ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1)
    
    'Positionnement sur la dernière ligne
    'Décalage d'une ligne vers le bas
    rng = Txtid.Value
    rng.Offset(0, 1).Value = cmbsoc
    rng.Offset(0, 2).Value = TxtCV
    rng.Offset(0, 3).Value = Cmbdiv
    rng.Offset(0, 4).Value = CmbVend
    rng.Offset(0, 5).Value = Txtref
    rng.Offset(0, 6).Value = Txtpur
    rng.Offset(0, 8).Value = Txtam
    rng.Offset(0, 9).Value = txtdtd
    rng.Offset(0, 10).Value = txtPdate
    rng.Offset(0, 16).Value = CmbPO
    rng.Offset(0, 18).Value = Txtcomm
    rng.Offset(0, 19).Value = TxtCons
    rng.Offset(0, 20).Value = Txtact
    rng.Offset(0, 21).Value = TxtRes
    rng.Offset(0, 22).Value = Txtissu
    
    MsgBox "Nouvelle entrée prise en compte", vbOKOnly + vbInformation, "CONFIRMATION"
    
End Sub

Private Sub UserForm_Initialize()
    Dim ws As Worksheet
    With Me.cbSheetNames
        For Each ws In ThisWorkbook.Worksheets
            .AddItem ws.Name
        Next ws
        .Style = fmStyleDropDownList
        .ListIndex = 0
    End With
End Sub

Dave
 
Upvote 0
Solution
Hi,
untested but try this update to your code & see if does what you want

Place combobox on your userforform & name it cbSheetNames

VBA Code:
 Private Sub BtnAdd_Click()
    Dim ws  As Worksheet
    Dim rng As Range
   
    Set ws = ThisWorkbook.Worksheets(Me.cbSheetNames.Value)
    Set rng = ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1)
   
    'Positionnement sur la dernière ligne
    'Décalage d'une ligne vers le bas
    rng = Txtid.Value
    rng.Offset(0, 1).Value = cmbsoc
    rng.Offset(0, 2).Value = TxtCV
    rng.Offset(0, 3).Value = Cmbdiv
    rng.Offset(0, 4).Value = CmbVend
    rng.Offset(0, 5).Value = Txtref
    rng.Offset(0, 6).Value = Txtpur
    rng.Offset(0, 8).Value = Txtam
    rng.Offset(0, 9).Value = txtdtd
    rng.Offset(0, 10).Value = txtPdate
    rng.Offset(0, 16).Value = CmbPO
    rng.Offset(0, 18).Value = Txtcomm
    rng.Offset(0, 19).Value = TxtCons
    rng.Offset(0, 20).Value = Txtact
    rng.Offset(0, 21).Value = TxtRes
    rng.Offset(0, 22).Value = Txtissu
   
    MsgBox "Nouvelle entrée prise en compte", vbOKOnly + vbInformation, "CONFIRMATION"
   
End Sub

Private Sub UserForm_Initialize()
    Dim ws As Worksheet
    With Me.cbSheetNames
        For Each ws In ThisWorkbook.Worksheets
            .AddItem ws.Name
        Next ws
        .Style = fmStyleDropDownList
        .ListIndex = 0
    End With
End Sub

Dave
I did what you said and it works perfectly, thanks you a lot for your help Dave
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top