Need VBA code to create loops Please
Solved/Closed
Related:
- Need VBA code to create loops Please
- Fruity loops free download - Download - Musical production
- Battery reset code - Guide
- How to get whatsapp verification code online - Guide
- Create skype account with gmail - Guide
- Samsung volume increase code - Guide
3 responses
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
May 2, 2010 at 06:26 PM
May 2, 2010 at 06:26 PM
Not entirely sure what you were looking for as I see that your code already had a loop. This is based on what i thought you meant
Sub WOLoopTest() Dim DestSheet As Worksheet Dim NewRow As Integer Dim sRow As Long 'row index on source worksheet Dim dRow As Long 'row index on destination worksheet Dim sCount As Long Dim SumL As Variant Dim strsearch As String Dim lastORow As Long Dim myORow As Long lastORow = Worksheets("Itemized Costs2").Cells(Rows.Count, "O").end(xlup).Row Set DestSheet = Worksheets("Itemized Costs3") SumL = 0 sCount = 0 For myORow = 2 To lastORow NewRow = Worksheets("Itemized Costs2").Range("O" & myORow).Value 'Destination ROW NUMBER Sheets("Itemized Costs3").Select strsearch = CStr(Worksheets("Itemized Costs2").Range("N" & myORow).Value) 'code num to search for in data sheet For sRow = 1 To Range("V65536").End(xlUp).Row 'search for code If Cells(sRow, "V") Like strsearch Then sCount = sCount + 1 dRow = NewRow + 1 NewRow = NewRow + 1 'destination row +1 DestSheet.Cells(dRow, "B") = Cells(sRow, "B") DestSheet.Cells(dRow, "C") = Cells(sRow, "C") DestSheet.Cells(dRow, "D") = Cells(sRow, "D") DestSheet.Cells(dRow, "E") = Cells(sRow, "E") DestSheet.Cells(dRow, "F") = Cells(sRow, "F") DestSheet.Cells(dRow, "G") = Cells(sRow, "G") DestSheet.Cells(dRow, "H") = Cells(sRow, "H") DestSheet.Cells(dRow, "I") = Cells(sRow, "I") DestSheet.Cells(dRow, "J") = Cells(sRow, "J") DestSheet.Cells(dRow, "K") = Cells(sRow, "K") DestSheet.Cells(dRow, "L") = Cells(sRow, "L") SumL = SumL + Cells(sRow, "L") End If Next sRow Next myORow MsgBox "Total = " & Format(SumL, Format:="$0,0.00") & Chr(10) & Chr(10) & sCount & " Row(s) have been copied", vbInformation, "Transfer Done" ' ActiveCell.Offset(1, 0).Select ' Loop ' Application.Run "Sheet15.WOLoopTest2" End Sub
I see where the "type mismatch" is coming from. I had formulas in the column "O" that had no value. It works now. I just need to find a "work around" for the formula issue. I think that will be an easy fix though. Thanks so much for your help. You were great!
Hi, i have found same problem.
I have a vba code that have too long, i can't be able to make it short because i have not experience in vba. but i believe its will be done some one that have experience. Here is code:
Sub Macro1()
'
' Macro1 Macro
'
Range("A5").Value = "4-16 - 4-22"
Range("B5").Value = "1st"
Range("C5").FormulaR1C1 = "=AVERAGEIFS('adt4-16 - 4-22'!C16, 'adt4-16 - 4-22'!C16, "">301"",'adt4-16 - 4-22'!C16, ""<480"")"
Range("D5").FormulaR1C1 = "=COUNTIFS('adt4-16 - 4-22'!C16,"">""&301,'adt4-16 - 4-22'!C16,""<""&480)"
Range("E5").FormulaR1C1 = "=(R2C3-RC3)*(R1C4*RC4)"
Range("F5").FormulaR1C1 = "=AVERAGEIFS('adt4-16 - 4-22'!C16, 'adt4-16 - 4-22'!C16, "">=1"",'adt4-16 - 4-22'!C16, ""<300"")"
Range("G5").FormulaR1C1 = "=COUNTIFS('adt4-16 - 4-22'!C16,"">=""&1,'adt4-16 - 4-22'!C16,""<""&300)"
Range("H5").FormulaR1C1 = "=(R2C3-RC6)*(R1C4*RC7)"
Range("I5").FormulaR1C1 = "=AVERAGE('adt4-16 - 4-22'!C16)"
Range("J5").FormulaR1C1 = "=COUNTIFS('adt4-16 - 4-22'!C16,"">=""&1)"
Range("K5").FormulaR1C1 = "=(R2C3-RC9)*(R1C4*RC10)"
Range("A6").Value = "4-23 - 4-29"
Range("B6").Value = "2nd"
Range("C6").FormulaR1C1 = "=AVERAGEIFS('adt4-23 - 4-29'!C16, 'adt4-23 - 4-29'!C16, "">301"",'adt4-23 - 4-29'!C16, ""<480"")"
Range("D6").FormulaR1C1 = "=COUNTIFS('adt4-23 - 4-29'!C16,"">""&301,'adt4-23 - 4-29'!C16,""<""&480)"
Range("E6").FormulaR1C1 = "=(R[-4]C3-RC3)*(R1C4*RC4)"
Range("F6").FormulaR1C1 = "=AVERAGEIFS('adt4-23 - 4-29'!C16, 'adt4-23 - 4-29'!C16, "">=1"",'adt4-23 - 4-29'!C16, ""<300"")"
Range("G6").FormulaR1C1 = "=COUNTIFS('adt4-23 - 4-29'!C16,"">=""&1,'adt4-23 - 4-29'!C16,""<""&300)"
Range("H6").FormulaR1C1 = "=(R[-4]C3-RC6)*(R1C4*RC7)"
Range("I6").FormulaR1C1 = "=AVERAGE('adt4-23 - 4-29'!C16)"
Range("J6").FormulaR1C1 = "=COUNTIFS('adt4-23 - 4-29'!C16,"">=""&1)"
Range("K6").FormulaR1C1 = "=(R2C3-RC9)*(R1C4*RC10)"
End Sub
This code is only for 2 row & 2 sheet, i have hundred of rows and sheets for calculation.
thanks
I have a vba code that have too long, i can't be able to make it short because i have not experience in vba. but i believe its will be done some one that have experience. Here is code:
Sub Macro1()
'
' Macro1 Macro
'
Range("A5").Value = "4-16 - 4-22"
Range("B5").Value = "1st"
Range("C5").FormulaR1C1 = "=AVERAGEIFS('adt4-16 - 4-22'!C16, 'adt4-16 - 4-22'!C16, "">301"",'adt4-16 - 4-22'!C16, ""<480"")"
Range("D5").FormulaR1C1 = "=COUNTIFS('adt4-16 - 4-22'!C16,"">""&301,'adt4-16 - 4-22'!C16,""<""&480)"
Range("E5").FormulaR1C1 = "=(R2C3-RC3)*(R1C4*RC4)"
Range("F5").FormulaR1C1 = "=AVERAGEIFS('adt4-16 - 4-22'!C16, 'adt4-16 - 4-22'!C16, "">=1"",'adt4-16 - 4-22'!C16, ""<300"")"
Range("G5").FormulaR1C1 = "=COUNTIFS('adt4-16 - 4-22'!C16,"">=""&1,'adt4-16 - 4-22'!C16,""<""&300)"
Range("H5").FormulaR1C1 = "=(R2C3-RC6)*(R1C4*RC7)"
Range("I5").FormulaR1C1 = "=AVERAGE('adt4-16 - 4-22'!C16)"
Range("J5").FormulaR1C1 = "=COUNTIFS('adt4-16 - 4-22'!C16,"">=""&1)"
Range("K5").FormulaR1C1 = "=(R2C3-RC9)*(R1C4*RC10)"
Range("A6").Value = "4-23 - 4-29"
Range("B6").Value = "2nd"
Range("C6").FormulaR1C1 = "=AVERAGEIFS('adt4-23 - 4-29'!C16, 'adt4-23 - 4-29'!C16, "">301"",'adt4-23 - 4-29'!C16, ""<480"")"
Range("D6").FormulaR1C1 = "=COUNTIFS('adt4-23 - 4-29'!C16,"">""&301,'adt4-23 - 4-29'!C16,""<""&480)"
Range("E6").FormulaR1C1 = "=(R[-4]C3-RC3)*(R1C4*RC4)"
Range("F6").FormulaR1C1 = "=AVERAGEIFS('adt4-23 - 4-29'!C16, 'adt4-23 - 4-29'!C16, "">=1"",'adt4-23 - 4-29'!C16, ""<300"")"
Range("G6").FormulaR1C1 = "=COUNTIFS('adt4-23 - 4-29'!C16,"">=""&1,'adt4-23 - 4-29'!C16,""<""&300)"
Range("H6").FormulaR1C1 = "=(R[-4]C3-RC6)*(R1C4*RC7)"
Range("I6").FormulaR1C1 = "=AVERAGE('adt4-23 - 4-29'!C16)"
Range("J6").FormulaR1C1 = "=COUNTIFS('adt4-23 - 4-29'!C16,"">=""&1)"
Range("K6").FormulaR1C1 = "=(R2C3-RC9)*(R1C4*RC10)"
End Sub
This code is only for 2 row & 2 sheet, i have hundred of rows and sheets for calculation.
thanks
May 2, 2010 at 06:53 PM
May 2, 2010 at 07:00 PM
One error was this. I have now corrected it
lastORow = Worksheets("Itemized Costs2").Cells(Rows.Count, "O").End(xlUp).Row