ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 147|回复: 1

钟表

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-6-18 08:17 来自手机 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
求VBA钟表代码
IMG_5280.jpeg

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-6-18 08:19 来自手机 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 开始运行()
    ActiveSheet.Shapes.SelectAll
    Selection.Delete
     Call 生成时钟图像
    On Error Resume Next
    If Err <> 0 Then Exit Sub
    For i = 1 To 600
    ActiveSheet.Shapes("秒针").Rotation = 6 *Int(Timer Mod 60)
   
    ActiveSheet.Shapes("分针").Rotation = 6 *Int(Timer / 60 Mod 60)
    ActiveSheet.Shapes("时针").Rotation = 30 *Int(Timer / 3600)
   
    Pause_T 1  
    Next i
End Sub
Function Pause_T(n As Integer)  '暂停n秒,期间可以进行其他操作
    '前面的代码
    t = Timer
    While Timer < t + n
        DoEvents
    Wend
    '后面的代码
End Function


Sub 生成时钟图像()
'
'定义中心位置
Dim x, y As Integer
Const pi = 3.1415926
x = Selection.Left
y = Selection.Top
If x < 80 Or y < 80 Then MsgBox "中心位置太靠边缘,请重新选择", vbDefaultButton2, x & y: ExitSub
On Error Resume Next
ActiveSheet.Shapes.Delete
   
'画表盘

   ActiveSheet.Shapes.AddShape(msoShapeOval, 425, 50, 575, 190).Select
   
    With Selection.ShapeRange
        .Name = "表盘"
        .Height = 150
        .Width = 150
        .Top = y - 150 / 2
        .Left = x - 150 / 2
    End With

    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor= msoThemeColorBackground1
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0
        .Transparency = 0
        .Solid
        .ForeColor.RGB = RGB(0, 176,80)
    End With
   
    With Selection.ShapeRange.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(112,48, 160)
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0.5
        .Transparency = 0
        .Style = msoLineThickThin
        .Visible = msoTrue
        .Weight = 8
    End With
'画刻度
    For i = 0 To 29
       ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 500, 55, 500,60).Select
        
        With Selection.ShapeRange
             .Name = "刻度A" & i
             If i Mod 5 = 0 Then
                .Height = 10
                .Line.ForeColor.RGB= vbRed
            Else
                 .Height = 8
                .Line.ForeColor.RGB= vbBlack
            End If
            .Width = 0
            .Line .Visible = msoTrue
            .Line.Weight = 2
             .Top = y - 70
             .Left = x
        End With
        
       ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 500, 185, 500,190).Select
        
        With Selection.ShapeRange
                .Name = "刻度B" & i
             If i Mod 5 = 0 Then
                .Height = 10
                .Line.ForeColor.RGB= vbRed
            Else
                .Height = 8
               .Line.ForeColor.RGB =vbBlack
            End If
            .Width = 0
            .Line .Visible = msoTrue
            .Line.Weight = 2
            .Top = y + 70 - .Height
            .Left = x
        End With
        
       ActiveSheet.Shapes.Range(Array("刻度A" & i, "刻度B"& i)).Group.Select
        Selection.ShapeRange.Name ="刻度" & i
       Selection.ShapeRange.Rotation = 6 * i
    Next i
'添加铭牌
   ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, _
        55, 32, 15).Select
    With Selection.ShapeRange
        .Height = 20
        .Width = 80
        .Line.Visible = msoFalse
        .Fill.Visible = msoFalse
        .Top = y + 15
        .Left = x - 40
       .TextFrame2.TextRange.Font.Name = "微软雅黑"
       .TextFrame2.TextRange.Font.Size = 10
       .TextFrame2.TextRange.Font.Bold = True
       .TextFrame2.TextRange.Characters.Text = "VBAHOME"
        .TextFrame2.TextRange.Characters().Font.Fill.ForeColor.RGB= vbWhite
        .TextFrame2.VerticalAnchor =msoAnchorMiddle
       .TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
    End With
