Need Help changing VBA code so it "loops" through each row

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
I am very bad with "looping" no idea how to do it

I have the code below that works. Where I need help if from the section that says "Cost Source Details"

I need to take the data from the one sheet and move it to another for each row from row 24 down to 56

But each time it moves the data to the other sheet there are cells that will be the same for each row - columns A through E will be the same in each row.

Thank you for your help. Its very much appreciated.

Code:
Sub AddtoTemplateCS()

Dim TPLR As Long
Dim ToDate As String
Dim RDate As String
Dim SumExcess As Double
Dim SumNRE As Double
Dim SumTariff As Double

  TPLR = Sheets("Cost Sources").Cells(Rows.Count, "A").End(xlUp).Row + 1
  SumExcess = Application.WorksheetFunction.Sum(Sheet4.Range("I24:I56"))
  SumNRE = Application.WorksheetFunction.Sum(Sheet4.Range("J24:J56"))
  SumTariff = Application.WorksheetFunction.Sum(Sheet4.Range("K24:K56"))
  
  'Identify Current User
  CurrentUser = Environ("UserName")

  
  ToDate = Sheets("3 Enter Quote Data").Range("R12").Value
    RDate = Right(ToDate, 4)

'Material
Sheets("Cost Sources").Range("A" & TPLR).Value = Sheets("3 Enter Quote Data").Range("F18").Value
'Material Type
Sheets("Cost Sources").Range("B" & TPLR).Value = "Part"
'Type
Sheets("Cost Sources").Range("C" & TPLR).Value = Sheets("3 Enter Quote Data").Range("W3").Value
'PP ID
Sheets("Cost Sources").Range("D" & TPLR).Value = Sheets("3 Enter Quote Data").Range("L5").Value
'PP Revision
Sheets("Cost Sources").Range("E" & TPLR).Value = Sheets("3 Enter Quote Data").Range("P5").Value
'From date
Sheets("Cost Sources").Range("F" & TPLR).Value = Sheets("3 Enter Quote Data").Range("O12").Value
'To Date
Sheets("Cost Sources").Range("G" & TPLR).Value = Sheets("3 Enter Quote Data").Range("R12").Value
'Library
Sheets("Cost Sources").Range("H" & TPLR).Value = "(Common)"
'Vendor
Sheets("Cost Sources").Range("I" & TPLR).Value = Sheets("3 Enter Quote Data").Range("F5").Value
'LT
Sheets("Cost Sources").Range("J" & TPLR).Value = Sheets("3 Enter Quote Data").Range("F20").Value
'Min Buy Qty
Sheets("Cost Sources").Range("K" & TPLR).Value = Sheets("3 Enter Quote Data").Range("P20").Value
'Esclation Base date
Sheets("Cost Sources").Range("M" & TPLR).Value = Sheets("3 Enter Quote Data").Range("S20").Value
'Escalation Rate -SOLVED
Sheets("Cost Sources").Range("N" & TPLR).Value = Sheets("3 Enter Quote Data").Range("L20").Value & "-" & RDate
'[MF] Subcatagory
Sheets("Cost Sources").Range("P" & TPLR).Value = Sheets("3 Enter Quote Data").Range("W4").Value
'[MF] Created by
Sheets("Cost Sources").Range("W" & TPLR).Value = CurrentUser
'[MF] Created
Sheets("Cost Sources").Range("X" & TPLR).Value = Date
'[MF] Vendor Quote ID
Sheets("Cost Sources").Range("AD" & TPLR).Value = Sheets("3 Enter Quote Data").Range("F12").Value
'[MF] Path/Location
Sheets("Cost Sources").Range("AC" & TPLR).Value = Sheets("3 Enter Quote Data").Range("F12").Value
'Excess
If SumExcess > 0 Then
Sheets("Cost Sources").Range("Z" & TPLR).Value = "Yes"
Else
Sheets("Cost Sources").Range("Z" & TPLR).Value = "No"
End If

'NRe
If SumTariff > 0 Then
Sheets("Cost Sources").Range("AA" & TPLR).Value = "Yes"
Else
Sheets("Cost Sources").Range("AA" & TPLR).Value = "No"
End If

