In a userform, Sheet name selected in listbox should show in the textbox

时间:2017-04-06 16:44:22

标签: excel excel-vba listbox vba

I have a userform which has one listbox which displays the names of worksheets in the activeworkbook. When I double click on any of the sheet name listed in the listbox it takes me to that sheet. In the same userform I also have textbox in which whatever I will type in it will change the active sheet name to that. All the above queries codes are working. Now I want another feature that whatever sheet name I have selected from listbox that sheet name should reflect in my textbox as well. Please let me know what code should I use. Please find below the codes which I have used so far to get the list of sheets in my listbox and to change the sheet name by typing the name in the textbox.

Private Sub CommandButton1_Click()

'unload the userform

Unload Me

End Sub

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

'declare the variables

Application.ScreenUpdating = False

Dim i As Integer, Sht As String

'for loop

For i = 0 To ListBox1.ListCount - 1

    'get the name of the selected sheet

    If ListBox1.Selected(i) = True Then

        Sht = ListBox1.List(i)

    End If

Next i

'test if sheet is already open

If ActiveSheet.Name = Sht Then

    MsgBox "This sheet is already open!"

    Exit Sub

End If

'select the sheet

Sheets(Sht).Select

'reset the userform

Unload Me

frmNavigation.Show

End Sub



Private Sub Sheetnametext_Change()

'If the length of the entry is greater than 31 characters, disallow the entry.

If Len(Sheetnametext) > 31 Then

    MsgBox "Worksheet tab names cannot be greater than 31 characters in length." & vbCrLf & "You entered " & mysheetname & ", which has " & Len(mysheetname) & " "
    characters.", , "Keep it under 31 characters"

    Exit Sub

End If

'Sheet tab names cannot contain the characters /, \, [, ], *, ?, or :.

'Verify that none of these characters are present in the cell's entry.

Dim IllegalCharacter(1 To 7) As String, i As Integer

IllegalCharacter(1) = "/"

IllegalCharacter(2) = "\"

IllegalCharacter(3) = "["

IllegalCharacter(4) = "]"

IllegalCharacter(5) = "*"

IllegalCharacter(6) = "?"

IllegalCharacter(7) = ":"


For i = 1 To 7

    If InStr(Sheetnametext, (IllegalCharacter(i))) > 0 Then

        MsgBox "You used a character that violates sheet naming rules." & vbCrLf & vbCrLf & "Please re-enter a sheet name without the ''" & IllegalCharacter(i) & "'' character.", 48, "Not a possible sheet name !!"

        Exit Sub

    End If

Next i

'Verify that the proposed sheet name does not already exist in the workbook.

Dim strSheetName As String, wks As Worksheet, bln As Boolean

strSheetName = Trim(Sheetnametext)

On Error Resume Next

Set wks = ActiveWorkbook.Worksheets(strSheetName)

On Error Resume Next

If Not wks Is Nothing Then

    bln = True

Else

    bln = False

    Err.Clear

End If

'History is a reserved word, so a sheet cannot be named History.

If UCase(mysheetname) = "HISTORY" Then

    MsgBox "A sheet cannot be named History, which is a reserved word.", 48, "Not allowed"

    Exit Sub

End If

'If the worksheet name does not already exist, name the active sheet as the InputBox entry.

'Otherwise, advise the user that duplicate sheet names are not allowed.

If bln = False Then

    ActiveSheet.Name = strSheetName

End If

End Sub
Private Sub UserForm_Initialize()

Dim Sh      As Variant

'for each loop the add visible sheets

For Each Sh In ActiveWorkbook.Sheets

    'add sheets to the listbox

    Me.ListBox1.AddItem Sh.Name

Next Sh

End Sub

1 个答案:

答案 0 :(得分:1)

需要Listbox_Change事件。这样的事情对你有用:

Private Sub ListBox1_Change()

    Dim i As Long

    For i = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(i) Then
            Me.Sheetnametext.Text = Me.ListBox1.List(i)
            Exit For
        End If
    Next i

End Sub