I am trying to store ranges in range variables startID, endID, and endDest, but I keep getting runtime error 1004 "Method Range of object _Global failed."
I have a list of loans on the Control Page of my workbook, and they all have multiple lines (anywhere from 2 to 6 lines) like this:
- A | B | C
- loan 1 | stuff | 1.0
- loan 1 | stuff | 1.1
- loan 2 | stuff | 2.0
- loan 2 | stuff | 2.1
- loan 2 | stuff | 2.2
The whole purpose is to move the loan where the activecell is down to the bottom of the list. Here's what I'm having trouble with (so far): Say the activecell is B2 in the little bulleted list makeshift worksheet above--the row of the activecell along with a fixed column index of 3 indicates that I'm on line 1.1. I then "fix" 1.1 to 1 and store that integer in currentID. Now I can Find the first occurrence of currentID in Column C, then store the address of the range where currentID appears in the range variable startID. In this case, I should be storing C1 in startID.
I have read that Find returns a range. So I tried Set startID = .Range("C:C").Find(what:=currentID)
, but that just stores the currentID of 1 in startID. So then I tried Set startID = Range(.Range("C:C").Find(what:=currentID))
(which is what appears below in my code block), but that causes the runtime error. I have also tried adding and removing periods in case I was messing up the With statement, but that didn't help. Any thoughts?
If there is some obvious thing I'm missing, I apologize. I have been using VBA for about three weeks now, so I typically have to google every single thing I need until I start remembering recurring themes.
Sub ExpireLoan()
With Workbooks("1908 AUS IC Loans Recon.xlsm").Worksheets("Control Page")
Dim currentID As Integer
Dim startID As Range
Dim endID As Range
Dim endDest As Range
Dim rowCount As Integer
'find the current loan range
currentID = Fix(.Cells(ActiveCell.Row, 3).Value)
Set startID = Range(.Range("C:C").Find(what:=currentID))
Set endID = Range(.Range("C:C").Find(what:=currentID + 1))
Set endID = Range(endID.Offset(-1, 0))
'find the last row
Set endDest = .Range("A7").End(xlDown)
Set endDest = endDest.Offset(-1, 0)
'copy the current loan and paste into the end of the table
rowCount = .Range(startID, endID).Count
.Range(startID, endID).EntireRow.Copy
.endDest.EntireRow.PasteSpecial
'set bottom border at new end of the table
endDest.Select
ActiveCell.Offset(0, 0).Range("A1:AF1").Borders(xlEdgeBottom).LineStyle = xlDouble
'delete rows from loan's original position above
.Range(startID, endID).EntireRow.Delete
'set sort ID
ActiveCell.Offset(0, 2).Value = Fix(ActiveCell.Offset(-1, 2).Value) + 1
For i = 0 To rowCount - 2
ActiveCell.Offset(i + 1, 2).Value = ActiveCell.Offset(i, 2).Value + 0.1
Next
'delete loanTypeCode
ActiveCell.Offset(0, 1).ClearContents
For i = 1 To rowCount - 1
ActiveCell.Offset(i, 1).ClearContents
Next
'fix sortID
Set sortIdRange = Range(startID, startID.End(xlDown))
Call subtractOneFromCells
'make it red
ActiveCell.Offset(0, 0).Range("A1:AF1").Interior.TintAndShade = 0.399975585192419
For i = 1 To rowCount - 1
ActiveCell.Offset(i, 17).Range("A1:P1").Interior.TintAndShade = 0.399975585192419
Next
End With
End Sub