+ Reply to Thread
Results 1 to 10 of 10

How to find lastRow for variable columns?

Hybrid View

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    How to find lastRow for variable columns?

    Hello

    I believe that it is possible.But can't figure it out.

    I have a userform with 2 commandbuttons "English list" and "Spainish List"
    On the worksheet I have
    An English List of Hospitals in Column A then in Column B it is in Spainish
    An English List of Airlines in Column C then in Column D it is in Spainish
    An English List of Banks in Column E then in Column F it is in Spainish

    and so on for other lists

    I want to click on English List CommandButton and I enter in the add new textbox and if I check the bank then the textbox values should be entered at the last available row of the relevent list in the relevent language,

    I know this is a full project but I am not requiring fully prepared application . I just want some help on how can I switch the lists last row.

    Please lemme know if you have any questions.

    Best Regards
    Imran Bhatti
    Attached Files Attached Files
    Teach me Excel VBA

  2. #2
    Registered User
    Join Date
    11-15-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: How to find lastRow for variable columns?

    The code below will give you the last used row in the specified column

    "sheetname" is the worksheet in which the data is stored
    "your column" will be A for english or B for spanish

    LastRow = Sheets("sheetname").Range("your column" & Rows.Count).End(xlUp).Row

  3. #3
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to find lastRow for variable columns?

    Hi Anand ji

    Thanks for reply
    Problem is that my column will be a variable
    May be A if I click the English List button and check Hospital checkbox
    Maybe D column ,if I click spainsh List button and check Airline checkbox

    LastRow = Sheets("sheetname").Range("your column" & Rows.Count).End(xlUp).Row
    The red one will be a variable depending on the English/spainish commandbuttons and the checkboxes.

  4. #4
    Registered User
    Join Date
    11-15-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: How to find lastRow for variable columns?

    I have missed the exclamation marks for the column number. The code should be

    Dim sColNo As String
        If cbhospital = True Then sColNo = "D"
        If cbairline = True Then sColNo = "E"
        If cbbank = True Then sColNo = "F"

  5. #5
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: How to find lastRow for variable columns?

    I usually use:
    Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
    to establish the last row in a column (A in this case). But since they are lists with no blanks you could use:
    lngRow = Application.WorksheetFunction.CountA(Worksheets(1).Columns(1)
    You then just call the routine to reset the last row triggered by pressing the English/Spanish buttons. You also need to call it when the checkboxes change. I would also name the ranges something like "English_Hospital", "Spanish_Hospital" etc and reference them explicitly in the code.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  6. #6
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to find lastRow for variable columns?

    Let me make it little bit simple

    Suppose there is an English List for Hospitals and Spanish List of Hospitals
    If I click on English List commandbutton and also check the Hospital checkbox then I need to determine the last row of the English List in Column A
    But
    If I click on Spanish Commandbutton and also check the Hospital button then I need to determine the last row of the Spanish List in B column.
    Hope this makes sense.

  7. #7
    Registered User
    Join Date
    11-15-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: How to find lastRow for variable columns?

    You can use a variable for defining the column number. A sample of the code is given below

    Private Sub btnEnglish_Click() 
         
        Dim sColNo As String
        If cbhospital = True Then sColNo = D
        If cbairline = True Then sColNo = E
        If cbbank = True Then sColNo = F
     
    End Sub
    Then your code for the last row will be

    LastRow = Sheets("sheetname").Range(sColNo & Rows.Count).End(xlUp).Row

  8. #8
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to find lastRow for variable columns?

    Thank you Anand ji it worked

    Dhanaywaad.(Thanks)

  9. #9
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: How to find lastRow for variable columns?

    Rather than hard-coding the column letters ... keep the text of the headers consistent and call a function to search row 1, match on the header/s you need, and return the column letter / number.

  10. #10
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to find lastRow for variable columns?

    Tnanks Matrix
    Unfortunately I am weak in creating functions and Class moduals.

+ 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. R1C1 lastRow not lastRow when data combined
    By Spyderwoman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2016, 05:20 PM
  2. [SOLVED] Find the LastRow
    By SIMBAtheCAT in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-22-2014, 11:45 AM
  3. [SOLVED] VBA Find LastRow Of Multiple Non Contiguous Columns On One SHeet
    By hobbiton73 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-08-2014, 01:20 PM
  4. Keeping a lastrow Variable Across Modules
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-12-2014, 09:40 AM
  5. [SOLVED] Incorporate the value of a LastRow variable in a VBA code/formula
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-03-2012, 11:05 PM
  6. lastrow of 3 columns
    By Directlinq in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-19-2009, 10:35 AM
  7. Chart how to find lastrow?
    By Craigm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-06-2006, 11:19 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