Hi folks!
I have data in 2 rows. Row 203 contains currency values. Row 204 contains 3-digit codes starting at Col 22 and ending at Col 136 (120, 134, 200 etc).
I would like code that will search row 204 for specific codes and upon finding a given code, two things will happen.
1 A sumif formula based on values in row 203 will be inserted in the cell directly below the 3-digit code, ie, into row 205.
2 That same cell will also be defined with a range name which will include the sheet name as a part of the range name. Eg, if the sheet name = Apr-May 13, then the range name would be TotSalesApr-May 13 etc.
The code will then continue on to find the next specific code. The following is the best I can do with my limited skills in VBA. It works fine for a single instance but how do I make it dynamic.
Private Sub SumMainCodes()
sn = ActiveSheet.Name
Cells(205, 22).FormulaR1C1 = "=SUMIF(R[-1]C:R[-1]C[20], ""<=120"", R[-3]C[1]:R[-3]C[20])"
Cells(205, 22).Select
With Selection
ActiveWorkbook.Names.Add Name:="TotSales" & sn, RefersToR1C1:= _
"=" & sn & "!R205C22"
ActiveWorkbook.Names("TotSales" & sn).Comment = ""
End With
End Sub
Cheers!
I have data in 2 rows. Row 203 contains currency values. Row 204 contains 3-digit codes starting at Col 22 and ending at Col 136 (120, 134, 200 etc).
I would like code that will search row 204 for specific codes and upon finding a given code, two things will happen.
1 A sumif formula based on values in row 203 will be inserted in the cell directly below the 3-digit code, ie, into row 205.
2 That same cell will also be defined with a range name which will include the sheet name as a part of the range name. Eg, if the sheet name = Apr-May 13, then the range name would be TotSalesApr-May 13 etc.
The code will then continue on to find the next specific code. The following is the best I can do with my limited skills in VBA. It works fine for a single instance but how do I make it dynamic.
Private Sub SumMainCodes()
sn = ActiveSheet.Name
Cells(205, 22).FormulaR1C1 = "=SUMIF(R[-1]C:R[-1]C[20], ""<=120"", R[-3]C[1]:R[-3]C[20])"
Cells(205, 22).Select
With Selection
ActiveWorkbook.Names.Add Name:="TotSales" & sn, RefersToR1C1:= _
"=" & sn & "!R205C22"
ActiveWorkbook.Names("TotSales" & sn).Comment = ""
End With
End Sub
Cheers!