当前位置: 首页 > news >正文

VBA 复制指定次数的List

实现功能如下:
excel指定行列范围内的所有单元格
规则1:从第一列开始到最后一列,每一列的每一行的单元格包含List<的话,竖着复制指定次数
规则2:规则1复制后,每一次复制后把(0)替换为(1)(2)等等序号
规则3:从第一列开始到最后一列,每一列的每一行连续相同的值,合并单元格。
规则4:规则3的合并单元,右侧的合并范围不能超过左侧的范围

如下图:
原Excel
在这里插入图片描述
目的Excel
在这里插入图片描述

Sub TestCopyFunction()
    Application.ScreenUpdating = False
    On Error GoTo ErrorHandler
    
    Dim startRow As Long: startRow = 1
    Dim startCol As Long: startCol = 1
    Dim endRow As Long: endRow = 24
    Dim endCol As Long: endCol = 6
    Dim copyTimes As Long: copyTimes = 2
    
    CopyRowsInMergedCells Sheet1, startRow, startCol, endRow, endCol, copyTimes
    
ExitSub:
    Application.ScreenUpdating = True
    Exit Sub
ErrorHandler:
    MsgBox "错误 " & Err.Number & ": " & Err.Description, vbCritical
    GoTo ExitSub
End Sub

Function CopyRowsInMergedCells(ws As Worksheet, startRow As Long, startCol As Long, endRow As Long, endCol As Long, copyTimes As Long)
    Dim mergeRowCount As Long, totalmergeRowCount As Long
    
    For i = startCol To endCol
        j = startRow
        Do While j <= endRow
            totalmergeRowCount = 0
            mergeRowCount = 0
            If InStr(1, ws.Cells(j, i).value, "List<", vbTextCompare) > 0 Then
                '如果是合并单元格
                If ws.Cells(j, i).MergeCells Then
                    '取得合并单元格的行数
                    mergeRowCount = ws.Cells(j, i).mergeArea.Rows.Count
                Else
                    mergeRowCount = 1
                End If
                
                For k = 1 To copyTimes
                    Rows((j + k * mergeRowCount) & ":" & (j + k * mergeRowCount)).Resize(mergeRowCount).Insert '在第5行下方插入3行空白行?:ml-citation{ref="4,6" data="citationList"}
                    
                    Range(Cells(j, i), Cells(j + mergeRowCount - 1, endCol)).Copy Destination:=Cells(j + k * mergeRowCount, i)
                    
                    Cells(j + k * mergeRowCount, i).value = Replace(Cells(j + k * mergeRowCount, i).value, "(0)", "(" & k & ")")
                    
                    totalmergeRowCount = totalmergeRowCount + mergeRowCount
                Next k
                
                endRow = endRow + totalmergeRowCount
                j = j + totalmergeRowCount + mergeRowCount - 1
            End If
            j = j + 1
        Loop
    Next i

    '合并单元格
    For i = startCol To endCol
        j = startRow
        Do While j <= endRow
            mergeRowCount = 0
            '空单元格+一致单元格
            For k = j To endRow
                If ws.Cells(k, i).value = "" Or ws.Cells(k, i).value = ws.Cells(j, i).value Then
                    mergeRowCount = mergeRowCount + 1
                Else
                    Exit For
                End If
            Next k
            
            '可以合并
            If mergeRowCount > 1 Then
                '计算左侧单元格的合并范围
                If i > startCol Then
                    For k = j + 1 To k + mergeRowCount
                        If ws.Cells(k, i - 1).value <> "" Then
                            Exit For
                        End If
                    Next k
                
                    '如果超过了范围,则订正范围
                    If j + mergeRowCount - 1 <= k - 1 Then
                        k = j + mergeRowCount
                    End If
                End If
                
                Application.DisplayAlerts = False
                With ws.Range(Cells(j, i), Cells(k - 1, i))
                        .Merge
                        .HorizontalAlignment = xlLeft
                        .VerticalAlignment = xlCenter
                End With
                Application.DisplayAlerts = True
                j = k
            Else
                j = j + 1
            End If
        Loop
    Next i
End Function

相关文章:

  • 最小生成树--Kruskal
  • 清华与人大最新研究表明:AGI的到来时间需70年与10^26个参数,好像不用那么急了...
  • 【计算机组成原理】第一章 计算机系统概述
  • 【The Rap of China】2018
  • 数据结构--【栈与队列】笔记
  • 2020CVPR-SiamBAN:用于视觉跟踪的Siamese框自适应网络
  • 【已解决】AttributeError: module ‘numpy‘ has no attribute ‘object‘.
  • Unity Shader学习总结
  • Linux安装升级docker
  • 指针的工作原理,函数的传值和传址
  • 第6届传智杯复赛第一场
  • 代码随想录算法训练营第三十二天(20250228) |509. 斐波那契数,70. 爬楼梯,746. 使用最小花费爬楼梯 -[补卡20250309]
  • ES Module 的 import 导入和 import () 动态导入
  • Blueprint —— Blueprint Editor(二)
  • 牛客周赛A:84:JAVA
  • 【移动WEB开发】rem适配布局
  • 【Kotlin】Kotlin基础笔记
  • 用python 的 sentiment intensity analyzer的情感分析器,将用户评论进行分类
  • HPC超算系列4——官方指南文档
  • Dify使用日常:我是如何按标题级别将word中的内容转存到excel中的
  • 最好的网站开发系统/seo运营招聘
  • html5 网站logo/怎么制作自己的个人网站
  • 网站面包屑导航设计特点/太原做网站的工作室
  • 网站设计用什么做/怎么做网页设计的页面
  • 专门做预售的网站/网址提交
  • 做高端网站/福州短视频seo网红