解决原始文件(包含2个标题行第一行是合并单元格数据内容)根据行数切割失败办法

5 篇文章 0 订阅

解决原始文件(包含2个标题行第一行是合并单元格数据内容)根据行数切割失败办法

暂时解决办法如下:

  1. 假设原始文件内容如下:
    在这里插入图片描述
  2. 请先将第一行中ABC单元格的内容先暂时保存一下:
    客户数据导入模板
    注意请XXXXXXXXXXXXXXXXXXXXXXXX
  3. 将第一行内容保存之后,请在原始文件中删掉第一行
    在这里插入图片描述
  4. 在删掉第一行中的原始文件中执行切割文件的宏
Sub splitexcel()
Dim r, c, i, totalhangshu, fileshu, bt As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
tRow = Val(Application.InputBox("请您输入需要切割的行数?"))
If tRow = 0 Then MsgBox "您未输入行数,程序退出!": Exit Sub
r = Range("A" & Rows.Count).End(xlUp).Row
c = Cells(1, Columns.Count).End(xlToLeft).Column
bt = 1 'title
totalhangshu = tRow 'num
fileshu = IIf(r - bt Mod 20000, Int((r - bt) / totalhangshu), Int((r - bt) / totalhangshu) + 1)
For i = 0 To fileshu
    Workbooks.Add
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Format(i, String(Len(fileshu), 0)) & ".xlsx"
    Application.DisplayAlerts = True
    ThisWorkbook.ActiveSheet.Range("A1").Resize(bt, c).Copy ActiveSheet.Range("A1")
    ThisWorkbook.ActiveSheet.Range("A" & bt + i * totalhangshu + 1).Resize(totalhangshu, c).Copy _
     ActiveSheet.Range("A" & bt + 1)
    ActiveWorkbook.Close True
Next
End Sub

具体操作请看文章:https://blog.csdn.net/weixin_43046974/article/details/113251276

  1. 在切割完成的文件目录中新建一个excel文件,执行如下宏:
    在这里插入图片描述
    执行如下宏:
Sub 插入()
Dim XLSX
Dim X1
XLSX = Dir(ThisWorkbook.Path & "\*.xlsx")   '指定要遍历excel文件的路径及文件类型
X1 = Application.InputBox("请您输入这一单元格的内容")
X2 = Application.InputBox("请您输入合并单元格的范围:比如“A1:C1”")
Do While XLSX <> ""
    If XLSX <> ThisWorkbook.Name Then
        Set XLSX = Workbooks.Open(ThisWorkbook.Path & "\" & XLSX)   '打开excel文件
        Sheets(1).Rows("1:1").Select
        Selection.Insert Shift:=xlShiftDown
        Sheets(1).Select
        Sheets(1).Cells(1, 1).Value = X1 '在A1单元格输入内容
        Sheets(1).Range(X2).Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        XLSX.Close True
    End If
XLSX = Dir '下一个excel文件
Loop
Set XLSX = Nothing '释放变量内存
End Sub

在这里插入图片描述
在这里插入图片描述
6. 我们观察结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值