Compile Error in Finding 1st Blank Line.

mysterious-dr-x

Board Regular
Joined
Jul 29, 2011
Messages
51
If I am very lucky this will be my last question.

This code causes "Compile error: invalid or unqualified reference" & highlights ".Range" in LRs. If I take out the whole LRs section it works fine (except for the obvious fact that the 2nd mention of LR uses the same range determined above, this needs to be different) so this is clearly the problematic section.

What do I need to do to make it work?

Code:
Sub UpdateGlobal()
    '
    ' Macro5 UpdateGlobal
    '
    ' Template: find last line, select range & copy
    ' Global: open, find 1st blank line, paste, save & close
    ' Template: clear selection

Application.DisplayAlerts = False

    Dim ws As Worksheet
    Dim LR As Long, lTopRow As Long, LRs As Long
    Set ws = Worksheets("Drawing Register")
    lTopRow = 9
    
    With ws
        LR = .Cells.Find(What:="*", After:=.Range("A1"), _
            LookIn:=xlFormulas, SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, MatchCase:=False).Row
            
        Rows("9:" & LR).Select
        Selection.Copy
    End With
    
    Workbooks.Open FileName:="K:\Testing\Global3.xlsm"
    Sheets("Drawing Register").Select
    
    LRs = .Cells.Find(What:="*", After:=.Range("A1"), _
        LookIn:=xlFormulas, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, MatchCase:=False).Row
        
    Range("A" & LR + 1).Select
    ActiveSheet.Paste
    Range("A1").Select
    Application.CutCopyMode = False

    ActiveWorkbook.Save
    ActiveWindow.Close
    Range("A1").Select
 
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try

Code:
LRs = Cells.Find(What:="*", After:=Range("A1"), _
        LookIn:=xlFormulas, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, MatchCase:=False).Row
 
Upvote 0
In the first case it is within the With ws block and . qualifies the range as belonging to ws.

In the second case, your statement is not in a With block so the . causes the error.
 
Upvote 0
Ok, that makes sense. Cheers.

Lastly, VoG's solution solves my problem (thanks), but is there anything wrong with the code in the sense that, it works fine, but should technically be done another way?
 
Upvote 0
Do you have a column in your source sheet that will always contain a non-blank value if that row is populated and needs to be extracted? Commonly this could be because of an ID number of the record in the first column? If so, you don't need to use Find but can simply ascertain the last row with data in this particular column and use this to determine the range copied:


Code:
Dim lr As Long
 
'Assume col A will always have data
 
lr = Cells(Rows.Count,"A").End(xlUp).Row   'go backwards from bottom of sheet up.
 
Range("9:" & lr).Copy

The same principle can be applied to finding the destination row on the sheet you are transferring the data to.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top