简体   繁体   中英

Visual Basic for Applications Object Issue

The following code is part of a much larger Sub for a form automation project I've been working on for my employer.

'the following variable is named thusly for good reason.
Dim whyareyouactinglikethis As Object
Set whyareyouactinglikethis = Workbooks("DRQ Log.xlsx").Worksheets("DRQ's")
Dim finalRow As Object
With whyareyouactinglikethis
    Set finalRow = Cells(99999, 1).End(xlUp).Row
End With
Dim Source As Range
Source = whyareyouactinglikethis.Range("D8:D" & finalRow & "")

So my current issue is that for whatever reason, I get a run time error 424 when I set finalRow. I'm really confused as to why this is as "whyareyouactinglikethis" is clearly set to an Object that the with statement is using, and so is finalRow. I've been staring at this multiple ways for the last two hours, here's what I've tried.

Changing finalRow to a long variable

Changing finalRow to a Range variable (This introduces an error 91 statement)

Activating and selecting whyareyouactinglikethis before setting finalRow so I know this isn't some.select error or lack of proper references.

What do I need to do to fix this? Please let me know. I'm still somewhat new to Visual Basic, so I'm unsure if it's just my inexperience or if this is a legitimate issue.

You Set objects ( Workbooks, Sheets, Ranges, etc. ) and assign values ( last row variables, strings, etc ).


Sub try()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("DRQ's")
Dim lr As Long, Source As Range

lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set Source = ws.Range("D8:D" & lr)

End Sub

Also, a more piratical last row calc is used here rather arbitrarily starting from row 99,999

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM