For Loop with If Statement Assistance

smick

New Member
Joined
Sep 19, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am looking for some assistance. I have a code which takes and copies a portion of an active workbook, opens another workbook, pastes the info in the last row, closes it, and then does the same thing for another workbook. Except on the second one it is looking for a value that aligns with another cell on the main sheet and pastes the data into a separate column in that row. I have been trying multiple ways to make the for work to find the appropriate row that has the value and paste but I am continually getting different results than what I am looking to accomplish. Any assistance would be appreciated. For reference the first part works fine, its just the bottom for section I cannot get to work. You can see two versions where I am trying different ways that I can see online.

As a reference: The cell with the required match value is D5 on the main sheet (Summary) which is set to LA
The column with the lookup information is D on the Sample sheet
And I am looking to then paste the information into M through AI.
Main copy sheet = Summary
Sheet going to that is issue is Sample

Any help that can be provided would be very very appreciated. (And pardon the random things in there from me trying different ways to work and I did replace file paths with Xs due to information restrictions)

VBA Code:
Sub copytoSummary()
    Dim destSht As Worksheet
    Dim RNG As Range
    Dim Text As Range
    Dim LA As String
    Dim C As Integer
    Dim Cell As Range
    
    
   Sheets("Summary").Range("B5:AL5").Copy
           
    Workbooks.Open ("S:\x1") '<- at opening a workbook it becomes the active one
    Set destSht = ActiveWorkbook.Worksheets("QA Sample") '<-- set the destination worksheet in the activeworkbook
    
    With Workbooks("x1 ' <-- here should be the Sheet's name
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row ' last row in column B
        .Range("B" & LastRow + 1).PasteSpecial xlValues
        
                End With
   
    destSht.Parent.Close True '<--| close the destination workbook, which is obtained as the Parent object of the destination worksheet

Sheets("Summary").Range("M5:AI5").Copy
           
LA = Range("D5")
    Workbooks.Open ("x2") '<- at opening a workbook it becomes the active one
    Set destSht = ActiveWorkbook.Worksheets("Sample") '<-- set the destination worksheet in the activeworkbook
    
    With Workbooks("x3) ' <-- here should be the Sheet's name
        
        For Each Cell In Range("D1:D80")
            If Cell.Value = LA Then
            LastRow = Cell.Rows.Count
            .Range("M" & LastRow).PasteSpecial xlValues
            Else
            End If
        Next
    End With

       ' For Each C In Worksheets("Sample").Range("D1:D75").Cells
          '  If Cells(C, "D").Value = LA Then
          '  Cells(C, "M").PasteSpecial xlValues
          '  End If
       ' Next C
                'End With
   
    destSht.Parent.Close True

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,215,078
Messages
6,122,996
Members
449,093
Latest member
masterms

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