+ Reply to Thread
Results 1 to 5 of 5

Issues with running a Procedure in Excel 2003

Hybrid View

  1. #1
    Registered User
    Join Date
    05-14-2012
    Location
    Gurgaon
    MS-Off Ver
    Excel 2003
    Posts
    2

    Issues with running a Procedure in Excel 2003

    Hi,

    I'm new to VBA and following a book called Mastering Excel 2003 Programming with VBA to learn. I'm stuck on one of the examples given in the book. Please see the details below:

    1: I've created an Excel sheet under C:\Examples\ named Example.xls. The sheet1 os the excel has data in Column B3 (="animal"), Column B4 (="art") and Column B5 (="test"). The pupose of the code (Below) is to make the column bold if the data starts with "a".

    2: As given in the book, I've added a module to this sheet & copied the below code to the module:
    Sub SimpleListProcessing()
        ' Declare our varibles
        Dim wb As Workbook
        Dim rg As Range
    
        ' Initialize our variables
        Set wb = Workbooks.Open("C:\Examples\Example.xls")
        Set rg = wb.Worksheets("Sheet1").Range("B3")
    
        ' Loop through cells moving down one cell
        ' until an empty cell is found
        Do Until IsEmpty(rg)
            If Left(rg.Value, 1) = "A" Then rg.Font.Bold = True
            Set rg = rg.Offset(1, 0)
        Loop
    
        ' Dereference object variables
        Set rg = Nothing
        Set wb = Nothing
    End Sub
    3: However, when I try to execute the above mentioned code, the strange thing is that Nothing Happens. What I mean is that No Error is thrown & nor does the data in sheet1 is made bold.

    Can someone pls help me understand what exactly is the issue here?

    Note: Just to make sure if other piece of code is running through a module, I added the below code to module & it works (Gives me an Output):
    Sub ExitForExample()
        Dim nLastRow As Integer
        Dim nColumn As Integer
        Dim nRow As Integer
        Dim ws As Worksheet
    
        Set ws = ThisWorkbook.Worksheets(1)
        nLastRow = 15
        nColumn = 1
    
        For nRow = 1 To nLastRow
            If ws.Cells(nRow, nColumn).Address = "$A$7" Then
                Debug.Print "Found cell. Exiting for loop."
                Exit For
            Else
                Debug.Print ws.Cells(nRow, nColumn).Address
            End If
        Next
    
        Set ws = Nothing
    End Sub
    Last edited by arlu1201; 05-14-2012 at 08:07 AM. Reason: Please use code tags in future as per forum rules.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Issues with running a Procedure in Excel 2003

    In Excel the letters "a" and "A" are not automatically equivalent. You can tell Excel to treat upper and lower case as equivalent in the macros in that module by putting this at the top of that module:

    Option Compare Text
    
    Sub SimpleListProcessing()
        ' Declare our varibles
        Dim wb As Workbook

    Other ways is to fix the test so it matches the lower case values:
            If Left(rg.Value, 1) = "a" Then rg.Font.Bold = True

    Or making the string upper case before you test "A":
            If Left(UCase(rg.Value), 1) = "A" Then rg.Font.Bold = True
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Issues with running a Procedure in Excel 2003

    Hello there,

    Are the values in B3 and B4 capitalized? In other words in B3 is it "animal" or "Animal" and in B4 is it "art" or "Art"? In VBA things are case sensitive so if in your Example worksheet the values begin with a lowercase "a" then VBA does not recognize them as "A". Change the "A" in the code to "a" or capitalize the "a" in animal and art to make the code run correctly.

    Let me know if this works!

    Thanks!

  4. #4
    Registered User
    Join Date
    05-14-2012
    Location
    Gurgaon
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Issues with running a Procedure in Excel 2003

    Hi,

    I did tried with both the options (Using the Font as "a" instead of "A" and by mentioning the Compare Text code.

    However, still nothing is happening.

    Is this because I'm trying to open a Sheet, which is already opened?

    Thanks,

  5. #5
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Issues with running a Procedure in Excel 2003

    Are you trying to run this from the Example workbook. In other words do you have the code written in the Example workbook and not another workbook?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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