2016-02-05 195 views
0

我正在尝试编写一个代码,该代码使用从我的输入工作簿中获取数据的按钮单击,然后将其粘贴到我的输出工作簿中。我无法弄清楚的问题是,我的输入工作簿名称按日期更改,但它们都处于相同的命名约定。我想要代码拉取最新的日期输入文件,然后将其粘贴到输出工作簿中。我不知道从哪里开始..任何帮助都很感激。将数据从一个excel工作簿复制到另一个

谢谢

+0

我不知道你在问什么。你需要[编辑]使其更清楚。在这样做的时候,请记住,除了您在问题中告诉我们的信息外,我们还有关于您正在尝试做什么的零信息;我们无法弄清楚你想告诉我们什么。请清楚说明问题并**提出具体问题**。请参阅[我如何在这里提出一个好问题?](http://stackoverflow.com/help/how-to-ask)了解一些提示。 –

+0

好吧,我很抱歉我会尝试修复它 –

回答

1

我同意肯:你问一个单一的问题太多。另外,“从我的输入工作簿获取数据,然后将其粘贴到我的输出工作簿”可能意味着什么。但是,这是我用来解决您的第一部分需求的功能。这些参数是文件夹名称和文件模板。它返回与模板匹配的文件夹中最新文件的名称。

Function NewestFileName(ByVal Path As String, ByVal FileTemplate As String) As String 

    ' * Finds, and returns the name of, the newest file in folder Path with a name 
    ' that matches FileTemplate. Returns "" if no matching file is found. 

    ' * Path   Folder in which to search for files 
    ' * FileTemplate File name specification of the file required. For example: 
    '      MyFile*.xls 

    ' 25Jul11 Copied from RiskRegisterControl V43.xls. 
    ' 22Nov11 Name changed from NewestFile to NewestFileName to match NextFileName. 
    ' 20Apr12 Minor improvements 

    Dim FileDateCrnt    As Date 
    Dim FileDateNewest   As Date 
    Dim FileNameCrnt    As String 
    Dim FileNameNewest   As String 

    If Right(Path, 1) <> "\" Then 
    Path = Path & "\" 
    End If 

    FileNameCrnt = Dir$(Path & FileTemplate) 
    If FileNameCrnt = "" Then 
    NewestFileName = "" 
    Exit Function 
    End If 

    FileNameNewest = FileNameCrnt 
    FileDateNewest = FileDateTime(Path & FileNameCrnt) 
    Do While True 
    FileNameCrnt = Dir$ 
    If FileNameCrnt = "" Then Exit Do 
    FileDateCrnt = FileDateTime(Path & FileNameCrnt) 
    If FileDateCrnt > FileDateNewest Then 
     FileNameNewest = FileNameCrnt 
     FileDateNewest = FileDateCrnt 
    End If 
    Loop 

    NewestFileName = FileNameNewest 

End Function 
+0

我认为这就是我所追求的......所有我想要的是,当我在我的输出excel工作表上按下按钮时,它会从最新输入中复制所有内容并将其粘贴在输出表中 –

+1

这是一个程序员互相帮助发展其技能的网站。没有人会为你编写你的整个宏。 (1)找到最新的工作簿,(2)打开工作簿作为'Src',(3)将选定的工作表从'Src'复制到'ThisWorkbook'(4)关闭'Src'而不保存更改(5) )将宏链接到按钮。我已经给你步骤(1)的代码。查找如何打开工作簿。你要模糊我确定我的步骤(3)是你想要的,以便澄清你的要求。如果我的步骤(3)是正确的,请查看复制工作表或使用宏记录器。 。 。 –

+1

查找步骤(4)和(5)。如果遇到困难,请一步一步求助。如果您按照我的建议分解宏,您可以自己创建宏。快乐编程。 –

相关问题