Excel VBA 编程练习

Excel VBA 编程练习根据表单名称从work查找

大家好,又见面了,我是你们的朋友全栈君。

最近做了一个VBA的小case,用于方便excel数据的处理,主要的功能代码记录如下。

1. 根据表单名称从workbook中查找出特定表单:

    For Each sitem In ThisWorkbook.Worksheets
        If sitem.Name = sname Then
            ' sitem is the object that we wants
            Exit For
        End If
    Next

2. 复制表单m的特定内容到表单n:

Sheets(m).Range("A10:C11").Copy Sheets(n).Cells(1, 1)

3. 删除表单特定区域或者是特定区域的数据验证逻辑规则:

Sheets(m).Range("A10:C11").Delete
Sheets(m).Range("A10:C11").Validation.Delete

4. 添加新的worksheet并更改其名称:

    ThisWorkbook.Worksheets.Add
    ActiveSheet.Name = sname 'ActiveSheet is the new one

5.具体代码

    r = ActiveSheet.UsedRange.Rows.Count
    c = ActiveSheet.UsedRange.Columns.Count
    Dim i As Integer
    Dim j As Integer
    Dim sname As String
    Dim sperson As String
    Dim rgtemp As String
    sname = ActiveSheet.Cells(1, 2).Text
    sperson = ActiveSheet.Cells(1, 4).Text
    If Sheet3.Cells(r, c).Text <> "" Or IsEmpty(sname) Then
        
        MsgBox ("A new sheet (Rig.: " + sname + "; Resp. person: " + sperson + ";) is about to be created.")
        Worksheets.Add
        ActiveSheet.name = sname
        Sheet2.Cells.Copy ActiveSheet.Cells(1, 1)
        rgtemp = "B3:E" + Trim(Str(r))
        Sheet3.Range(rgtemp).Copy ActiveSheet.Cells(18, 5)
        ActiveSheet.Cells(5, 3).Value = sname
        rgtemp = "A3:A" + Trim(Str(r))
        Sheet3.Range(rgtemp).Copy ActiveSheet.Cells(18, 2)
        rgtemp = "A4:E" + Trim(Str(r))
        Sheet3.Range(rgtemp).Delete
        For i = 2 To 5
            Sheet3.Cells(3, i).Value = ""
        Next i
        Sheet3.Cells(1, 2).Value = ""
        Sheet3.Cells(1, 4).Value = ""
        
        Sheet1.Select
        lastrow = ActiveSheet.UsedRange.Rows.Count
        lastcol = ActiveSheet.UsedRange.Columns.Count
        ActiveSheet.Range("A" + Trim(Str(lastrow)) + ":BF" + Trim(Str(lastrow))).Copy ActiveSheet.Range("a" & lastrow).Offset(1, 0)
        For j = 6 To lastcol
            ActiveSheet.Cells(lastrow + 1, j).Value = ""
        Next j
        ActiveSheet.Cells(lastrow + 1, 2).Value = ""
        ActiveSheet.Cells(lastrow + 1, 3).Value = ""
        ActiveSheet.Cells(lastrow + 1, 4).Value = sname
        ActiveSheet.Cells(lastrow + 1, 5).Value = sperson
        MsgBox ("Sheet " + sname + " has been created.")
    Else
        MsgBox ("There must be some wrong with in your input. Please check it again!")
    End If

 

    c = ActiveSheet.UsedRange.Columns.Count
    r = ActiveSheet.UsedRange.Rows.Count
    c = c + 1 'this statement need to be comment if the template has been updated
    For i = 18 To r
        ActiveSheet.Cells(i, 3).Select
        c_thn = 0
        c_ton = 0
        For j = 9 To c
           temp = ActiveSheet.Cells(i, j).Text
           If (temp = "OH" Or temp = "NOH") Then
                c_thn = c_thn + 1
           End If
        Next j
        ActiveCell.Value = c_thn
       
        ActiveSheet.Cells(i, 4).Select
        For j = 9 To c
            temp = ActiveSheet.Cells(i, j).Text
           If (temp = "OH" Or temp = "ONH") Then
                c_ton = c_ton + 1
           End If
        Next j
        ActiveCell.Value = c_ton
    Next i
    
    Dim ofs(12) As Integer
    Dim mydata() As String
    For j = 0 To 11
        ofs(j) = 0
    Next j
    For j = 9 To c
        temp = ActiveSheet.Cells(17, j).Text
        mydata() = Split(temp, "/")
        Select Case CInt(mydata(0))
            Case Is = 1
                ofs(0) = ofs(0) + 1
            Case Is = 2
                ofs(1) = ofs(1) + 1
            Case Is = 3
                ofs(2) = ofs(2) + 1
            Case Is = 4
                ofs(3) = ofs(3) + 1
            Case Is = 5
                ofs(4) = ofs(4) + 1
            Case Is = 6
                ofs(5) = ofs(5) + 1
            Case Is = 7
                ofs(6) = ofs(6) + 1
            Case Is = 8
                ofs(7) = ofs(7) + 1
            Case Is = 9
                ofs(8) = ofs(8) + 1
            Case Is = 10
                ofs(9) = ofs(9) + 1
            Case Is = 11
                ofs(10) = ofs(10) + 1
            Case Else
                ofs(11) = ofs(11) + 1
        End Select
    Next j
    Dim c_pdp(3) As Integer
    
    For i = 0 To 2
        c_pdp(i) = 0
    Next i
    
    Dim idx As Integer
    idx = 0
    Dim leng As Integer
    leng = 0
    Dim k As Integer
    
    For j = 9 To c
        ActiveSheet.Cells(17, j).Select
        For k = 18 To r
            temp = ActiveSheet.Cells(k, j).Text
            If Trim(temp) <> "" Then
                c_pdp(0) = c_pdp(0) + 1
                If temp = "OH" Then
                    c_pdp(1) = c_pdp(1) + 1
                    c_pdp(2) = c_pdp(2) + 1
                ElseIf temp = "NOH" Then
                    c_pdp(1) = c_pdp(1) + 1
                ElseIf temp = "ONH" Then
                    c_pdp(2) = c_pdp(2) + 1
                End If
            End If
        Next k

        leng = 0
        
        For i = 0 To idx
            leng = leng + ofs(i)
        Next i
        
        If j = 8 + leng Then
            ActiveSheet.Cells(12, j - ofs(idx) + 1).Value = c_pdp(0)
            ActiveSheet.Cells(13, j - ofs(idx) + 1).Value = c_pdp(1)
            ActiveSheet.Cells(14, j - ofs(idx) + 1).Value = c_pdp(2)
            If c_pdp(0) = 0 Then
                ActiveSheet.Cells(10, j - ofs(idx) + 1).Value = "No PM planned"
                ActiveSheet.Cells(11, j - ofs(idx) + 1).Value = "No PM planned"
            Else
                ActiveSheet.Cells(10, j - ofs(idx) + 1).Value = c_pdp(1) / CDbl(c_pdp(0))
                ActiveSheet.Cells(11, j - ofs(idx) + 1).Value = c_pdp(2) / CDbl(c_pdp(0))
            End If
            For i = 0 To 2
                c_pdp(i) = 0
            Next i
            idx = idx + 1
        End If
        
    Next j

 
    r = Sheet1.UsedRange.Rows.Count
    c = Sheet1.UsedRange.Columns.Count
    'c = c + 1 'this statement need to be commented if the template has been updated
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim result As String
    Dim thn, ton As Integer
    thn = 0
    ton = 0
    For i = 13 To r
        For Each sht In ThisWorkbook.Worksheets
            temp = Sheet1.Cells(i, 4).Text
            If sht.name = Sheet1.Cells(i, 4).Text Then
                sts = sht.Index
                Exit For
            End If
        Next
        If IsEmpty(sts) Then
            MsgBox ("the sheet is null")
            Exit For
        End If
        ssr = Sheets(sts).UsedRange.Rows.Count
        For j = 18 To ssr
            thn = thn + Sheets(sts).Cells(j, 3).Value
            ton = ton + Sheets(sts).Cells(j, 4).Value
        Next j
        Sheet1.Cells(i, 2).Value = thn
        Sheet1.Cells(i, 3).Value = ton
        For j = 6 To c
            result = ""
            
            For k = 18 To ssr
                temp = Sheets(sts).Cells(k, j + 3).Text
                If Trim(temp) <> "" Then
                    result = result + Sheets(sts).Cells(k, 7).Text + " "
                End If
            Next k
            Sheet1.Cells(i, j).Value = Trim(result)
        Next j
    Next i

 
    Dim r, c As Integer
    c = ActiveSheet.UsedRange.Columns.Count
    c = c + 1 'this statement need to be commented if the template has been updated
    
    ActiveSheet.Range("I" & 10, "I" & 18).Copy Sheet1.Cells(5, 6)
    c = Sheet1.UsedRange.Columns.Count
    For j = 1 To c
        Sheet1.Cells(13, j).Validation.Delete
    Next j



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/135423.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • idea 2021 激活码 3月最新注册码

    idea 2021 激活码 3月最新注册码,https://javaforall.cn/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

  • 武侠世界中的ERP乱弹[通俗易懂]

    1990年一位威力大侠(Wylie)发明了ERP,它内通百窍(企业各个业务部门),外通天地二气(客户,供应商),彻底摆脱了原先MRP,MRPII对人体的约束,从此武侠世界进入了新的篇章,各式奇功妙法,各路英雄豪杰辈出,好一派繁荣盛世!二十几年的血雨腥风过去了,江湖格局日趋稳定。天下以五派为尊,分别是SAP,Oracle,Sage,Infor,Microsoft,其它小门小派,不一一细数。作

  • QQ机器人制作教程,超详细「建议收藏」

    QQ机器人制作教程,超详细「建议收藏」目录前期准备1、机器人框架的下载和配置2、python的配置和安装具体实现1、发送信息2、获取群成员列表3、接收上报的事件4、实现简单的自动回复下一篇文章介绍更多功能前期准备1、机器人框架的下载和配置首先需要一个qq机器人框架,我使用的是基于mirai以及MiraiGo开发的go-cqhttp(里面有开发文档)。框架下载地址Windows下32位文件为go-cqhttp-v*-windows-386.zipWindows下64位文件为go-cqhttp-v*-windows-amd6

  • BeanUtils工具类常用方法「建议收藏」

    BeanUtils工具类常用方法「建议收藏」        BeanUtils是Apachecommons组件的成员之一,主要用于简化JavaBean封装数据的操作。它可以给JavaBean封装一个字符串数据,也可以将一个表单提交的所有数据封装到JavaBean中。使用第三方工具,需要导入jar包:BeanUtils工具常用工具类有两个:BeanUtils、ConvertUtils。BeanUtils用于封装数据,ConvertUti…

  • 卸载52好压,极速输入法,手机模拟大师这些流氓软件[通俗易懂]

    卸载52好压,极速输入法,手机模拟大师这些流氓软件[通俗易懂]卸载52好压,极速输入法的方法:通过控制面板里卸载了之后发现这两个流氓软件居然还在?于是换了一种卸载方法:先在官网下载并重新安装这两个软件,如果电脑已经安装了旧版本会提示安装的新版本会覆盖旧的版本,然后通过腾讯电脑管家卸载卸载手机模拟大师:在D盘找到LDSGameMaster,在文件夹里找到uninst,双击后卸载不要直接删除手机模拟大师的文件!如果直接删除文件到最后会发现有一个文件死活删不了,要通过卸载才能删除干净…

  • 将链接地址转换为二维码并且复制文字_二维码怎么转换成链接

    将链接地址转换为二维码并且复制文字_二维码怎么转换成链接前言:我的需求是讲链接地址转换成二维码,供用户去使用并展示H5端,这里会说到一些小细节,先上代码吧~1.html结构2.生成二维码3.复制二维码要注意的一点是:首先二维码的密度是根据参数的多少来显示的,参数如果特别多,就会导致二维码密度太密,用户拿手机是扫不出来的.解决方案:1.要后端或者自己写一个接口专门放这些地址,可以理解成压缩.然后拿到压缩的东西再去转码.2.把在另外一端能获取到的参数,通过方式获取到,在转码的时候尽量减少参数的携带,带上必要..

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号