在sharedrive上打开最新文件并复制/粘贴到工作簿中

时间:2016-04-27 23:18:50

标签: excel vba excel-vba

我已经能够获得一些代码来打开位于共享驱动器上的最新文件。我真正努力的代码部分是最后一部分,它试图将该文件的内容复制并粘贴到我的主人身上 - 我试着录制最后一部分并改变代码但没有运气 - 我觉得喜欢我在正确的轨道上,但会欣赏任何指针!特定错误是"运行时错误' 1004'"

<label  class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="switch-1">
    <input type="checkbox" id="switch-1" class="mdl-switch__input" checked />
    <span class="mdl-switch__label">USEREMAIL Subscribed</span>
</label>

2 个答案:

答案 0 :(得分:0)

假设RMBS ...是主文件:

Workbooks.Open(latestFile)
Worksheets("Ratesheet").Activate
Range("A7").Select
Selection.copy
' ActiveWorkbook.Close if needed

Workbooks.Open("RMBS Pricing_New v5 (version 1) [Autosaved]")
Range("A7").Select
Selection.Paste
' Save/close as needed

答案 1 :(得分:0)

这是实现您想要的最简单方法。避免使用.Select/.Activate。您可能希望看到THIS

我的假设

  1. 您尝试打开的文件可以在Excel中打开
  2. 您正在运行RMBS Pricing_New v5 (version 1)的代码。如果没有,则声明另一个工作簿对象并将此工作簿分配给该对象。
  3. 这是你在尝试什么? (的未测试

    Sub GetLatestFile()
        Dim strFolder As String, strFile As String, latestFile As String
        Dim dtLast As Date
    
        Dim wbThis As Workbook, wbThat As Workbook
        Dim wsThis As Worksheet, wsThat As Worksheet
    
        Set wbThis = ThisWorkbook
        '<~~ Change this to the relevant sheetname where you want to paste
        Set wsThis = wbThis.Sheets("Sheet1")
    
        '
        '~~> Your code to find the latest file
        '
    
        Set wbThat = Workbooks.Open(latestFile)
        Set wsThat = wbThat.Sheets("Ratesheet")
    
        wsThat.Range(A7).Copy wsThis.Range("A7")
    End Sub