Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3218|回复: 5

[已解决]求改编码

[复制链接]
发表于 2014-4-10 11:07 | 显示全部楼层 |阅读模式
本帖最后由 文轩馨婷 于 2014-4-10 13:12 编辑

要求:
        如何在下面编程中在Strsheetname1 表中点击按钮自动生产Strsheetname2 表,当再次在Strsheetname1 表中点击按钮时会弹出提示框“已生成了Strsheetname2 ,是否重新生成”;并且重新生成后原已生成的Strsheetname2 表将被再次生成的表替代!


Option Explicit
Dim Strsheetname1 As String, Strsheetname2 As String, Ilen As Integer
Sub Chapter13()
    Strsheetname1 = ActiveSheet.Name '
    Ilen = Len(Strsheetname1) '
    Sheets.Add after:=Sheets(Strsheetname1) '
    Strsheetname2 = Left(Strsheetname1, Ilen) + "工资条"
    ActiveSheet.Name = Strsheetname2
    Chapter13_1 '
End Sub





最佳答案
2014-4-10 12:58
本帖最后由 810126769 于 2014-4-10 13:01 编辑

再简化一下:
  1. Option Explicit
  2. Dim Strsheetname1 As String, Strsheetname2 As String, Ilen As Integer
  3. Sub Chapter13()
  4.    Dim sh As Worksheet
  5.    Application.DisplayAlerts = False
  6.     Strsheetname1 = ActiveSheet.Name '
  7.     Ilen = Len(Strsheetname1) '
  8.     Strsheetname2 = Left(Strsheetname1, Ilen) + "工资条"
  9.      For Each sh In Sheets
  10.            If sh.Name = Strsheetname2 Then
  11.                If MsgBox("已生成了Strsheetname2 ,是否重新生成", vbOKCancel) = 1 Then
  12.                   Sheets(Strsheetname2).Delete
  13.                Else
  14.                   Exit Sub
  15.                End If
  16.                Exit For
  17.            End If
  18.       Next sh
  19.           Sheets.Add after:=Sheets(Strsheetname1) '
  20.           ActiveSheet.Name = Strsheetname2
  21.           Sheets(Strsheetname1).Activate
  22.           Chapter13_1 '
  23.   Application.DisplayAlerts = True
  24. End Sub
复制代码
发表于 2014-4-10 12:35 | 显示全部楼层
本帖最后由 810126769 于 2014-4-10 12:38 编辑

Option Explicit
Dim Strsheetname1 As String, Strsheetname2 As String, Ilen As Integer
Sub Chapter13()
   dim sh as worksheet,m as byte,bl as boolean
   application.displayalerts=false
    Strsheetname1 = ActiveSheet.Name '
    Ilen = Len(Strsheetname1) '
    Strsheetname2 = Left(Strsheetname1, Ilen) + "工资条"
     for each sh in sheets
           if sh.name=Strsheetname2  then
                bl=true
               m = MsgBox("已生成了Strsheetname2 ,是否重新生成", vbOKCancel)
               exit for
           end if
      next sh
  if bl=true then
     if m=1 then
          sheets(Strsheetname2 ).delete
          Sheets.Add after:=Sheets(Strsheetname1) '
          ActiveSheet.Name = Strsheetname2
          Chapter13_1 '
     else
         exit sub
      end if
end if
  application.displayalerts=true
End Sub
回复

使用道具 举报

发表于 2014-4-10 12:58 | 显示全部楼层    本楼为最佳答案   
本帖最后由 810126769 于 2014-4-10 13:01 编辑

再简化一下:
  1. Option Explicit
  2. Dim Strsheetname1 As String, Strsheetname2 As String, Ilen As Integer
  3. Sub Chapter13()
  4.    Dim sh As Worksheet
  5.    Application.DisplayAlerts = False
  6.     Strsheetname1 = ActiveSheet.Name '
  7.     Ilen = Len(Strsheetname1) '
  8.     Strsheetname2 = Left(Strsheetname1, Ilen) + "工资条"
  9.      For Each sh In Sheets
  10.            If sh.Name = Strsheetname2 Then
  11.                If MsgBox("已生成了Strsheetname2 ,是否重新生成", vbOKCancel) = 1 Then
  12.                   Sheets(Strsheetname2).Delete
  13.                Else
  14.                   Exit Sub
  15.                End If
  16.                Exit For
  17.            End If
  18.       Next sh
  19.           Sheets.Add after:=Sheets(Strsheetname1) '
  20.           ActiveSheet.Name = Strsheetname2
  21.           Sheets(Strsheetname1).Activate
  22.           Chapter13_1 '
  23.   Application.DisplayAlerts = True
  24. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-4-10 13:11 | 显示全部楼层
810126769 发表于 2014-4-10 12:58
再简化一下:

可否把中文解释也注释上去!——新手上路!

谢谢!
回复

使用道具 举报

发表于 2014-4-10 15:32 | 显示全部楼层
文轩馨婷 发表于 2014-4-10 13:11
可否把中文解释也注释上去!——新手上路!

谢谢!
  1. Option Explicit
  2. Dim Strsheetname1 As String, Strsheetname2 As String, Ilen As Integer '定义模块级变量
  3. Sub Chapter13()
  4.    Dim sh As Worksheet '定义工作表对象变量
  5.    Application.DisplayAlerts = False '关闭警告提示
  6.     Strsheetname1 = ActiveSheet.Name ' '给变量Strsheetname1赋值为活动工作表的名称
  7.     Ilen = Len(Strsheetname1) ' '给变量Ilen赋值,让它等于活动工作表名的字符数,其实这句可以删除,把下一句改为:Strsheetname2 = Strsheetname1 & "工资条"
  8.     Strsheetname2 = Left(Strsheetname1, Ilen) + "工资条" '给变量Strsheetname2赋值让它等于活动工作表的名称连接上"工资条"
  9.      For Each sh In Sheets '在所有的工作表中进行循环sh这个工作表变量
  10.            If sh.Name = Strsheetname2 Then '如果sh这个工作表名与Strsheetname2这个名称相同那么执行下面的代码
  11.                If MsgBox("已生成了Strsheetname2 ,是否重新生成", vbOKCancel) = 1 Then '弹出提示对话框,提示Strsheetname2这个工作表已经生成了,是否重新生成。设置对话框生成两个按钮:确定和取消。如果点击确定,它就返回一个值:1。如果其值为1,那么执行下面的代码。
  12.                   Sheets(Strsheetname2).Delete '删除原有的Strsheetname2这个工作表
  13.                Else '否则执行下面的代码
  14.                   Exit Sub '退出本程序
  15.                End If '结束if判断
  16.                Exit For '退出循环
  17.            End If '结束if判断
  18.       Next sh '循环下一个sh
  19.           Sheets.Add after:=Sheets(Strsheetname1) ' '在工作表Strsheetname1之后插入新的工作表
  20.           ActiveSheet.Name = Strsheetname2 '新插入的工作表改名为:Strsheetname2
  21.           Sheets(Strsheetname1).Activate '激活按钮所在的工作表,以方便下一个操作
  22.           Chapter13_1 ' '调用另一个程序: Chapter13_1
  23.   Application.DisplayAlerts = True '打开警告提示
  24. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-4-10 19:50 | 显示全部楼层
810126769 发表于 2014-4-10 15:32

十分感谢!{:171:}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-25 15:28 , Processed in 0.261761 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表