excel 微调按钮值和重命名工作表

gg0vcinb  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(171)

我想创建一个新的工作表,并更改数字调节钮中的值。如果ActiveSheet为2021 Aug,则新工作表应为2021 Sep.“AA 3”和“AG 3”与数字调节钮值关联。我想将新工作表的名称设为数字调节钮值或“AA 3”+“AG 3”值。
简而言之,
1.对于新工作表,微调按钮中的一个月增量
1.根据微调按钮值或文本框值或“AA 3”+“AG 3”值更改工作表名称最好显示为2021年8月、2021年9月等......

到目前为止,我已经编写了从Stackoverflow和各种网页的帮助。它创建了一个新的工作表,但不改变值,也工作表名称。

Sub CreateSheet()

    ActiveSheet.Select
    Sheets(Sheets.Count).Copy After:=Sheets(Sheets.Count)
    
    If ActiveSheet.SpinButton2.Value <= 10 Then 
        ActiveSheet.SpinButton2.Value = SpinButton2.Value + 1
    ElseIf ActiveSheet.SpinButton2.Value = 11 Then
        ActiveSheet.SpinButton1.Value = ActiveSheet.SpinButton1.Value + 1 And ActiveSheet.SpinButton2.Value = 0
    End If

    ActiveSheet.name = SpinButton2.Value & SpinButton1.Value
    
End Sub

Private Sub SpinButton1_Change()

    SpinButton1.Max = 2999
    SpinButton1.Min = 1900
    SpinButton1.SmallChange = 1
    TextBox1.Value = SpinButton1.Value
        
    Call MakeDate(SpinButton1.Value, SpinButton2.Value)

Private Sub SpinButton2_Change()
    Dim Mths As Variant

    SpinButton2.Max = 11
    SpinButton2.Min = 0
    Mths = Array("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec")
    TextBox2.Text = Mths(SpinButton2.Value)
        
    Call MakeDate(SpinButton1.Value, SpinButton2.Value)
        
End Sub

Private Sub MakeDate(sp1Val As String, sp2Val As String)
    Dim tempDate As Date

    tempDate = DateValue(sp1Val & "-" & sp2Val + 1 & "-" & 1)

    With Range("AE3")
        .NumberFormat = "yyyy"
        .Value = tempDate
    End With
     
    With Range("AA3")
         .NumberFormat = "mmm"
         .Value = tempDate
    End With

End Sub

更新1

从@vinod回答,我这样更改,它只创建一个工作表和正确的名称从2021年2月到2021年12月

NewName = ActiveSheet.TextBox2.Text & "-" & ActiveSheet.TextBox1.Value ActiveSheet.Name = NewName

现在的问题是,当复制Dec-2021表时,会创建重复的Dec-2021和spinbuttle。值未更改。

Sub CreateSheet()
    
    ' Display a messagebox based on the response

CarryOn = MsgBox("Do you want to create a new sheet? ", vbYesNo, "Continue?")
  
If CarryOn = vbYes Then
  
    ActiveSheet.Select
    Sheets(Sheets.Count).Copy After:=Sheets(Sheets.Count)
    
    
If ActiveSheet.SpinButton2.Value <= 10 Then ActiveSheet.SpinButton2.Value = ActiveSheet.SpinButton2.Value + 1
ElseIf ActiveSheet.SpinButton2.Value = 11 Then
    ActiveSheet.SpinButton2.Value = ActiveSheet.SpinButton2.Value - 11 And ActiveSheet.SpinButton1.Value = ActiveSheet.SpinButton1.Value + 1
End If

NewName = ActiveSheet.TextBox2.Text & "-" & ActiveSheet.TextBox1.Value
ActiveSheet.Name = NewName
       
End Sub
yh2wf1be

yh2wf1be1#

根据任务的逻辑流,您需要在要添加新工作表的代码中添加以下代码。

NewName = ActiveSheet.Range("AA3").Value & "-" & ActiveSheet.Range("AG3").Value
Sheets.Add.Name = NewName

您可以将此代码放置在任何需要的位置,例如单击按钮、单击微调按钮或选择的任何触发器。

ctehm74n

ctehm74n2#

最后,我设法解决了这个问题。

Sub CreateSheet()

   
    ' Display a messagebox based on the response

    CarryOn = MsgBox("Do you want to create a new sheet? ", vbYesNo, "Continue?")
  
    If CarryOn = vbYes Then
        Sheets(Sheets.Count).Copy After:=Sheets(Sheets.Count)
    End If
    
    
    If ActiveSheet.SpinButton2.Value <= 10 Then
        ActiveSheet.SpinButton2.Value = ActiveSheet.SpinButton2.Value + 1
    Else
        ActiveSheet.SpinButton2.Value = ActiveSheet.SpinButton2.Value - 11
        ActiveSheet.SpinButton1.Value = ActiveSheet.SpinButton1.Value + 1
    End If

    NewName = ActiveSheet.TextBox2.Text & "-" & ActiveSheet.TextBox1.Value
    ActiveSheet.Name = NewName   
       
End Sub

相关问题