Simplify code

johnmpc

Board Regular
Joined
Oct 19, 2020
Messages
108
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have working code but haven't been able to simplify it. Please can you assist.
First 9 lines are unique. The rest are within A31:AG47
I missed out column Q originally so its at the bottom of the list, so would be possible to put back in line.

VBA Code:
Sub copyproductdata()

Dim c As Range
    Dim Custline As Range
     Set Custline = Sheets("Customers").Range("A:A").Find(Sheets("order template").Range("B6").Value)
    If Not Custline Is Nothing Then
    
    Custline.Offset(, 1) = Sheets("Order Template").Range("B4").Value
    Custline.Offset(, 2) = Sheets("Order Template").Range("B8").Value
    Custline.Offset(, 3) = Sheets("Order Template").Range("B10").Value
    Custline.Offset(, 4) = Sheets("Order Template").Range("B12").Value
    Custline.Offset(, 5) = Sheets("Order Template").Range("B14").Value
    Custline.Offset(, 6) = Sheets("Order Template").Range("B15").Value
    Custline.Offset(, 7) = Sheets("Order Template").Range("B16").Value
    Custline.Offset(, 8) = Sheets("Order Template").Range("B17").Value
    Custline.Offset(, 9) = Sheets("Order Template").Range("B18").Value
   
