+ Reply to Thread
Results 1 to 4 of 4

VBA Script not working - Please help

  1. #1
    Forum Contributor
    Join Date
    05-29-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    143

    VBA Script not working - Please help

    I'm trying to do something very simple or so I thought. I'm trying to check whether the last column contains text (e.g., "Yes") and if so, it will continue with the code. I keep getting an error (invalid or unqualified reference).

    All of the details are in the workbook and here's the code:

    Please Login or Register  to view this content.
    Attached Files Attached Files

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

    Re: VBA Script not working - Please help

    Are you trying to find the last column on a sheet with this,
    Please Login or Register  to view this content.
    if so, which sheet?
    If posting code please use code tags, see here.

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,676

    Re: VBA Script not working - Please help

    For starters, you have:
    LR = .Cells(1, .Columns.Count).End(xlToLeft).Column

    The dots before Cells and columns requires a reference sheet, such as:
    With Sheet1
    .....LR = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With

    However, you also have:
    Dim LR As String
    so that you are trying to find the number of the last column and return it as a string value (the number 10 is not the string "10")

    Next, you have:
    If (LR.Text = "Yes") Then...
    Which is trying to treat LR as a range variable so that you can test the value of the cell, but LR is a string and has no .Text property to access, it only holds the number of the last column as string data.

    You have two instances of similar mistakes:
    With WorkProWS
    For Each Wk In Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(3))

    The WITH statement says you are working with WorkProWS and the dots before Cells (.Cells) refers to that sheet, however, you don't preceed Range or Rows with a dot so that they may, or may not refer to that sheet. If it's the active sheet, they will. If some other sheet is active, Range and Rows.Count will refer to it instead.

    You have two additional With blocks within the first one. You can't, generally do that because each must refer back to the first. In this case they are independent worksheets.
    Please Login or Register  to view this content.
    Last edited by protonLeah; 10-31-2016 at 08:32 PM.
    Ben Van Johnson

  4. #4
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: VBA Script not working - Please help

    Please Login or Register  to view this content.
    Kind regards
    Leo

+ 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. Formula in script not working
    By at22134 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2015, 09:30 AM
  2. VB script not working
    By ssschaar in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-04-2014, 03:28 PM
  3. Script not working Error13 Please help
    By archangel9999 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-11-2014, 10:55 AM
  4. This script isn't working, why not?
    By ledworld in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2013, 06:04 PM
  5. [SOLVED] VBA Script has stopped working
    By Philfy in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-10-2013, 11:24 AM
  6. [SOLVED] my if-then-else script is not working
    By dschmitt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-01-2013, 02:24 AM
  7. VBA Script to open table in DB not working
    By DC88310 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2011, 04:20 PM

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