实现功能:
将XXX.xlsx某列的月工资格式如:2056.85或者2141或者1,234.56替换XX.Txt文件下0.00格式的内容。
注意事项:
[if !supportLists]1. [endif]一定要将XX.txt和XXX.xlsx一定要做好备份!备份!
[if !supportLists]2. [endif]1.XX.txt文件需要和XXX.xlsx文件在同一级别目录
[if !supportLists]3. [endif]2. XX.txt文件需要和XXX.xlsx文件两个文件内容。
VBA宏代码实现如下:
Private Sub ExportText()
'读取当前文件夹下的\23070113112223.txt文档内容,保存到brr,关闭文件
Application.ScreenUpdating = False
arr = ActiveSheet.[a1].CurrentRegion
Open ThisWorkbook.Path & "\23070113112223.txt" For Input As #1
s = StrConv(InputB(LOF(1), 1), vbUnicode)
brr = Split(s, vbCrLf)
Close #1
'读取execel文档,将第九列"月工资"读取出来并格式化字符串,保存到ly中,输出到23070113112223.txt,关闭文件
Dim objApp As Object
Dim objWbk As Object
Dim objSht As Object
Dim i&, strTemp$, strWorkBook$
'工作簿文件名自己处理
strWorkBook = "D:\text\2020.1.13嘉荫县客运有限责任公司-15246914997-23070113111820.xlsx"
Set objApp = CreateObject("Excel.Application")
Set objWbk = objApp.Workbooks.Open(strWorkBook)
'要导出哪张表,自己处理
Set objSht = objWbk.Sheets(1)
'输出的文本文件名自己处理
Open "D:\text\23070113112223.txt" For Output As #1
i = 4 '从第4行开始,处理工资变量
For j = 0 To UBound(brr)
strTemp = brr(j)
k = 4 '从第4行开始,excel文档读取身份证号到末尾为空代表结束.
Do
sfz = objSht.Cells(k, 6)
'xm = objSht.Cells(k, 3)
If (sfz = "") Then Exit Do
'身份证,姓名必须与txt文档某一行完全匹配,格式化金额当前字符串,否则,原文本输出
If InStr(brr(j), sfz) <> 0 And sfz <> "" Then
lb = objSht.Cells(k, 9)
ld = Format(lb, "General Number")
lc = Format(ld, "Fixed")
ly = Left(strTemp, InStrRev(strTemp, "0.00") - 1) & lc
strTemp = ly
' If j > 0 Then
'i = i + 1
'End If
Exit Do
End If
k = k + 1
Loop
Print #1, strTemp
Next j
Close #1
objWbk.Close False
Set objSht = Nothing
Set objWbk = Nothing
Set objApp = Nothing
MsgBox "数据导出完毕!", vbInformation
End Sub
操作过程:
效果如下:
网友评论