VBA Code Overwriting

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
551
I have amended the code below and have got it working. The problem I have now is that every time it loops it overwrites the data it wrote the previous loop

Offending line being ActiveSheet. Range ("A1: D30") = ValuesArray

I have known that somehow it should remember the last row and copy below this one but I cannot get it to work

Any help would be appreciated


Code:
Sub Basic_Example_1()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String
    Dim SourceRcount As Long, Fnum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long


    'Fill in the path\folder where the files are
    MyPath = "C:\Combined test"


    'Add a slash at the end if the user forget it
    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If


    'If there are no Excel files in the folder exit the sub
    FilesInPath = Dir(MyPath & "*.xl*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If


    'Fill the array(myFiles)with the list of Excel files in the folder
    Fnum = 0
    Do While FilesInPath <> ""
        Fnum = Fnum + 1
        ReDim Preserve MyFiles(1 To Fnum)
        MyFiles(Fnum) = FilesInPath
        FilesInPath = Dir()
    Loop


     'Change ScreenUpdating, Calculation and EnableEvents
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With


    'Add a new workbook with one sheet
    Set desout = Workbooks.Add
    With desout
        .Title = "Compliance Test"
        .Subject = "Compliance Test"
    End With


    desout.Worksheets.Add().Name = "dcp1"
   


    'Loop through all files in the array(myFiles)
    If Fnum > 0 Then
        For Fnum = LBound(MyFiles) To UBound(MyFiles)
            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
            On Error GoTo 0


            If Not mybook Is Nothing Then


                On Error Resume Next
'Tims Code


Dim NoOfSheets As Integer
NoOfSheets = mybook.Sheets.Count
Dim ValuesArray()
ReDim ValuesArray(NoOfSheets, 3)


For i = 1 To NoOfSheets
mybook.Sheets(i).Activate


With mybook.Sheets(i)


    
    
    'get job number
    rawJobNumber = .Range("A1").Value
    rawJobNumberHyphen = InStr(1, rawJobNumber, "-") + 1
    rawJobNumberSpace = InStr(1, rawJobNumber, " ")
    JobNumber = Mid(rawJobNumber, rawJobNumberHyphen, rawJobNumberSpace - rawJobNumberHyphen)
    ValuesArray(i - 1, 0) = JobNumber
    
    'get date
    ValuesArray(i - 1, 1) = .Range("B3").Value
    
    'get actual
    ValuesArray(i - 1, 2) = .Range("B4").Value
    
    'get count of screens out
    TempValue = .Range("A18:A10000").Cells.SpecialCells(xlCellTypeConstants).Count
    
    If TempValue = 1 Then
        If .Range("B18").Value = "" Then
          ValuesArray(i - 1, 3) = 0
        Else
          ValuesArray(i - 1, 3) = 1
        End If
    Else
        ValuesArray(i - 1, 3) = .Range("A18:A10000").Cells.SpecialCells(xlCellTypeConstants).Count
    End If
     
End With
Next i


          desout.Sheets("dcp1").Activate
          
          ActiveSheet.Range("A1:D30") = ValuesArray
         End If
             
                mybook.Close savechanges:=False
           


        Next Fnum
        BaseWks.Columns.AutoFit
    End If


ExitTheSub:
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

L. Howard

Well-known Member
Joined
Oct 16, 2012
Messages
4,514
Maybe this will work. Where the next empty row in column A will be the top-left cell to place the four column ValuesArray.

ValuesArray = Cells(Rows.Count, "A").End(xlUp) (2).Row

or if you want a space then the ValuesArray...

ValuesArray = Cells(Rows.Count, "A").End(xlUp) (3).Row </SPAN></SPAN>


Regards,
Howard
</SPAN></SPAN>
 

Watch MrExcel Video

Forum statistics

Threads
1,123,396
Messages
5,601,420
Members
414,449
Latest member
Pashtun

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
Top