【问题标题】:Dynamically add and run a vba code动态添加并运行 vba 代码
【发布时间】:2017-05-19 04:49:03
【问题描述】:

以下是我目前的步骤

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

  2. 导出的 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


【解决方案1】:

您可以简单地将现有的第 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

【讨论】:

  • 所以你已经编辑到 CDate(rst(3)) >= Date!我看到你在那里做什么!谢谢会让你知道它是如何工作的!
  • 非常感谢!你是个天才! :) 你找到了一个更简单的方法来解决它!再次感谢您
  • 只需检查下游进程是否期望逗号或管道作为分隔符。我的猜测是,您的旧方法是将文件从 excel 中保存为真正的逗号分隔值文件,而 Access 将其保存为管道分隔。 (您可能需要在我发布的代码中将"|" 更改为","。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多