文本导入excel分列(pdf粘贴到excel怎么分列)

可执行宏命令。

打开总表1.xlsm文件,执行宏ThisWorkbook.Macro2

代码如下:

Sub Macro2()

Dim fileDir As String ‘文本文件目录

Dim fileName As String ‘要打开的文本文件名

Dim serverNo As String

Dim serverDate As String

Dim dataSum As Long ‘要合并的文本记录数

Dim dataTotalOld As Long ‘汇总表中未合并时的记录条数

Application.ScreenUpdating = False

fileDir = ActiveWorkbook.Path & “”

fileName = Dir(fileDir, 7)

Do While fileName <> “” And Right(fileName, 3) = “txt”

‘fileName = “1 4-10.txt”

‘获取服务器号和日期

serverNo = Left(fileName, InStr(1, fileName, ” “) – 1) & “服”

serverDate = Mid(fileName, InStr(1, fileName, ” “) + 1)

serverDate = Replace(serverDate, “-“, “月”)

serverDate = Replace(serverDate, “.txt”, “日”)

Workbooks.OpenText fileName:= _

ActiveWorkbook.Path + Application.PathSeparator & fileName, Origin:=936, _

StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _

ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _

, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _

Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True

dataSum = Range(“a65535”).End(xlUp).Row

Range(“A1:D” & dataSum).Select

Range(“A1:D” & dataSum).Select

Selection.Copy

‘总表1.xlsm为要合并后的启动宏工作表

Workbooks(“总表1.xlsm”).Activate

dataTotalOld = Range(“a65535”).End(xlUp).Row + 1

If dataTotalOld = 2 Then dataTotalOld = 1 ‘第一次使用

Range(“C” & dataTotalOld).Select

ActiveSheet.Paste

Range(“A” & dataTotalOld) = serverNo

Range(“B” & dataTotalOld) = serverDate

Range(“A” & dataTotalOld + 1) = serverNo

Range(“B” & dataTotalOld + 1) = serverDate

Range(“A” & dataTotalOld & “:B” & dataTotalOld + 1).Select

Selection.AutoFill Destination:=Range(“A” & dataTotalOld & “:B” & dataTotalOld + dataSum – 1), Type:=xlFillDefault

Workbooks(fileName).Close savechanges:=False

fileName = Dir

Debug.Print fileName

Loop

Application.ScreenUpdating = True

End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 86345@qq.com 举报,一经查实,本站将立刻删除。
(0)
上一篇 2022-04-06 14:20:06
下一篇 2022-04-06 14:30:05

猜你喜欢

发表回复

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