Loop sheets, columns, rows and copy value from sheet to correct cell

Dalvin

New Member
Joined
Jul 3, 2019
Messages
5
Hi,

I have written a script that, is intended to, for each row in a range find a specific sheet, then in that sheet find the correct column, then the row that matches two criteria from the source sheet row. The inner code is supposed to copy a value from the row in the source sheet and paste it in the correct sheet-column-row.

Code:
Dim srcrow As Long 'Variable for row loop in source sheet
Dim sht As Long 'Variable for sheet loop
Dim col As Long 'Variable for column loop in target sheet
Dim row As Long 'Variable for row loop in target sheet
Dim LastCol As Long
Dim LastRow As Long
LastRow = Sheets("qPCR temp").Cells(Sheets("qPCR temp").Rows.Count, 1).End(xlUp).Column
For srcrow = 2 To LastRow
For sht = 7 To Sheets.Count
    If sht = Sample Then
    LastCol = Sheets(sht).Cells(15, Sheets(sht).Columns.Count).End(xlToLeft).Column
        For col = 1 To LastCol
            If Sheets(sht).Cells(15, col) = test Then
                For row = 19 To 386
                    If Sheets(sht).Cells(row, 9).Value = Sheets("qPCR temp").Cells(srcrow, 1) And Sheets(sht).Cells(row, 10).Value = Sheets("qPCR temp").Cells(srcrow, 2) Then
                    Sheets(sht).Cells(srcrow, 3).Copy Sheets(sht).Cells(row, col)
                    End If
                Next row
            End If
        Next col
    End If
Next sht
Next srcrow

The "sample" and "test" string has been declared and assigned values earlier in the sub.

My problem is that the code runs without any error messages but no values are copied anywhere.
I'm new here so I don't know the convention about asking for help with code, but does anyone see any obvious mistakes as to why the code isn't returning what I want it to?

Thanks for the help!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi & welcome to MrExcel
How about changing
Code:
If sht = Sample Then
to
Code:
If sheets(sht).Name = Sample Then
 
Upvote 0
Are you trying to copy from the "qPCR temp" sheet?
 
Upvote 0
In taht case this
Code:
Sheets(sht).Cells(srcrow, 3).Copy Sheets(sht).Cells(row, col)
should be
Code:
Sheets("qPCR temp").Cells(srcrow, 3).Copy Sheets(sht).Cells(row, col)
 
Upvote 0
More specifically, the "qPCR temp" sheet contains 352 rows that each have a value that should be copied to a row in the same variable sheet and the same variable column. So neither the sheet nor column will differ in any of the 352 copied values, only the row.
 
Upvote 0
With all those nested loops, it's impossible to figure out what's happening.
A few questions.
1) Does "Sample" change?
2) Does "Test" change?
3) Will "Test" only appear once in row 15?
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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