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



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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 腾讯云服务器配置ssl,腾讯云服务器SSL证书申请及配置[通俗易懂]

    腾讯云服务器配置ssl,腾讯云服务器SSL证书申请及配置[通俗易懂]最近在研究微信小程序,服务端需要部署在一台服务器上,查看了一下,腾讯云在搞活动,就申请了腾讯云的服务器,但是微信小程序访问需要用https协议才能请求,于是研究了一下如何申请及配置ssl证书。本人穷逼一枚,一向以节俭,所以申请了一个免费证书。申请步骤如下:1、登录证书申请页面https://console.qcloud.com/ssl/apply2、输入必要信息,通用名称及申请邮箱,点击下一步这一…

    2022年9月4日
    4
  • 如何把域名解析到网站空间IP上?

    如何把域名解析到网站空间IP上?

    2021年9月20日
    53
  • xshell 激活成功教程版安转教程

    xshell 激活成功教程版安转教程转自:https://www.cnblogs.com/bowendown/p/11937159.html,亲测perfect!目录一、xshell6商业版安装教程1.为什么要用xshell2

    2022年8月4日
    4
  • ag-grid 学习

    ag-grid 学习项目要将angular从1.5升级到5,ui-grid在5中并不支持,所以为了替换ui-grid,来学习了ag-grid。简单来说,2者相差并不大,使用方式也大致雷同,这里用

    2022年8月5日
    24
  • java基础API

    java基础APIJava常用类库1.API(1)ApplicationProgrammingInterface,应用程序接口。是一些预先定义的类和接口,或指软件系统不同组成部分衔接的约定。(2)API说明文档API文档查看方式:第一步选择包,第二步,选择类或接口,第三步查看类和接口的使用说明,右边的区域。右边的区域分为五块,分别为类的定义和功能介绍、属性的介绍、构造器的介绍、构造器的介绍、方法的列表、每个方法的使用详细说明。2、java.lang包(1)这是我们api中最基础的一个包(2)该包下面的

    2022年7月9日
    20
  • 百度地图开放平台开发者注册_全国矢量地图shp格式百度云

    百度地图开放平台开发者注册_全国矢量地图shp格式百度云SHA1分为发布版和开发版,便于开发者开发调试和最终上线使用。1通过Eclipse获取使用adt22以上版本,可以在Eclipse中直接查看,具体位置如下:Windows:依次在Eclipse中打开Window->Preferances->Android->Build;Mac:依次在Eclipse中打开Eclipse/ADT->Preferances->Android->Build。在弹出的对话框中SHA1f

    2022年8月10日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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