【发布时间】:2017-05-19 04:49:03
【问题描述】:
以下是我目前的步骤
Access 文件通过宏(VBA 代码)导出 CSV 文件
导出的 CSV 文件被宏(VBA 代码)修改
现在,我在 Access 上执行宏(步骤 1)-> 在导出的文件下,添加代码并运行(步骤 2)
我想简化流程。
是否可以通过执行第 1 步,将第 2 步的代码添加到 csv 文件并运行?
第一步代码
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim intFile As Integer
Dim strFilePath As String
Dim intCount As Integer
Dim strHold
strFilePath = "C:\temp\TEST.csv"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Import", dbOpenForwardOnly)
intFile = FreeFile
Open strFilePath For Output As #intFile
Do Until rst.EOF
For intCount = 0 To rst.Fields.Count - 1
strHold = strHold & rst(intCount).Value & "|"
Next
If Right(strHold, 1) = "|" Then
strHold = Left(strHold, Len(strHold) - 1)
End If
Print #intFile, strHold
rst.MoveNext
strHold = vbNullString
Loop
Close intFile
rst.Close
Set rst = Nothing
End Function
第二步代码
Sub deleterows()
lastrow = Cells(Rows.Count, 4).End(xlUp).Row
For i = lastrow To 1 Step -1
If Cells(i, 4).Value < Date Then Rows(i).EntireRow.Delete
Next i
End Sub
注意 我不喜欢在某个时间使用 windows 调度程序来运行宏。
到目前为止,我的良好参考是Is it possible to automatically input VBA code when a new sheet is generated? & Dynamically insert macro into a new excel workbook & https://support.microsoft.com/en-us/kb/219905
我会尽力响应!请发表评论以进行澄清。谢谢!
【问题讨论】:
-
(a) 您不能在 CSV 文件中包含 VBA - CSV 文件是一个简单的文本文件。 (b) 与其通过宏删除行,不如不费心将这些记录写入原始 CSV 文件? (c) 我刚刚注意到您的文件不是 CSV 文件,而是带有 CSV 扩展名的管道分隔文件。
-
@YowE3K 感谢您的评论!由于是连接Access的SQL服务器,所以无法编辑。
-
所以你不能编辑“Step 1”代码?不是 Access VBA?
-
@YowE3K 跟进 (a),我相信我可以在 csv 文件上运行 vba 代码。因为我已经通过 vba 代码删除了不必要的行。
-
@YowE3K 也许我可以编写代码只导出我想导出的那些行?
标签: vba excel macros export-to-csv