"New VBA user, Excel 2010, and I have several cost estimate workbooks within same folder. Within a separate summary workbook I want to loop through all workbooks, then loop through only the first and second worksheets, then copy and final paste values of specific cells.
I have pieced together certain snippets from several sources in the below. Currently only the first "If" loop for worksheet "Distro Sheet" seems to be grabbing data. The second "If" loop for "Execution Estimate" never seems to paste any cells? I have tried flagging the first two worksheets, used an array, and used a "Case" statement. None of these methods worked. Any ideas would be greatly appreciated!"
Sub GatherData()
Dim wkbkorigin As Workbook
Dim originsheet As Worksheet
Dim destsheet As Worksheet
Dim ResultRow As Long
Dim Fname As String
Dim RngDest As Range
Dim ws As Worksheet
Set destsheet = ThisWorkbook.Worksheets("Project Cost Tracker")
Set RngDest = destsheet.Cells(Rows.Count, 1).End(xlUp).Offset(1,0).EntireRow
Fname = Dir(ThisWorkbook.Path & "/*.xlsx")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While Fname <> "" And Fname <> ThisWorkbook.Name
Set wkbkorigin = Workbooks.Open(ThisWorkbook.Path & "/" & Fname)
For Each ws In wkbkorigin.Worksheets
If ws.Name = "Distro Sheet" Then
RngDest.Cells(6, 1).Value = ws.Range("C8").Value
RngDest.Cells(6, 5).Value = ws.Range("H8").Value
RngDest.Cells(5, 2).Value = ws.Range("C10").Value
RngDest.Cells(7, 1).Value = ws.Range("C15").Value
RngDest.Cells(8, 1).Value = ws.Range("C16").Value
RngDest.Cells(9, 1).Value = ws.Range("C17").Value
RngDest.Cells(10, 1).Value = ws.Range("C18").Value
RngDest.Cells(11, 1).Value = ws.Range("C19").Value
RngDest.Cells(7, 5).Value = ws.Range("D20").Value
RngDest.Cells(8, 5).Value = ws.Range("D21").Value
RngDest.Cells(9, 5).Value = ws.Range("D22").Value
RngDest.Cells(10, 5).Value = ws.Range("D23").Value
RngDest.Cells(11, 5).Value = ws.Range("D24").Value
End If
If ws.Name = "Execution Estimate" Then
RngDest.Cells(8, 10).Value = ws.Range("J99").Value
RngDest.Cells(9, 10).Value = ws.Range("J157").Value
RngDest.Cells(10, 10).Value = ws.Range("J186").Value
End If
Set RngDest = RngDest.Offset(1, 0)
Next ws
wkbkorigin.Close SaveChanges:=False
Fname = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
ws.Name
when you get to the second sheet? Is it"Execution Estimate"
(exact case, no extra spaces, exact spelling)? (i.e. when you step through the code, does it actually step into the secondIf
statement?) – YowE3K