Copy from a textbox based on a selection from listbox

Melani_6696

New Member
Joined
Jul 14, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

First time posting and a beginner on VBA but here it goes. I have created a list box based on the names of the tabs I have on an excel file but now I want to be able to add text to a tab if selected on the listbox, I havnt been able to make it work :/. Below what i tried... but i dont think is right? anybody can help out?


Private Sub CommandButton1_Click()
Dim i As Long, c As Long
Dim SheetArray() As String

With ActiveSheet.ListBoxSh
For i = 0 To .ListCount - 1
If .Selected(i) Then
ReDim Preserve SheetArray(c)
texbox2.Value = SheetArray(c).Range("B16").Value
SheetArray(c) = .List(i)
c = c + 1
End If
 

Attachments

  • VBA.JPG
    VBA.JPG
    77.6 KB · Views: 7

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi and welcome to MrExcel.

I am not understanding, do you want to change the name of the sheet?
Or do you want that for the selected elements of the listbox add the data that is in the textbox?

In a simulation on your sheet, you could put the desired result.
 
Upvote 0
I would like to add the textbox information to an specific cell of the sheet selected in the listbox.
 
Upvote 0
Try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim i As Long
  
  With ListBoxSh
    For i = 0 To .ListCount - 1
      If .Selected(i) Then
        Sheets(.List(i)).Range("B16").Value = texbox2.Value
      End If
    Next
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,239
Members
448,951
Latest member
jennlynn

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