Custline.Offset(, 10) = Sheets("Order Template").Range("A31").Value
    Custline.Offset(, 11) = Sheets("Order Template").Range("D31").Value
    Custline.Offset(, 12) = Sheets("Order Template").Range("F31").Value
    Custline.Offset(, 13) = Sheets("Order Template").Range("I31").Value
    Custline.Offset(, 14) = Sheets("Order Template").Range("AB31").Value
    Custline.Offset(, 15) = Sheets("Order Template").Range("AC31").Value
    Custline.Offset(, 16) = Sheets("Order Template").Range("AD31").Value
    Custline.Offset(, 17) = Sheets("Order Template").Range("AE31").Value
    Custline.Offset(, 18) = Sheets("Order Template").Range("AF31").Value
    Custline.Offset(, 19) = Sheets("Order Template").Range("AG31").Value
    Custline.Offset(, 20) = Sheets("Order Template").Range("A32").Value
    Custline.Offset(, 21) = Sheets("Order Template").Range("D32").Value
    Custline.Offset(, 22) = Sheets("Order Template").Range("F32").Value
    Custline.Offset(, 23) = Sheets("Order Template").Range("I32").Value
    Custline.Offset(, 24) = Sheets("Order Template").Range("AB32").Value
    Custline.Offset(, 25) = Sheets("Order Template").Range("AC32").Value
    Custline.Offset(, 26) = Sheets("Order Template").Range("AD32").Value
    Custline.Offset(, 27) = Sheets("Order Template").Range("AE32").Value
    Custline.Offset(, 28) = Sheets("Order Template").Range("AF32").Value
    Custline.Offset(, 29) = Sheets("Order Template").Range("AG32").Value
    Custline.Offset(, 30) = Sheets("Order Template").Range("A33").Value
    Custline.Offset(, 31) = Sheets("Order Template").Range("D33").Value
    Custline.Offset(, 32) = Sheets("Order Template").Range("F33").Value
    Custline.Offset(, 33) = Sheets("Order Template").Range("I33").Value
    Custline.Offset(, 34) = Sheets("Order Template").Range("AB33").Value
    Custline.Offset(, 35) = Sheets("Order Template").Range("AC33").Value
    Custline.Offset(, 36) = Sheets("Order Template").Range("AD33").Value
    Custline.Offset(, 37) = Sheets("Order Template").Range("AE33").Value
    Custline.Offset(, 38) = Sheets("Order Template").Range("AF33").Value
    Custline.Offset(, 39) = Sheets("Order Template").Range("AG33").Value
    Custline.Offset(, 40) = Sheets("Order Template").Range("A34").Value
    Custline.Offset(, 41) = Sheets("Order Template").Range("D34").Value
    Custline.Offset(, 42) = Sheets("Order Template").Range("F34").Value
    Custline.Offset(, 43) = Sheets("Order Template").Range("I34").Value
    Custline.Offset(, 44) = Sheets("Order Template").Range("AB34").Value
    Custline.Offset(, 45) = Sheets("Order Template").Range("AC34").Value
    Custline.Offset(, 46) = Sheets("Order Template").Range("AD34").Value
    Custline.Offset(, 47) = Sheets("Order Template").Range("AE34").Value
    Custline.Offset(, 48) = Sheets("Order Template").Range("AF34").Value
    Custline.Offset(, 49) = Sheets("Order Template").Range("AG34").Value
    Custline.Offset(, 50) = Sheets("Order Template").Range("A35").Value
    Custline.Offset(, 51) = Sheets("Order Template").Range("D35").Value
    Custline.Offset(, 52) = Sheets("Order Template").Range("F35").Value
    Custline.Offset(, 53) = Sheets("Order Template").Range("I35").Value
    Custline.Offset(, 54) = Sheets("Order Template").Range("AB35").Value
    Custline.Offset(, 55) = Sheets("Order Template").Range("AC35").Value
    Custline.Offset(, 56) = Sheets("Order Template").Range("AD35").Value
    Custline.Offset(, 57) = Sheets("Order Template").Range("AE35").Value
    Custline.Offset(, 58) = Sheets("Order Template").Range("AF35").Value
    Custline.Offset(, 59) = Sheets("Order Template").Range("AG35").Value
    Custline.Offset(, 60) = Sheets("Order Template").Range("A36").Value
    Custline.Offset(, 61) = Sheets("Order Template").Range("D36").Value
    Custline.Offset(, 62) = Sheets("Order Template").Range("F36").Value
    Custline.Offset(, 63) = Sheets("Order Template").Range("I36").Value
    Custline.Offset(, 64) = Sheets("Order Template").Range("AB36").Value
    Custline.Offset(, 65) = Sheets("Order Template").Range("AC36").Value
    Custline.Offset(, 66) = Sheets("Order Template").Range("AD36").Value
    Custline.Offset(, 67) = Sheets("Order Template").Range("AE36").Value
    Custline.Offset(, 68) = Sheets("Order Template").Range("AF36").Value
    Custline.Offset(, 69) = Sheets("Order Template").Range("AG36").Value
    Custline.Offset(, 70) = Sheets("Order Template").Range("A37").Value
    Custline.Offset(, 71) = Sheets("Order Template").Range("D37").Value
    Custline.Offset(, 72) = Sheets("Order Template").Range("F37").Value
    Custline.Offset(, 73) = Sheets("Order Template").Range("I37").Value
    Custline.Offset(, 74) = Sheets("Order Template").Range("AB37").Value
    Custline.Offset(, 75) = Sheets("Order Template").Range("AC37").Value
    Custline.Offset(, 76) = Sheets("Order Template").Range("AD37").Value
    Custline.Offset(, 77) = Sheets("Order Template").Range("AE37").Value
    Custline.Offset(, 78) = Sheets("Order Template").Range("AF37").Value
    Custline.Offset(, 79) = Sheets("Order Template").Range("AG37").Value
    Custline.Offset(, 80) = Sheets("Order Template").Range("A38").Value
    Custline.Offset(, 81) = Sheets("Order Template").Range("D38").Value
    Custline.Offset(, 82) = Sheets("Order Template").Range("F38").Value
    Custline.Offset(, 83) = Sheets("Order Template").Range("I38").Value
    Custline.Offset(, 84) = Sheets("Order Template").Range("AB38").Value
    Custline.Offset(, 85) = Sheets("Order Template").Range("AC38").Value
    Custline.Offset(, 86) = Sheets("Order Template").Range("AD38").Value
    Custline.Offset(, 87) = Sheets("Order Template").Range("AE38").Value
    Custline.Offset(, 88) = Sheets("Order Template").Range("AF38").Value
    Custline.Offset(, 89) = Sheets("Order Template").Range("AG38").Value
    Custline.Offset(, 90) = Sheets("Order Template").Range("A39").Value
    Custline.Offset(, 91) = Sheets("Order Template").Range("D39").Value
    Custline.Offset(, 92) = Sheets("Order Template").Range("F39").Value
    Custline.Offset(, 93) = Sheets("Order Template").Range("I39").Value
    Custline.Offset(, 94) = Sheets("Order Template").Range("AB39").Value
    Custline.Offset(, 95) = Sheets("Order Template").Range("AC39").Value
    Custline.Offset(, 96) = Sheets("Order Template").Range("AD39").Value
    Custline.Offset(, 97) = Sheets("Order Template").Range("AE39").Value
    Custline.Offset(, 98) = Sheets("Order Template").Range("AF39").Value
    Custline.Offset(, 99) = Sheets("Order Template").Range("AG39").Value
    Custline.Offset(, 100) = Sheets("Order Template").Range("A40").Value
    Custline.Offset(, 101) = Sheets("Order Template").Range("D40").Value
    Custline.Offset(, 102) = Sheets("Order Template").Range("F40").Value
    Custline.Offset(, 103) = Sheets("Order Template").Range("I40").Value
    Custline.Offset(, 104) = Sheets("Order Template").Range("AB40").Value
    Custline.Offset(, 105) = Sheets("Order Template").Range("AC40").Value
    Custline.Offset(, 106) = Sheets("Order Template").Range("AD40").Value
    Custline.Offset(, 107) = Sheets("Order Template").Range("AE40").Value
    Custline.Offset(, 108) = Sheets("Order Template").Range("AF40").Value
    Custline.Offset(, 109) = Sheets("Order Template").Range("AG40").Value
    Custline.Offset(, 110) = Sheets("Order Template").Range("A41").Value
    Custline.Offset(, 111) = Sheets("Order Template").Range("D41").Value
    Custline.Offset(, 112) = Sheets("Order Template").Range("F41").Value
    Custline.Offset(, 113) = Sheets("Order Template").Range("I41").Value
    Custline.Offset(, 114) = Sheets("Order Template").Range("AB41").Value
    Custline.Offset(, 115) = Sheets("Order Template").Range("AC41").Value
    Custline.Offset(, 116) = Sheets("Order Template").Range("AD41").Value
    Custline.Offset(, 117) = Sheets("Order Template").Range("AE41").Value
    Custline.Offset(, 118) = Sheets("Order Template").Range("AF41").Value
    Custline.Offset(, 119) = Sheets("Order Template").Range("AG41").Value
    Custline.Offset(, 120) = Sheets("Order Template").Range("A42").Value
    Custline.Offset(, 121) = Sheets("Order Template").Range("D42").Value
    Custline.Offset(, 122) = Sheets("Order Template").Range("F42").Value
    Custline.Offset(, 123) = Sheets("Order Template").Range("I42").Value
    Custline.Offset(, 124) = Sheets("Order Template").Range("AB42").Value
    Custline.Offset(, 125) = Sheets("Order Template").Range("AC42").Value
    Custline.Offset(, 126) = Sheets("Order Template").Range("AD42").Value
    Custline.Offset(, 127) = Sheets("Order Template").Range("AE42").Value
    Custline.Offset(, 128) = Sheets("Order Template").Range("AF42").Value
    Custline.Offset(, 129) = Sheets("Order Template").Range("AG42").Value
    Custline.Offset(, 130) = Sheets("Order Template").Range("A43").Value
    Custline.Offset(, 131) = Sheets("Order Template").Range("D43").Value
    Custline.Offset(, 132) = Sheets("Order Template").Range("F43").Value
    Custline.Offset(, 133) = Sheets("Order Template").Range("I43").Value
    Custline.Offset(, 134) = Sheets("Order Template").Range("AB43").Value
    Custline.Offset(, 135) = Sheets("Order Template").Range("AC43").Value
    Custline.Offset(, 136) = Sheets("Order Template").Range("AD43").Value
    Custline.Offset(, 137) = Sheets("Order Template").Range("AE43").Value
    Custline.Offset(, 138) = Sheets("Order Template").Range("AF43").Value
    Custline.Offset(, 139) = Sheets("Order Template").Range("AG43").Value
    Custline.Offset(, 140) = Sheets("Order Template").Range("A44").Value
    Custline.Offset(, 141) = Sheets("Order Template").Range("D44").Value
    Custline.Offset(, 142) = Sheets("Order Template").Range("F44").Value
    Custline.Offset(, 143) = Sheets("Order Template").Range("I44").Value
    Custline.Offset(, 144) = Sheets("Order Template").Range("AB44").Value
    Custline.Offset(, 145) = Sheets("Order Template").Range("AC44").Value
    Custline.Offset(, 146) = Sheets("Order Template").Range("AD44").Value
    Custline.Offset(, 147) = Sheets("Order Template").Range("AE44").Value
    Custline.Offset(, 148) = Sheets("Order Template").Range("AF44").Value
    Custline.Offset(, 149) = Sheets("Order Template").Range("AG44").Value
    Custline.Offset(, 150) = Sheets("Order Template").Range("A45").Value
    Custline.Offset(, 151) = Sheets("Order Template").Range("D45").Value
    Custline.Offset(, 152) = Sheets("Order Template").Range("F45").Value
    Custline.Offset(, 153) = Sheets("Order Template").Range("I45").Value
    Custline.Offset(, 154) = Sheets("Order Template").Range("AB45").Value
    Custline.Offset(, 155) = Sheets("Order Template").Range("AC45").Value
    Custline.Offset(, 156) = Sheets("Order Template").Range("AD45").Value
    Custline.Offset(, 157) = Sheets("Order Template").Range("AE45").Value
    Custline.Offset(, 158) = Sheets("Order Template").Range("AF45").Value
    Custline.Offset(, 159) = Sheets("Order Template").Range("AG45").Value
    Custline.Offset(, 160) = Sheets("Order Template").Range("A46").Value
    Custline.Offset(, 161) = Sheets("Order Template").Range("D46").Value
    Custline.Offset(, 162) = Sheets("Order Template").Range("F46").Value
    Custline.Offset(, 163) = Sheets("Order Template").Range("I46").Value
    Custline.Offset(, 164) = Sheets("Order Template").Range("AB46").Value
    Custline.Offset(, 165) = Sheets("Order Template").Range("AC46").Value
    Custline.Offset(, 166) = Sheets("Order Template").Range("AD46").Value
    Custline.Offset(, 167) = Sheets("Order Template").Range("AE46").Value
    Custline.Offset(, 168) = Sheets("Order Template").Range("AF46").Value
    Custline.Offset(, 169) = Sheets("Order Template").Range("AG46").Value
    Custline.Offset(, 170) = Sheets("Order Template").Range("A47").Value
    Custline.Offset(, 171) = Sheets("Order Template").Range("D47").Value
    Custline.Offset(, 172) = Sheets("Order Template").Range("F47").Value
    Custline.Offset(, 173) = Sheets("Order Template").Range("I47").Value
    Custline.Offset(, 174) = Sheets("Order Template").Range("AB47").Value
    Custline.Offset(, 175) = Sheets("Order Template").Range("AC47").Value
    Custline.Offset(, 176) = Sheets("Order Template").Range("AD47").Value
    Custline.Offset(, 177) = Sheets("Order Template").Range("AE47").Value
    Custline.Offset(, 178) = Sheets("Order Template").Range("AF47").Value
    Custline.Offset(, 179) = Sheets("Order Template").Range("AG47").Value
   
