Home > Software engineering >  Is there something wrong with the way I am writing this VBA For Loop?
Is there something wrong with the way I am writing this VBA For Loop?

Time:08-05

I'm trying to write a code that will copy data over from one worksheet into another, I keep getting an error specifically on the line where I implement my For loop, the error says "Application-defined or object-defined error", any help would be greatly appreciated.

Global qty As Variant

***********

Sub PartOrder()


qty = Application.InputBox("How many assemblies are needed?")

Sheets.Add After:=Worksheets(Sheets.Count)
PartOrderForm.Show



End Sub

If CheckBox1.Value = True Then


ActiveSheet.Range("A1") = "Part Number"
ActiveSheet.Range("B1") = "Part Name"
ActiveSheet.Range("C1") = "Number of Parts Needed"
Range("A2").Activate
For i = 2 To 8
ActiveSheet.Cells(i - 1, 1) = Worksheets("F8X SUSPENSION LINKS REV2").Cells(8 - i, 2)
Next i

Else: End If
End Sub

CodePudding user response:

When the counter reaches 8, you are referring to 8-i row, which turns out to be 0 resulting in the error.

CodePudding user response:

Seems like an off by one error

This part of your code Cells(8 - i, 2), goes to Cell 0. And in excel, it the cell index needs to start with 1

  • Related