循环文件夹并检查Excel文件名是否以工作表名结尾,然后复制工作表

循环文件夹并检查Excel文件名是否以工作表名结尾,然后复制工作表,excel,vba,Excel,Vba,我需要一些帮助和想法 我有一个文件夹,比如说这个C:\Users\Me\Desktop\Test Dir\Files\ 在这个文件夹中,我有几本工作簿和名为Test.xlsm 在我的主工作簿中,我有几个不同名称的工作表AB11、AB12、BC13等 我想构建一个代码,将工作表AB1复制并移动到文件夹中的excel文件中,该文件夹的名称以AB1结尾(或者可能包含该单词) 然后循环将继续。如果下一个Excel文件以AB2结尾,则从主工作簿复制并移动工作表AB2 有什么想法吗?到目前为止,我得到的是:

我需要一些帮助和想法

我有一个文件夹,比如说这个
C:\Users\Me\Desktop\Test Dir\Files\
在这个文件夹中,我有几本工作簿和名为
Test.xlsm

在我的主工作簿中,我有几个不同名称的工作表AB11、AB12、BC13等

我想构建一个代码,将工作表AB1复制并移动到文件夹中的excel文件中,该文件夹的名称以AB1结尾(或者可能包含该单词)

然后循环将继续。如果下一个Excel文件以AB2结尾,则从主工作簿复制并移动工作表AB2

有什么想法吗?到目前为止,我得到的是:

但是,找不到我的文件,即以“AB11”结尾的文件


我不喜欢人们更倾向于用“更加努力”的信息来回应,而不是从实际解决了这些问题的人那里得到一些实际的建议。下面是一些在两个工作簿之间移动工作表的有用代码。我解释了每一行的作用

Dim SourceWorkbook As Workbook, CurrentWorkbook As Workbook 'sets up objects references for the workbook files.

Set CurrentWorkbook = ThisWorkbook 'ThisWorkbook is a keyword that references the book this code is running on
Set SourceWorkbook = Workbooks.Open("D:\work files\Warehouse inventory system\Input file.xlsx") 'this will open the file and assign the reference to it to SourceWorkbook

SourceWorkbook.Sheets("Sheet1").Copy After:=CurrentWorkbook.Sheets("Main") 'this code section opens the file, imports the sheet
SourceWorkbook.Close 'Closes the second workbook, recommended to free up resources

Sheets("Sheet1").Activate 'this makes the newly pasted sheet the active sheet
Sheets("Sheet1").Cells.Select 'this will select all cells on that sheet, it is not nessacary to activate the sheet to select the cells
Selection.Copy 'copys the selection to the clipboard
Sheets("Main").Select 'selects the sheet named Main
Cells(1, 1).Select 'selects the first cell in the sheet
ActiveSheet.Paste 'pastes in the data from the other sheet

Application.DisplayAlerts = False 'turns off alert messages before deleting to remove the "are you sure" message
Sheets("Sheet1").Delete 'deletes the sheet named "Sheet1"
Application.DisplayAlerts = True 'turns on the alert messages after the delete
Sheets.Add(After:=Sheets("Main")).name = "Barcode" 'adds a new sheet and names it Barcode
Sheets("Main").Select 'selects the Main sheet

当然,您必须找出如何修改此代码以使其适合您的情况。但它应该足以让你开始。

我的想法是:将你的项目分成更小的部分。对每个部分都做些研究。尝试一些东西,然后带着您的尝试(您的代码)返回,并向其提出问题,包括告诉出了什么问题或您遇到了什么问题:请参阅和。尝试搜索如何使用Dir以及如何将数据从一个工作簿传输到另一个工作簿,这两个工作簿都有许多重复目标。不要使用
。选择
。你可能会从阅读中受益。我们没有要求“更努力”我们只是要求“努力”否则这只是要求其他人做这项工作。@Scott Cannon:原因主要是因为SO是用来寻找问题答案的资源,而不是一次又一次地问同一个问题。看见但如今,与其他论坛一样,有人问代码,有人回答得或多或少好,没有人会用给出的答案来解决类似的问题。请摆脱所有
激活
选择
-语句。并限定对象,如
图纸
单元格
。永远不要依赖于
Activesheet
@ScottCannon问题是这不是,也从来没有打算成为一个教程网站。如果你需要学习编程,有很多资源。如果你想得到一般帮助,reddit可能是你最好的选择。如果你需要一些完整的、快速的、符合你具体要求的东西,那么会有很多人愿意为此付钱。具体的编程相关问题也是如此。它给人的感觉是严酷的,但不是注定的,有些东西就是不适合这样。(在OP编辑之后,它肯定处于更好的状态。)教程和代码调试之间的区别在于语义。你还在解释如何完成一项任务,你的问题是你不喜欢这个人,因为在你看来,这个人的知识水平太低,问的问题太广泛。它的核心是一个完全主观的论点。有人问别人如何解决这个问题并没有什么错,有时候你所坚持的代码是在找出可用工具的限制,有时候是在找出可用的工具。谷歌不能只提供洞察参考。
Dim SourceWorkbook As Workbook, CurrentWorkbook As Workbook 'sets up objects references for the workbook files.

Set CurrentWorkbook = ThisWorkbook 'ThisWorkbook is a keyword that references the book this code is running on
Set SourceWorkbook = Workbooks.Open("D:\work files\Warehouse inventory system\Input file.xlsx") 'this will open the file and assign the reference to it to SourceWorkbook

SourceWorkbook.Sheets("Sheet1").Copy After:=CurrentWorkbook.Sheets("Main") 'this code section opens the file, imports the sheet
SourceWorkbook.Close 'Closes the second workbook, recommended to free up resources

Sheets("Sheet1").Activate 'this makes the newly pasted sheet the active sheet
Sheets("Sheet1").Cells.Select 'this will select all cells on that sheet, it is not nessacary to activate the sheet to select the cells
Selection.Copy 'copys the selection to the clipboard
Sheets("Main").Select 'selects the sheet named Main
Cells(1, 1).Select 'selects the first cell in the sheet
ActiveSheet.Paste 'pastes in the data from the other sheet

Application.DisplayAlerts = False 'turns off alert messages before deleting to remove the "are you sure" message
Sheets("Sheet1").Delete 'deletes the sheet named "Sheet1"
Application.DisplayAlerts = True 'turns on the alert messages after the delete
Sheets.Add(After:=Sheets("Main")).name = "Barcode" 'adds a new sheet and names it Barcode
Sheets("Main").Select 'selects the Main sheet