Custline.Offset(, 180) = Sheets("Order Template").Range("Q31").Value
    Custline.Offset(, 181) = Sheets("Order Template").Range("Q32").Value
    Custline.Offset(, 182) = Sheets("Order Template").Range("Q33").Value
    Custline.Offset(, 183) = Sheets("Order Template").Range("Q34").Value
    Custline.Offset(, 184) = Sheets("Order Template").Range("Q35").Value
    Custline.Offset(, 185) = Sheets("Order Template").Range("Q36").Value
    Custline.Offset(, 186) = Sheets("Order Template").Range("Q37").Value
    Custline.Offset(, 187) = Sheets("Order Template").Range("Q38").Value
    Custline.Offset(, 188) = Sheets("Order Template").Range("Q39").Value
    Custline.Offset(, 189) = Sheets("Order Template").Range("Q40").Value
    Custline.Offset(, 190) = Sheets("Order Template").Range("Q41").Value
    Custline.Offset(, 191) = Sheets("Order Template").Range("Q42").Value
    Custline.Offset(, 192) = Sheets("Order Template").Range("Q43").Value
    Custline.Offset(, 193) = Sheets("Order Template").Range("Q44").Value
    Custline.Offset(, 194) = Sheets("Order Template").Range("Q45").Value
    Custline.Offset(, 195) = Sheets("Order Template").Range("Q46").Value
    Custline.Offset(, 196) = Sheets("Order Template").Range("Q47").Value

    
    Else
