+ Reply to Thread
Results 1 to 2 of 2

General querry

Hybrid View

  1. #1
    Registered User
    Join Date
    09-02-2014
    Location
    Kolkata
    MS-Off Ver
    2007
    Posts
    1

    General querry

    Hi All,

    I am new to this forum. Also I am new to VBA i.e beginners. Can anyone clarify what does the following code do:

    Cells(Rows.Count, 1).End(xlUp).Row

    Also what is lbound and ubound? what are they used for? Please clarify this with an example.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: General querry

    Hi and welcome to ExcelForum,

    Your post is fine, but please make sure you read the forum rules for future reference.
    We are not allowed to answer your posts if the rules aren't followed, especially if you are posting code samples (CODE TAGS are needed): http://www.excelforum.com/forum-rule...rum-rules.html


    Cells(Rows.Count, 1).End(xlUp).Row returns the number of the last row with data in Column 'A'. See the following for a better explanation than I can do:
    http://www.globaliconnect.com/excel/...=79&Itemid=475

    LBound and UBound are used to find the lower and upper bounds for an array. Consider the following example:
    Option Explicit
    
    Sub ArrayExample()
       
      Dim a(1 To 3) As Long
      Dim b(5 To 7) As Long
      Dim i As Long
      Dim iMaxIndexForC As Long
      
      'Static arrays a and b
      a(1) = 1
      a(2) = 3
      a(3) = 5
      For i = 1 To 3
        Debug.Print "a(" & i & ") = " & a(i)  'Outputs results in Immediate Window (CTRL G)
      Next i
       
      b(5) = 7
      b(6) = 8
      b(7) = 9
      For i = 5 To 7
        Debug.Print "b(" & i & ") = " & b(i)  'Outputs results in Immediate Window (CTRL G)
      Next i
    
      For i = LBound(a) To UBound(a)
        Debug.Print "a(" & i & ") = " & a(i)  'Outputs results in Immediate Window (CTRL G)
      Next i
       
      For i = LBound(b) To UBound(b)
        Debug.Print "b(" & i & ") = " & b(i)  'Outputs results in Immediate Window (CTRL G)
      Next i
    
    
      'Dynamic array c
      Dim c() As Integer
      
      iMaxIndexForC = 0
      For i = 1 To 6
        iMaxIndexForC = iMaxIndexForC + 1
        If iMaxIndexForC = 1 Then
          ReDim c(1 To iMaxIndexForC)               'Redim sets the array indices and clears the values in the array
        Else
          ReDim Preserve c(1 To iMaxIndexForC)      'Redim 'Preserve' sets the array indices and keeps existing values in the array
        End If
        c(iMaxIndexForC) = i * 3
      Next i
      For i = LBound(c) To UBound(c)
        Debug.Print "c(" & i & ") = " & c(i)  'Outputs results in Immediate Window (CTRL G)
      Next i
    
    End Sub
    Additional tips:
    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Debugger Secrets:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    I hope this helps.

    Lewis

+ 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. Replies: 1
    Last Post: 10-03-2013, 03:58 PM
  2. Replies: 7
    Last Post: 12-16-2011, 02:41 PM
  3. Log in web querry
    By chris85 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-12-2011, 04:09 PM
  4. Symbols for General Number and General Letter?
    By mwgr5 in forum Excel General
    Replies: 9
    Last Post: 08-07-2008, 12:15 PM
  5. MS Querry for Excel
    By amish in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-12-2005, 12:06 AM

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