如何使按钮创建一个名为像上一个工作日期加上一天的新工作表 [英] How to make button to create a new sheet named like the previous sheet date put plus one day

查看:198
本文介绍了如何使按钮创建一个名为像上一个工作日期加上一天的新工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在excel和这个项目中建立一个新的临床预约项目。我需要一个按钮来单击以创建一个带有新日期的新工作表,这个日期比上一个日期多一天。我在互联网上找到了一个代码,用我的模板创建了一个新的工作表,但它创建了今天的工作日期,但我需要的是创建工作表比最后一个工作表或活动工作表多一天。任何人都可以帮忙吗?







我找到的代码是:



我尝试了什么:



  Sub  Insert_Sheet_Template()
Dim sh As 工作表
Dim shName 作为 字符串

' 工作表模板的名称
shName = template.xltm

' 插入工作表模板
使用 ThisWorkbook
设置 sh = Sheets.Add(类型:= Application.TemplatesPath& shName,_
af ter:=。表格(.Sheets.Count))
结束 使用

' 为工作表命名,此示例中为今天的日期
On 错误 恢复 下一步
sh.Name =格式(日期 < span class =code-string> yyyy-mmm-dd)
如果 Err.Number> 0 然后
MsgBox 更改工作表的名称:& sh.Name& 手动
Err.Clear
结束 如果
开启 错误 GoTo 0
结束 Sub

解决方案

尝试此功能

 私有 功能 FindNextSheetDate() As  字符串 
' 逐步完成ThisWorkbook中的工作表并确定哪个(如果有)是日期
' 确定最新的这些日期并增加1天
' 将该日期作为yyyy-mmm-dd格式的字符串返回
' 如果找不到工作表,则返回今天的日期
' 假设错误处理由调用函数处理

Dim dt As 日期
Dim maxdt 作为 日期
maxdt = CDate (< span class =code-string> 1900-Jan-01

Dim sht 作为 变体
对于 每个 sht ThisWorkbook.Sheets
如果 IsDate(sht.Name)然后
dt = CDate (sht.Name)
如果 dt> maxdt 然后
maxdt = dt
结束 如果
结束 如果
下一步

如果 maxdt = CDate 1900-Jan-01然后
' 将maxdt设置为昨天,因为返回值中有一天的DateAdd
maxdt = DateAdd( d,-1,日期
结束 如果

FindNextSheetDate =格式


(DateAdd( d 1 ,maxdt),< span class =code-string> yyyy-mmm-dd

结束 功能

 sh.Name = FindNextSheetDate()


I am making a new project for clinic reservation in excel and in this project. I require a button to click to create a new sheet with a new date, which is one day more than the date of the previous. I found a code in the internet which create a new sheet with my template but it create the sheet with today date, but what I need is to create sheet with one day more then the last sheet created or the active sheet. Can any one please help?



the code i found is :

What I have tried:

Sub Insert_Sheet_Template()
    Dim sh As Worksheet
    Dim shName As String

    'name of the sheet template
    shName = "template.xltm"

    'Insert sheet template
    With ThisWorkbook
        Set sh = Sheets.Add(Type:=Application.TemplatesPath & shName, _
                            after:=.Sheets(.Sheets.Count))
    End With

    'Give the sheet a name, today's date in this example
    On Error Resume Next
    sh.Name = Format(Date, "yyyy-mmm-dd")
    If Err.Number > 0 Then
        MsgBox "Change the name of Sheet : " & sh.Name & " manually"
        Err.Clear
    End If
    On Error GoTo 0
End Sub

解决方案

Try this function

Private Function FindNextSheetDate() As String
'Steps through the sheets in ThisWorkbook and determines which (if any) are a date
'determines the latest of those dates and adds 1 day 
'returns that date as a string in yyyy-mmm-dd format
'If no sheets are found, returns today's date
'Error handling is assumed to be handled by the calling function

    Dim dt As Date
    Dim maxdt As Date
    maxdt = CDate("1900-Jan-01")
    
    Dim sht As Variant
    For Each sht In ThisWorkbook.Sheets
        If IsDate(sht.Name) Then
            dt = CDate(sht.Name)
            If dt > maxdt Then
                maxdt = dt
            End If
        End If
    Next

    If maxdt = CDate("1900-Jan-01") Then
        'Set maxdt to yesterday as there is a DateAdd of one day in the return value
        maxdt = DateAdd("d", -1, Date)
    End If

    FindNextSheetDate = Format


(DateAdd("d", 1, maxdt), "yyyy-mmm-dd") End Function

Used like

sh.Name = FindNextSheetDate()


这篇关于如何使按钮创建一个名为像上一个工作日期加上一天的新工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