解决原始文件(包含2个标题行第一行是合并单元格数据内容)根据行数切割失败办法
暂时解决办法如下:
- 假设原始文件内容如下:
- 请先将第一行中ABC单元格的内容先暂时保存一下:
客户数据导入模板
注意请XXXXXXXXXXXXXXXXXXXXXXXX - 将第一行内容保存之后,请在原始文件中删掉第一行
- 在删掉第一行中的原始文件中执行切割文件的宏
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
- 在切割完成的文件目录中新建一个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. 我们观察结果