代码之家  ›  专栏  ›  技术社区  ›  anky

从表1中的按钮调用宏,并在表2中写入一些数据

  •  0
  • anky  · 技术社区  · 6 年前

    我试图通过使用工作表中的按钮来调用下面的代码 Homepage 希望它把数据写进表格里 Logs

    下面是我的代码:

    Sub MyRenamePDF()
    
    Dim MyFolder As String
    Dim MyFile As String
    Dim i As Long
    Dim MyOldFile As String
    Dim MyNewFile As String
    Dim dt As String
    Dim FSO As Object
    Dim rng As Range
    Dim input_file As String
    Dim output_file As String
    dt = Format(Now(), "YYYY_MM_DD_HH_MM")
    Set FSO = CreateObject("Scripting.Filesystemobject")
    MyFolder = "D:\test\"
    TargetFolder = "D:\output\"
    MyFile = Dir(MyFolder & "\*.pdf")
    
     Do While MyFile <> ""
    
        MyOldFile = MyFolder & "\" & MyFile
        MyNewFile = MyFolder & "\" & "0001" & "_" & dt & "_" & MyFile
        Name MyOldFile As MyNewFile
        output_file = FSO.GetFileName(MyNewFile)
        With ThisWorkbook.Worksheets("Logs")
            Cells(.Rows.Count, "B").End(xlUp).Offset(1).Value = output_file
            Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = MyFile
        End With
        FSO.MoveFile MyNewFile, TargetFolder
        MyFile = Dir
     Loop
    End Sub
    

    2 回复  |  直到 6 年前
        1
  •  2
  •   BigBen    6 年前

    添加句点 . Cells(.Rows.Count, "B")... Cells(.Rows.Count, "A")... .

    按原样, ActiveSheet With...End With 阻止。

    看到了吗 Using With Statements 更多细节。

        2
  •  1
  •   Ryan R    6 年前

    我认为您只需要在“With”语句的单元格前面添加“.”:

    With ThisWorkbook.Worksheets("Logs")
        .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Value = output_file
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = MyFile
    End With