MsgBox ("Customer Not Found")
End If


End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
VBA Code:
Sub copyproductdata()
   Dim Ary As Variant
   Dim i As Long, j As Long, x As Long
   Dim c As Range
   Dim Custline As Range
   
   Ary = Array("A", "D", "F", "I", "AB", "AC", "AD", "AE", "AF", "AG")
   With Sheets("Order Template")
      Set Custline = Sheets("Customers").Range("A:A").Find(.Range("B6").Value)
      If Not Custline Is Nothing Then
         Custline.Offset(, 1) = .Range("B4").Value
         Custline.Offset(, 2) = .Range("B8").Value
         Custline.Offset(, 3) = .Range("B10").Value
         Custline.Offset(, 4) = .Range("B12").Value
         Custline.Offset(, 5) = .Range("B14").Value
         Custline.Offset(, 6) = .Range("B15").Value
         Custline.Offset(, 7) = .Range("B16").Value
         Custline.Offset(, 8) = .Range("B17").Value
         Custline.Offset(, 9) = .Range("B18").Value
         x = 9
         For i = 31 To 47
            For j = LBound(Ary) To UBound(Ary)
               x = x + 1
               Custline.Offset(, x) = .Range(Ary(j) & i).Value
            Next j
         Next i
         For i = 31 To 47
            x = x + 1
            Custline.Offset(, x) = .Range("Q" & i).Value
         Next i
         
      Else
         MsgBox ("Customer Not Found")
      End If
   End With