'Tariff
If SumNRE > 0 Then
Sheets("Cost Sources").Range("AB" & TPLR).Value = "Yes"
Else
Sheets("Cost Sources").Range("AB" & TPLR).Value = "No"
End If

'****************************
'Cost Source Details
'****************************

'***********************************
'NEED HELP WITH THE CODE FROM HERE DOWN
'***********************************

Dim CSDLR As Long

 CSDLR = Sheets("Cost Source Details").Cells(Rows.Count, "A").End(xlUp).Row + 1

'***********************************
'This data is repeated for each Row
'***********************************

'Material
Sheets("Cost Source Details").Range("A" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("F18").Value
'Material Type
Sheets("Cost Source Details").Range("B" & CSDLR).Value = "Part"
'Type
Sheets("Cost Source Details").Range("C" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("W3").Value
'PP ID
Sheets("Cost Source Details").Range("D" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L5").Value
'PP Revision
Sheets("Cost Source Details").Range("E" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("P5").Value

'*****************************************************************************************
'Loop through rows with the data from Rows 24 down to 56 if there is a value in cloumn F
'*****************************************************************************************

'From Qty
Sheets("Cost Source Details").Range("F" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("F24").Value
Sheets("Cost Source Details").Range("G" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("G24").Value
Sheets("Cost Source Details").Range("H" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("H24").Value

If Sheets("3 Enter Quote Data").Range("I24").Value > 0 And Sheets("3 Enter Quote Data").Range("J24").Value > 0 And Sheets("3 Enter Quote Data").Range("K24").Value > 0 Then
Sheets("Cost Source Details").Range("I" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L24").Value & " " & "{EXCESS: $" & Sheets("3 Enter Quote Data").Range("I24").Value & "} " & "{TARIFF: $" & Sheets("3 Enter Quote Data").Range("K24").Value & "}" & " {NRE: $" & Sheets("3 Enter Quote Data").Range("J24").Value & "}"

Else

If Sheets("3 Enter Quote Data").Range("J24").Value > 0 And Sheets("3 Enter Quote Data").Range("I24").Value > 0 Then
    Sheets("Cost Source Details").Range("I" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L24").Value & " " & "{EXCESS: $" & Sheets("3 Enter Quote Data").Range("I24").Value & "} " & "{NRE: $" & Sheets("3 Enter Quote Data").Range("J24").Value & "}"

Else

If Sheets("3 Enter Quote Data").Range("J24").Value > 0 And Sheets("3 Enter Quote Data").Range("K24").Value > 0 Then
    Sheets("Cost Source Details").Range("I" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L24").Value & " " & "{EXCESS: $" & Sheets("3 Enter Quote Data").Range("I24").Value & "} " & "{NRE: $" & Sheets("3 Enter Quote Data").Range("J24").Value & "}"

Else
If Sheets("3 Enter Quote Data").Range("I24").Value > 0 And Sheets("3 Enter Quote Data").Range("K24").Value > 0 Then
    Sheets("Cost Source Details").Range("I" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L24").Value & " " & "{EXCESS: $" & Sheets("3 Enter Quote Data").Range("I24").Value & "} " & "{TARIFF: $" & Sheets("3 Enter Quote Data").Range("K24").Value & "}"

Else
If Sheets("3 Enter Quote Data").Range("I24").Value > 0 Then
    Sheets("Cost Source Details").Range("I" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L24").Value & " " & "{EXCESS: $" & Sheets("3 Enter Quote Data").Range("I24").Value & "}"

Else
If Sheets("3 Enter Quote Data").Range("K24").Value > 0 Then
    Sheets("Cost Source Details").Range("I" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L24").Value & " " & "{NRE: $" & Sheets("3 Enter Quote Data").Range("K24").Value & "}"

Else
If Sheets("3 Enter Quote Data").Range("J24").Value > 0 Then
    Sheets("Cost Source Details").Range("I" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L24").Value & " " & "{TARIFF: $" & Sheets("3 Enter Quote Data").Range("K24").Value & "}"

Else

 Sheets("Cost Source Details").Range("I" & CSDLR).Value = Sheets("3 Enter Quote Data").Range("L24").Value


End If
End If
End If
End If
End If
End If
End If



End Sub


Screenshot 2022-02-09 164954.jpg

Screenshot 2022-02-09 165636.jpg
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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