'添加表盘数字
For j = 1 To 12
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal,100, _
        55, 32, 15).Select
   With Selection.ShapeRange
    .Height = 8
    .Width = 16
    .Line.Visible = msoFalse
    .Fill.Visible = msoFalse
   
        If Sin(j * pi / 6) >= 0Then
            .Top = y + 60 * Sin(j *pi / 6) - 8
        Else
            .Top = y + 60 * Sin(j *pi / 6)
        End If
        
        If Cos(j * pi / 6) >= 0Then
            .Left = x + 60 * Cos(j *pi / 6) - 16
        Else
            .Left = x + 60 * Cos(j *pi / 6)
        End If
        
        If (j + 2) Mod 12 + 1 = 6Then
            .Left = x + 60 * Cos(j *pi / 6) - 8
            .Top = y + 60 * Sin(j *pi / 6) - 8
        End If
        If (j + 2) Mod 12 + 1 = 12Then
            .Left = x + 60 * Cos(j *pi / 6) - 8
        End If
     
     End With
     
    WithSelection.ShapeRange.TextFrame2
        .TextRange.Characters.Text =(j + 2) Mod 12 + 1
        .TextRange.Font.Name ="微软雅黑"
        .TextRange.Font.Size = 8
        .TextRange.Font.Bold = True
        .TextRange.Characters().Font.Fill.ForeColor.RGB= RGB(255, 0, 0)
        .MarginLeft = 0
        .MarginRight = 0
        .MarginTop = 0
        .MarginBottom = 0
       .TextRange.ParagraphFormat.Alignment = msoAlignCenter
        .VerticalAnchor =msoAnchorMiddle
    End With
   
Next j
'画时针
   ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 500, 120, 500,90).Select
   
    With Selection.ShapeRange
        .Name = "时针1"
        .Line.EndArrowheadStyle =msoArrowheadTriangle
        .Height = 30
        .Width = 0
        .Top = y - 30
        .Left = x
        .Line.Visible = msoTrue
        .Line.Weight = 4
    End With
   
       ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 500, 120, 500,150).Select
   
    With Selection.ShapeRange
        .Name = "时针2"
        .Line.EndArrowheadStyle =msoArrowheadTriangle
        .Height = 30
        .Width = 0
        .Top = y
        .Left = x
        .Line.Visible = msoFalse
        .Line.Weight = 4
    End With
   
  ActiveSheet.Shapes.Range(Array("时针2", "时针1")).Group.Select
    Selection.ShapeRange.Name ="时针"
   


'画分针
   ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 500, 120, 500,80).Select
   
     With Selection.ShapeRange
        .Name = "分针1"
        .Line.EndArrowheadStyle =msoArrowheadTriangle
        .Height = 40
        .Width = 0
        .Top = y - 40
        .Left = x
        .Line.Visible = msoTrue
        .Line.Weight = 3
    End With
    ActiveSheet.Shapes.AddConnector(msoConnectorStraight,500, 120, 500, 160).Select
   
    With Selection.ShapeRange
        .Name = "分针2"
        .Line.EndArrowheadStyle =msoArrowheadTriangle
        .Height = 40
        .Width = 0
        .Top = y
        .Left = x
        .Line.Visible = msoFalse
        .Line.Weight = 3
    End With

   ActiveSheet.Shapes.Range(Array("分针2", "分针1")).Group.Select
    Selection.ShapeRange.Name ="分针"
   
'画秒针

   ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 500, 120, 500, 70).Select
   
    With Selection.ShapeRange
        .Name = "秒针1"
        .Line.EndArrowheadStyle =msoArrowheadTriangle
        .Height = 50
        .Width = 0
        .Top = y - 50
        .Left = x
        .Line.Visible = msoTrue
        .Line.Weight = 2
    End With
   
   ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 500, 120, 500,170).Select
   
     With Selection.ShapeRange
        .Name = "秒针2"
        .Line.EndArrowheadStyle =msoArrowheadTriangle
        .Height = 50
        .Width = 0
        .Top = y
        .Left = x
        .Line.Visible = msoFalse
        .Line.Weight = 2
    End With
   
   ActiveSheet.Shapes.Range(Array("秒针2", "秒针1")).Select
   Selection.ShapeRange.Group.Select
    Selection.ShapeRange.Name ="秒针"
   
        '画表中心
   ActiveSheet.Shapes.AddShape(msoShapeOval, 495, 116.25, 525,12.75).Select
   
    With Selection.ShapeRange
        .Name = "表心"
        .Height = 8
        .Width = 8
        .Top = y - 8 / 2
        .Left = x - 8 / 2
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB =RGB(255, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Line.Visible = msoTrue
        .Line.ForeColor.RGB = RGB(0,0, 255)
        .Line.Transparency = 0
        .ZOrder msoBringToFront
    End With
R = ActiveSheet.Shapes("表盘").TopLeftCell.Address
Range(R).Select
End Sub

评分

1

查看全部评分

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

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-19 21:53 , Processed in 0.025619 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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