在用户窗体中,在列表框中select的图纸名称应显示在文本框中

我有一个用户窗体,其中有一个列表框显示活动工作簿中工作表的名称。 当我双击列表框中列出的任何一个表名称,我把它带到那张表。 在同一个用户窗体中,我也有文本框,在其中input的任何内容都会将活动工作表名称更改为。 所有上述查询代码正在工作。 现在我想要另一个function,无论我从列表框中select的任何表名,该表的名称也应该反映在我的文本框中。 请让我知道我应该使用什么代码。 请在下面find我迄今使用的代码,以获取列表框中的图纸列表,并通过在文本框中input名称来更改图纸名称。

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 

需要一个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