auto populate sheets

scorpweb

Board Regular
Joined
Jul 26, 2011
Messages
124
hi all,

my mate made some code for me a few months ago to auto populate data from 1 spreadsheet into a "log" so i can maintain all my data ....

however i need to expand this to about 7 spreadsheets now rather than 2 which he had set up

what i have so far:

Code:
Private Sub CommandButton1_Click()
    Dim ObjFolder
    Dim ObjFileCollection
    Dim ObjSubFolderCollection
    Dim ObjSubFolder
    Dim ObjFile
    Dim objFSO
    Dim xlapp
    Dim mwb As Workbook
    Dim cwb As Workbook
    Dim strsubfolderpath As String
    Dim rightcheck As Boolean
    Dim rightcheckpos As Integer
    strsubfolderpath = "C:\Users\MrLee\Documents\new pc\files"
   Set objFSO = CreateObject("Scripting.FileSystemObject")

   'Getting the folder
   Set ObjFolder = objFSO.GetFolder(strsubfolderpath)
   
   'Getting the list of files
   Set ObjFileCollection = ObjFolder.Files
   Set xlapp = Excel.Application
   Set mwb = xlapp.Workbooks.Open("C:\Users\MrLee\Documents\new pc\master\Master.xls")
   
   mwbpos1 = 1
   mwbpos2 = 2
   rightcheckpos = 1
   
   Do Until mwb.Worksheets(2).Cells(mwbpos1, 1).Value = ""
   mwbpos1 = mwbpos1 + 1
   Loop
   
   Do Until mwb.Worksheets(1).Cells(mwbpos2, 1).Value = ""
   mwbpos2 = mwbpos2 + 1
   Loop
   
   'Printing each file
   For Each ObjFile In ObjFileCollection
    rightcheck = True
   
    If LCase(Right(ObjFile.Name, 4)) = ".xls" Then
       Set cwb = xlapp.Workbooks.Open(ObjFile.Path)
       j = 3
       p = 3
       
restartloop:
       Do Until cwb.Sheets(2).Cells(j, 1).Value = ""
            
            For k = 1 To 6
            If cwb.Sheets(2).Cells(j, k).Value = 0 Then
                cwb.Worksheets(2).Cells(j, 12).Value = "Error - Cell " & k & " is empty"
                j = j + 1
                rightcheck = False
                GoTo restartloop
            End If
            Next k
            
            mwb.Worksheets(2).Rows(mwbpos1).Value = cwb.Worksheets(2).Rows(j).Value
            cwb.Worksheets(2).Rows(j).Delete
            mwbpos1 = mwbpos1 + 1
            
            
         
       Loop

restartloop2:

       Do Until cwb.Sheets(1).Cells(p, 1).Value = ""
            For q = 1 To 5
            If cwb.Sheets(1).Cells(p, 15).Value = "" Then
                cwb.Worksheets(1).Cells(p, 21).Value = "Error - Cell 15 is empty"
                p = p + 1
                rightcheck = False
                GoTo restartloop
            End If
            Next q
                     
            mwb.Worksheets(1).Rows(mwbpos2).Value = cwb.Worksheets(1).Rows(p).Value
            cwb.Worksheets(1).Rows(p).Delete
            mwbpos2 = mwbpos2 + 1

       Loop
    Application.DisplayAlerts = False
    If rightcheck = False Then
    Me.Cells(1, rightcheckpos).Value = ObjFile.Name
    rightcheckpos = rightcheckpos + 1
    End If
    cwb.Save
    cwb.Close
    Application.DisplayAlerts = True
      End If

    
    Next
     
    Application.DisplayAlerts = False
    mwb.Save
    mwb.Close
    Application.DisplayAlerts = True
    MsgBox "All Done!"
End Sub


i thought i'd have been able simply add

Code:
restartloop3:

       Do Until cwb.Sheets(3).Cells(p, 1).Value = ""
            For q = 1 To 5
            If cwb.Sheets(3).Cells(p, 15).Value = "" Then
                cwb.Worksheets(3).Cells(p, 21).Value = "Error - Cell 15 is empty"
                p = p + 1
                rightcheck = False
                GoTo restartloop
            End If
            Next q
                     
            mwb.Worksheets(3).Rows(mwbpos2).Value = cwb.Worksheets(3).Rows(p).Value
            cwb.Worksheets(3).Rows(p).Delete
            mwbpos2 = mwbpos2 + 1

       Loop

but it doesn't seem to work and debugs on:

Code:
        Do Until cwb.Sheets(3).Cells(p, 1).Value = ""

anyone able to shed any light on how i can expand this to more than the 2 sheets please?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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