简体   繁体   English

VBA使用ComboBox命名工作表

[英]VBA Name a Sheet with ComboBox

I have a commandButton which opens a UserForm to add a new Worksheet. 我有一个commandButton,它将打开一个UserForm以添加一个新的工作表。 In this Userform is a ComboBox, where i can choose the machine type. 在此用户窗体中是一个组合框,我可以在其中选择机器类型。

Now i want to create a new Worksheet with the Name of the machine type which was selected in the ComboBox. 现在,我想用在ComboBox中选择的机器类型的名称创建一个新的工作表。

This is my Code: 这是我的代码:

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Sheets("Sheet1").Copy Before:=Sheets(10)
    ws.Name = ComboBox1
    [UserForm1].Hide
End Sub

Private Sub UserForm_Initialize()
    ComboBox1.List = Array("Machine Type 1", "Machine Type 2")
End Sub

Is there a way to create a new sheet, which is a copy from Sheet1 and name it like the machine type from the ComboBox1? 有没有办法创建一个新工作表,它是Sheet1的副本,并像ComboBox1中的机器类型一样命名?

Thanks for your help. 谢谢你的帮助。

Try the code below, see notes in the code's comments: 尝试下面的代码,请参见代码注释中的注释:

Private Sub CommandButton1_Click()

    Dim ws As Worksheet

    Sheets("Sheet1").Copy Before:=Sheets(10)
    Set ws = ActiveSheet ' <-- you need to set the worksheet object to the latest copied sheet
    ws.Name = ComboBox1.Value '<-- now you can modify the name using the worksheet object
    Me.Hide '<-- hide the user-form        
End Sub

use 采用

Private Sub CommandButton1_Click()
    Sheets("Sheet1").Copy Before:=Sheets(Sheets.Count)
    ActiveSheet.Name = ComboBox1.Value
    Me.Hide
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM