简体   繁体   中英

Loop folder and check if Excel file name ends with worksheet name, then copy worksheet

I am in need of some help and ideas.

I have a folder, let's say this one C:\Users\ Me \Desktop\Test Dir\Files\ In this folder I have several workbooks and my Master Workbook named Test.xlsm

In my master Workbook I have several sheets with different names AB11, AB12, BC13 etc

I want to build a code that will copy and move the sheet AB1 to a excel file in the folder whose name ends with AB1 (or maybe contains this word).

And then the loop will continue. If the next Excel file ends with AB2, then copy and move the sheet AB2 from my Master Workbook.

Any ideas? What I have so far is:

However, my file cannot be found, the one that ends with the name "AB11"


Dim myPath As String
Dim myFileAB11 As Workbook
Dim wb As Workbook

myPath = "C:\Users\Me\Desktop\Test\Split "

filenamefilters = Array("AB11.xlsx", "AB12.xlsx", "BC13.xlsx", "BC14.xlsx")
myFileAB11 = Dir(myPath & "*AB115*.xls", vbNormal)
myFileAB12 = Dir(myPath & "*AB12*.xls", vbNormal)
myFileBC13 = Dir(myPath & "*BC13*.xls", vbNormal)
myFileBC14 = Dir(myPath & "*BC14*.xls", vbNormal)

Workbooks.Open (Dir(myPath & "*GB55.xlsx", vbNormal))

Workbooks(myFileAB11).Activate
Sheets.Add
ActiveSheet.Name = "AB11"

Workbooks.Open Filename:="C:\Users\Me\Desktop\Test\Test.xlsm"
Sheets(AB11).Copy Before = Workbooks(myFileAB11).Sheets(Sheets.Count)
ActiveSheet.Name = "AB11"



End Sub

I don't like that people are more likely to respond with a "try harder" message than some actual advice from people who have actually solved these problems. Here is some useful code for moving sheets between two workbooks. I explained what each line does.

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

you will of course have to figure out how to modify this code to make it work for your situation. but it should be more then enough to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM