【问题标题】:userform textbox to display active sheet name用户窗体文本框以显示活动工作表名称
【发布时间】:2017-04-06 15:21:09
【问题描述】:

在我的工作簿中有 50 个工作表,我创建了一个用户窗体,它会弹出一个文本框,现在可以帮助我更改工作表名称我希望当我切换不同的工作表时,当前工作表名称应显示在该文本框中,并且然后我将使用下面所述的代码修改工作表名称。

您能否告诉我一个代码行,文本框通过该代码行显示当前工作表名称。

请在下面找到我可以通过在文本框中输入名称来更改现有工作表名称的代码Sheetnametext

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

【问题讨论】:

  • 如果我正在尝试 Sheetnametext.Text = ActiveSheet.Name 那么它会显示当前工作表名称,但不允许我在文本框中写任何内容。

标签: excel vba


【解决方案1】:

Astha 最好使用列表框来导航工作簿中的表格,试试这个代码。

Private Sub UserForm_Initialize()

Dim N As Long 对于 N = 1 到 ActiveWorkbook.Sheets.Count ListBox1.AddItem ActiveWorkbook.Sheets(N).Name 下一个

结束子

【讨论】:

  • 所以我把以前的代码也从文本框(Sheetnametext)更改为 Listbox1
  • 如果你使用我的代码就足够了。
  • 您希望我创建另一个列表框,因为这是一个文本框,因此您的代码无法正常工作
  • 是的,创建一个列表框,因为它可以保存值列表。
  • 我不想要值列表,我基本上希望当我单击任何工作表时,工作表名称应在文本框中突出显示,然后我可以将其修改为我想要的任何名称
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 1970-01-01
  • 2019-06-02
  • 2022-06-14
相关资源
最近更新 更多