End Sub
 
Upvote 0
Perfect, Thanks.

Have been able to adapt for new bits too.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.
Just thinking, Could a line be added to exit the loop if the cell in column A is empty. would mean it would only run until the end of the data.
 
Upvote 0
Maybe
VBA Code:
Sub copyproductdata()
   Dim Ary As Variant
   Dim i As Long, j As Long, x As Long, Lr As Long
   Dim c As Range
   Dim Custline As Range
   
   Ary = Array("A", "D", "F", "I", "AB", "AC", "AD", "AE", "AF", "AG")
   With Sheets("Order Template")
      Lr = .Range("A" & Rows.Count).End(xlUp).Row
      Set Custline = Sheets("Customers").Range("A:A").Find(.Range("B6").Value)
      If Not Custline Is Nothing Then
         Custline.Offset(, 1) = .Range("B4").Value
         Custline.Offset(, 2) = .Range("B8").Value
         Custline.Offset(, 3) = .Range("B10").Value
         Custline.Offset(, 4) = .Range("B12").Value
         Custline.Offset(, 5) = .Range("B14").Value
         Custline.Offset(, 6) = .Range("B15").Value
         Custline.Offset(, 7) = .Range("B16").Value
         Custline.Offset(, 8) = .Range("B17").Value
         Custline.Offset(, 9) = .Range("B18").Value
         x = 9
         For i = 31 To Lr
            For j = LBound(Ary) To UBound(Ary)
               x = x + 1
               Custline.Offset(, x) = .Range(Ary(j) & i).Value
            Next j
         Next i
         For i = 31 To Lr
            x = x + 1
            Custline.Offset(, x) = .Range("Q" & i).Value
         Next i
         
      Else
         MsgBox ("Customer Not Found")
      End If
   End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,220
Messages
6,170,807
Members
452,354
Latest member
yuzha

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