+ Reply to Thread
Results 1 to 5 of 5

Invalid or Unqualified Reference Error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-16-2012
    Location
    Brooklyn
    MS-Off Ver
    Excel 2010 PC
    Posts
    137

    Invalid or Unqualified Reference Error

    Hi.

    I'm trying to write a Macro to go through each worksheet in a workbook and modify some ranges. I keep getting an error and I don't know what's wrong with my Syntax. Can any please help?
    The error occurs when I set LastRow = to .Rows...

    HTML Code: 

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Invalid or Unqualified Reference Error

    You're using a .Range notation but have no corresponding With Statement....

  3. #3
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Invalid or Unqualified Reference Error

    Range("A" & Rows.Count).End(xlUp).Row
    I don't think you want periods on this line. I believe that is used with a 'With/End With' clause.

    I believe that will include the ones below too. It might be quicker to add
    with ws
    'code
    end with
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  4. #4
    Forum Contributor
    Join Date
    10-16-2012
    Location
    Brooklyn
    MS-Off Ver
    Excel 2010 PC
    Posts
    137

    Re: Invalid or Unqualified Reference Error

    Thanks guys. I'm gonna apply these suggestions when I'm back in front of my computer. Thanks again for the assistance.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Invalid or Unqualified Reference Error

    Perhaps, with the missing With.
    Sub Year_Fix()
    
    Dim Rng1 As Range, Rng2 As Range, Rng3, Rng4 As Range
    Dim LastRow As Long
    Dim ws1 As Worksheet
    
        For Each ws1 In ActiveWorkbook.Worksheets
    
            With ws1
                With .Columns("K:K")
                    .Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    .Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    .Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                End With
                LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
                Set Rng1 = .Range("N2", .Cells(LastRow, 14))    'Original
                Set Rng2 = .Range("M2", .Cells(LastRow, 13))    'Year
                Set Rng3 = .Range("L2", .Cells(LastRow, 12))    'Century
                Set Rng4 = .Range("K2", .Cells(LastRow, 11))    'Date
    
                Rng4.Formula = "=IF(N2>0,TEXT(N2,""dd/mm/""),"""")"
                Rng3.Formula = "=IF(N2>0,LEFT(TEXT(N2,""yyyy""),2),"""")"
                Rng2.Formula = "=IF(N2>0,RIGHT(TEXT(N2,""yyyy""),2),"""")"
    
                Rng4.Select
                Rng4.Copy
                Rng4.PasteSpecial Paste:=xlPasteValues
    
                Rng3.Copy
                Rng3.PasteSpecial Paste:=xlPasteValues
    
                Rng3.Replace What:="19", Replacement:="20", LookAt:=xlPart, _
                             SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                             ReplaceFormat:=False
    
                Rng2.Copy
                Rng2.PasteSpecial Paste:=xlPasteValues
    
    '            Rng1 = Rng3 & Rng2 & Rng4
    
                Rng1.Copy
                Rng1.PasteSpecial Paste:=xlPasteValues
    
                'Remove the helper columns
                .Columns("K:M").Delete Shift:=xlToLeft
    
            End With
    
        Next ws1
    
    End Sub
    By the way, I've commented out this line because it will error
    
                Rng1 = Rng3 & Rng2 & Rng4
    if you are trying to do some sort of concatenation involving 3 ranges have a look at using a formula.
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Invalid or unqualified reference
    By michelle 1 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-22-2013, 08:11 AM
  2. [SOLVED] VBA Debug (compile error invalid or unqualified reference)
    By Kezwick in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-14-2012, 04:38 AM
  3. Invalid or unqualified reference error using the .Find in vba
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-08-2011, 07:10 PM
  4. Invalid or Unqualified Reference
    By dcompagnone in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-13-2010, 08:45 AM
  5. Invalid or unqualified reference error when inserting a formula into a cell via VBA
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-11-2010, 10:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1