2017-04-06 13 views
0

私のワークブックには50のワークシートがあります。私はシートフォーム名を変更するのに役立つテキストボックスをポップアップ表示するuserformを作成しました。名前がそのテキストボックスに表示されるはずです。そして、私は以下に述べるコードでシート名を変更します。アクティブフォーム名を表示するユーザーフォームのテキストボックス

テキスト行に現在のワークシート名が表示されているコード行を教えてください。 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より良いあなたはワークブック内のシートを移動するためのリストボックスを使用し,,してみてください

私はテキストボックスに名前を入力することで、既存のワークシート名を変更できるの助けを借りて、コードの下に見つけてくださいこのコード。

プライベートサブActiveWorkbook.Sheets.Count ListBox1.AddItem ActiveWorkbook.Sheets(N)End Subの

.NAME次にN

にN = 1のための長い としてUserForm_Initialize()

薄暗いN

+0

だから私は前のコードをテキストボックス(Sheetnametext)からリストボックス1に変更します – astha

+0

私のコードを使用すれば解決策を得るのに十分です。 –

+0

これはテキストボックスなので別のリストボックスを作成してコードが機能しないようにしたい – astha

関連する問題