Excel masterfile vba

moshc

New Member
Joined
Oct 24, 2019
Messages
6
Good day!

Anyone can help me please?

I've been looking for an EXCEL VBA code in which i will have the option to select a folder path first which all the workbooks i need to combined is saved and have all the first sheets in all workbook in that specific folder is then combined to a new workbook.

Would really much appreciate any immediate response.

Thank you!
 
"MyFolder" has a 255 character limit. The "31" limit refers to the length of a sheet name.
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Got it with FN!!! Can we have the code paste at destination VALUES only, cells with formulas are challenging. I hope your weekend is not spoilt! Next week I will point you at some strange behaviour hapenning when it populates A2 and B2, occasionally A2 overshoots by a single row. Causing the output to have some some rows with information on Column A and no values on Column B onwards. See you on Tuesday! Happy Easter Monday!!!
 
Upvote 0
Please explain this problem in detail and post sample data that shows what this looks like.
I am using the code to merge data from 4 differently named Sheets to consolidate data into 4 different WBs one at a time. When I want to get data from "EntryList" I simply edit the code and type "Entrylist" in the five occurrences in the code, when I want "StockList" or "SeedPrep" or "Master", I do the same. All the sheets except "Master have data starting from row 25. Data starts at Row 2 for Sheet "Master" (you may want to put a remark for me in the code to guide me where to change this). It is only when I run the code for sheet "StockList" that this anomaly in Column A and B happens. I notice that in Sheet "StockList" A6 and A7 are not blank but does not form part of the data. Could this be the cause of this behavior?
Attached is a XL2bb mini sheets, one showing the anomaly and the other is showing Sheet "StockList". Please help.

KB19B-Stocklists.xlsx
ABCDE
1NurseryNameEntryStockIDNameBreedersPedigree1
2KB19B-0B011qqtrt4h5h25254456hwhdhwd
3KB19B-0B012564545h35hhhhhhh45hertqetqerrhrt66545645
4KB19B-0B013etrqert4525hhwerghwdghwrhgwhg5y245yy
5KB19B-0B011RETERTRETQTTTTTEQRTQERTQERTRTHWERQER
6KB19B-0B012ERQTQERT134616SDFHDHDFHH
7KB19B-0B023D1602-3431613HFGFGETETQWETWTWETETT
8KB19B-0B021GRQERQERTQG34TDSVQERG
9KB19B-0B022D1570-2ERT34B34154WEGEFASDG
10KB19B-0B033QEWT4TT34TTQEASGSAT34TDFGDSG
11KB19B-0B031ERTERTTERTERRETQETRRETQERT
12KB19B-0B032345tewtwetqwet4643rgadfg
13KB19B-0B273qewtqwetsgqegesdagadgdaSGH
14KB19B-0B27
15KB19B-0B27
KB19B-Stocklists


Sheet "StockList"

0B01.xls
ABCDKNZ
1EntryStockIDNameBreedersPedigree1OriginMemo1nEarsSelected
6(Use cell C6) Material: MLN-FXLINES20A
7(Use cell C7) Root Stock ID:D1568
21
22
23EntryStockNamePedigreeOriginMemoEars
24IDSelect
251qqtrt4h5h25254456hwhdhwdhsfhfhfh11
262564545h35hhhhhhh45hertqetqerrhrt66545645hsfhhsf10
273etrqert4525hhwerghwdghwrhgwhg5y245yyfdhdfhafh11
Stocklist
 
Upvote 0
It looks like the data in "StockList" starts at row 2 not row 25. If this is correct, then you will have the same problem as in the "Master" sheet where the data also starts in row 2.
 
Upvote 0
The data in "StockList" starts at Row 25. The information in A6 and A7 and C6 and C7 is not part of the data, it is for a totally different purpose. Please assist. In the case of "Master", all data will start at Row 2. Also, lets paste values and save in parent folder. Thanks, - Kasango.
 
Upvote 0
Could you attach a copy of StockList that has some data starting in row 25?
 
