简体   繁体   中英

Excecute loop within a 'If…Then' statement VBA

I am adding cells from multiple workbooks into a single workbook by specifying the cell in a formula and adding them to a designated cell/column in my new workbook. I would also like to include a for loop within my 'if.. then..With' statement. I would like my 'for' loop to add the the specified range A2:C57 from each of the sheets 3,4,5,6,7,8,9 into the cells G,H,I and so on. How can I add this loop in my current VBA code?

Sub GetData()
Dim MyPath As String
Dim FileName As String
Dim SheetName As String
Dim n As Long
Dim NewRow As Long

MyPath = "C:\attach"
SheetName = "Title"


FileName = Dir(MyPath & "\*.xlsx")
Do While FileName <> ""
 If FileName <> ThisWorkbook.Name Then
   With ThisWorkbook.Sheets("Sheet1")
   NewRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1

  'Transferring cells from worksheet(2)"Title" into "Sheet1" in thisworkbook"
  With .Range("A" & NewRow)
    .Formula = "='" & MyPath & "\[" & FileName & "]" & SheetName & "'!B4"
    .Value = .Value
  End With
  With .Range("B" & NewRow)
    .Formula = "='" & MyPath & "\[" & FileName & "]" & SheetName & "'!B5"
    .Value = .Value
  End With
  With .Range("C" & NewRow)
    .Formula = "='" & MyPath & "\[" & FileName & "]" & SheetName & "'!B6"
    .Value = .Value
  End With
  With .Range("D" & NewRow)
    .Formula = "='" & MyPath & "\[" & FileName & "]" & SheetName & "'!B7"
    .Value = .Value
  End With
  With .Range("E" & NewRow)
    .Formula = "='" & MyPath & "\[" & FileName & "]" & SheetName & "'!A1"
    .Value = .Value
  End With
  With .Range("F" & NewRow)
    .Formula = "='" & MyPath & "\[" & FileName & "]" & SheetName & "'!A2"
    .Value = .Value
  End With
  'Selects the range A2:C57 from sheets 3,4,5,6,7,8,9
  With .Range("G" & NewRow)
   Dim i As Integer, j As Integer, k As Integer
    k = 1   'row counter for destination sheet
    'loop sheets 3-9
     For i = 3 To 9
         'loop rows 2-57
          For j = 2 To 57
            'if C is not empty
                If WrkBookSrs.Sheets(i).Cells(j, 3).Value <> "" Then
                 'code here to add A:C on this row into this workbook in sheet1 column G.

                         k = k + 1'increment counter for next row
    End If
Next

Your explanation and pseudo code are not very clear. Are you wanting to add the sums of all the values from the cells (A:C) in your loop? Your comments mention just columns A:C being used

So if I understand correctly you should be able to set a variable to the sum of the values

With WrkBookSrs.Sheets(i)
   newGvalue= .Cells(j, 1).Value + .Cells(j, 2).Value + .Cells(j, 3).Value
End With

Add a for loop like this.

For q = 3 To 9
    With ThisWorkbook.Sheets("Sheet" & q)

If you are only wanting to include the same sheets over and over, this should work. Of course adjust the range of q to meet your specs

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