dynamic添加和运行一个VBA代码

以下是我目前的步骤

  1. Access文件通过macros导出CSV文件(VBA代码)

  2. 导出的CSV文件被macros(VBA代码)修改

现在,我在Access上执行macros(步骤1) – >在导出的文件下,添加代码并运行(步骤2)

我想简化这个过程。

是否有可能通过执行第1步,第2步代码被添加到csv文件并运行?

第1步代码

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 

第2步代码

 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调度程序在特定时间运行macros。

我的好参考是迄今为止是否有可能自动inputVBA代码时,生成一个新的表单? & dynamic插入macros到一个新的Excel工作簿 & https://support.microsoft.com/en-us/kb/219905

我会尽我所能来响应! 请留下澄清的意见。 谢谢!

您可以简单地将您现有的Step 1代码更改为:

 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 'Check the 4th field to see whether it is today or later If CDate(rst(3)) >= Date Then 'If so, create a record (if not, don't) 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 End If rst.MoveNext strHold = vbNullString Loop Close intFile rst.Close Set rst = Nothing End Function