Upvote 0
It looks like the data in "StockList" starts at row 2 not row 25. If this is correct, then you will have the same problem as in the "Master" sheet where the data also starts in row 2.
It is the XL2BBminisheet above. Let me attach another two so that you have three XL2BBminisheets to test with.
0B02.xls
ABCDKNZ
1EntryStockIDNameBreedersPedigree1OriginMemo1nEarsSelected
6(Use cell C6) Material: FAW-LINES
7(Use cell C7) Root Stock ID:D1602
23EntryStockNamePedigreeOriginMemoEars
24IDSelect
251RETERTRETQTTTTTEQRTQERTQERTRTHWERQERDFNSFGN53
262ERQTQERT134616SDFHDHDFHHGNSF56
273D1602-3431613HFGFGETETQWETWTWETETTFSNSDFN38
Stocklist


0B03.xls
ABCDKNZ
1EntryStockIDNameBreedersPedigree1OriginMemo1nEarsSelected
6(Use cell C6) Material: S5-AFLLINES
7(Use cell C7) Root Stock ID:D1570
23EntryStockNamePedigreeOriginMemoEars
24IDSelect
251GRQERQERTQG34TDSVQERGQEWT34TGQEGQWE1
262D1570-2ERT34B34154WEGEFASDGTWEQWET435341
273QEWT4TT34TTQEASGSAT34TDFGDSGGQERT34T34535131
Stocklist
 
Upvote 0
Could you attach a copy of StockList that has some data starting in row 25?
I have done so. You now have three XL2BB minisheets to work with. 0B01, 0B02 and 0B03. Thanks- Kasango.
 
Upvote 0
This worked for me:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim fso, oFolder, oSubfolder, oFile, queue As Collection, MyFolder As String
    Dim wsDest As Worksheet, wkbSource As Workbook, lCol As Long, lRow As Long
    Dim splt As Variant, FN As String, wbName As String, x As Long: x = 1
    Set wsDest = ThisWorkbook.Sheets("Sheet1")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set queue = New Collection
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder"
        .Show
        .AllowMultiSelect = False
        If .SelectedItems.Count = 0 Then
            MsgBox "You did not select a folder."
            Exit Sub
        End If
        MyFolder = .SelectedItems(1) & "\"
    End With
    queue.Add fso.GetFolder(MyFolder)
    Do While queue.Count > 0
        Set oFolder = queue(1)
        queue.Remove 1
        For Each oSubfolder In oFolder.SubFolders
            queue.Add oSubfolder
        Next oSubfolder
        For Each oFile In oFolder.Files
            'If Right(oFile, 3) Like "xl*" Then
            If Right(oFile, 3) Like "ls*" Then
                Set wkbSource = Workbooks.Open(oFile)
                splt = Split(oFile, "\")
                FN = splt(UBound(splt) - 1)
                wbName = Split(wkbSource.Name, ".")(0)
                If Not IsError(Evaluate("=ISREF('[" & oFile.Name & "]" & "StockList" & "'!$A$1)")) Then
                    If Sheets("StockList").Range("A25") <> "" Then
                        With Sheets("StockList")
                            If x = 1 Then
                                lCol = .Cells(1, Columns.Count).End(xlToLeft).Column
                                wsDest.Range("A1") = "NurseryName"
                                .Range("A1").Resize(, lCol).Copy
                                wsDest.Range("B1").PasteSpecial xlPasteValues
                                x = x + 1
                                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                                .Range("A25").Resize(lRow - 24, lCol).Copy
                                wsDest.Range("B2").PasteSpecial xlPasteValues
                                wsDest.Range("A2").Resize(lRow - 24) = FN & "-" & wbName
                            Else
                                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                                lCol = .Cells(25, Columns.Count).End(xlToLeft).Column
                                .Range("A25").Resize(lRow - 24, lCol).Copy
                                wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                                wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Resize(lRow - 24) = FN & "-" & wbName
                            End If
                        End With
                    End If
                End If
                Application.DisplayAlerts = False
                wkbSource.Close False
                Application.DisplayAlerts = True
            End If
        Next oFile
    Loop
    '=================================
    wsDest.Name = "2019A" & "-StockList"
   ' ActiveWorkbook.SaveAs FileName:=MyFolder & FN & "-" & "EntryLists", FileFormat:=xlCSV, CreateBackup:=False
    ActiveWorkbook.SaveAs Filename:="2019A" & "-" & "EntryLists", FileFormat:=xlCSV, CreateBackup:=False
   
    Rows("1:1").Select
    Selection.AutoFilter
    Rows("2:2").Select
    ActiveWindow.FreezePanes = True
    Application.ScreenUpdating = True
    Application.ScreenUpdating = True
    '=================================
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top