2017-04-06 46 views
0

在我的工作簿中有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 
+0

如果我尝试Sheetnametext.Text = ActiveSheet.Name然后它显示当前工作表名称,但它不允许我在文本框中写任何东西。 – astha

回答

0

Astha更好地使用一个列表框浏览表在工作簿,,尝试这段代码。

私人小组UserForm_Initialize()

昏暗N作为龙 对于n = 1至ActiveWorkbook.Sheets.Count ListBox1.AddItem ActiveWorkbook.Sheets(N),请将.Name 下一个N

结束子

+0

所以shud我也改变我以前的代码文本框(Sheetnametext)到ListBox1的 – astha

+0

如果你用我的代码将足以得到解决。 –

+0

你要我创建另一个列表框,因为这是一个文本框让你的代码是不工作 – astha

相关问题