I'm writing my first VBA code and have a complex set of steps working on data in Excel. Everything works fine except this one sub, which runs fine, but gives an error at the end, which I'm pretty sure is because at some point after the data is exhausted, it returns "nothing" and that causes the "Run-time error '91': Object variable or With block variable not set" crash. I'm stuck with how to fix this. Any suggestions? I'm sure it's something simple - and yes, this code is slow, but it works. Thanks!
Sub D_GSB_Sort_Data()
Dim MyCell As Range
For Each MyCell In Range("A2:A5000")
ActiveWorkbook.ActiveSheet.Columns(1).Find("*Submitted*").Select
ActiveCell.Offset(0, 3).Cut ActiveCell.Offset(0, 0)
ActiveCell.Offset(1, 1).Cut ActiveCell.Offset(0, 1)
ActiveCell.Offset(2, 1).Cut ActiveCell.Offset(0, 2)
ActiveCell.Offset(3, 1).Cut ActiveCell.Offset(0, 3)
ActiveCell.Offset(4, 0).Cut ActiveCell.Offset(0, 4)
Next MyCell
End Sub
Findsucceeded before attempting to do anything else, including theSelect.Range.AutoFilterhere instead ofFindthough.Find()if you're already looping over the range - just useIf MyCell.Value Like "*Submitted *" Thenand useMyCellin place ofActiveCellSub GSB_Sort_Data() Dim MyCell As Range For Each MyCell In Range("A2:A5000") If MyCell.Value Like "*Submitted*" Then MyCell.Offset(0, 3).Cut MyCell.Offset(0, 0) MyCell.Offset(1, 1).Cut MyCell.Offset(0, 1) MyCell.Offset(2, 1).Cut MyCell.Offset(0, 2) MyCell.Offset(3, 1).Cut MyCell.Offset(0, 3) MyCell.Offset(4, 0).Cut MyCell.Offset(0, 4) Next MyCell End Subbut now get a "Next without For" errorEnd If... add it prior toNext MyCell.