Consulting

Results 1 to 5 of 5

Thread: Solved: find the letter for the last colomn

  1. #1
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location

    Solved: find the letter for the last colomn

    hello

    it is an possibility to find the letter of the last column used? (for example)

    [vba]
    "Sheet1!A2:H" & xlLastRow("Sheet1")
    'I like to change "H" with the last "column" used
    [/vba] xlLastcolumn("Sheet1") will return an number how can I transform this one in letter an then the letter to be put't in "Sheet1!A2:H" & xlLastRow("Sheet1")?

    thx

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    instead of A1 notation, use rows/columns:
    [vba]Range(sheet1!.cells(2,1),sheet1!.cells(xlLastRow("Sheet1"),xlLastColumn("Sh eet1"))[/vba]
    You could extract the letter as you suggest with:
    [vba]ColumnLetter = Split(Cells(1, xlLastColumn("Sheet1")).Address, "$")(1)
    Sheet1!A2:" & ColumnLetter & xlLastRow("Sheet1").select[/vba] but I don't see it as required.

    Oops, I 've just realised this could be on a sheet, in which case:

    OFFSET(A2,0,0,xlLastRow-ROW(A2)+1,xlLastColumn-COLUMN(A2)+1)

    (I've substituted shortened versions of lastrow/column)

    Or write yourself a little function to convert column numbers to column letters:
    [VBA]Function ColLetter(ColNumber)
    ColLetter = Split(Cells(1, ColNumber).Address, "$")(1)
    End Function[/VBA]
    then use
    "Sheet1!A2:" & ColLetter(xlLastColumn("Sheet1")) & xlLastRow("Sheet1")
    Last edited by p45cal; 04-20-2010 at 08:55 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    thx Pascal

    the idea is to populate an list view an I have like this:
    [VBA]
    Private Sub UserForm_Activate()

    Dim cell As Range
    Dim i As Long
    Dim li As ListItem
    On Error GoTo Eroare:

    With Me.ListView1
    .ColumnHeaders.Add , , "Name", Me.TextBox1.Width 'Add columns
    .HideColumnHeaders = False 'set some properties
    .View = lvwReport
    .Gridlines = True

    For Each cell In Range("B2:F2").Value
    .ListItems.Add , , cell
    Next cell

    ListView1_ItemClick .ListItems(.SelectedItem.Index) 'fill edit controls
    End With
    Eroare:
    'MsgBox "error"

    End Sub
    [/VBA]

    And I like to change Range("B2:F2") specially the last one "F2" to an variable. the problem is that I do not know how. like you put't is not going

  4. #4
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    [VBA]
    Private Sub UserForm_Activate()
    Dim LR As Long, LC As Long
    With Sheets("Sheet1")
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With

    Dim cell As Range
    Dim i As Long
    Dim li As ListItem
    On Error GoTo Eroare:

    With Me.ListView1
    .ColumnHeaders.Add , , "Name", Me.TextBox2.Width 'Add columns
    '.ColumnHeaders.Add , , "Density", Me.TextBox1.Width
    .HideColumnHeaders = False 'set some properties
    .View = lvwReport
    .Gridlines = True
    'Range("A2:G2")
    For Each cell In Range("A2", Cells(LR, LC))
    .ListItems.Add , , cell
    Next cell

    ListView1_ItemClick .ListItems(.SelectedItem.Index) 'fill edit controls
    End With

    Eroare:
    'MsgBox "error"
    End Sub

    [/VBA]

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    see if you can tweak this proof of concept:
    [VBA]Sub blah()
    Dim Cell As Range
    LastCol = 4
    LastRow = 5
    'Range(Cells(2, 2), Cells(LastRow, LastCol)).Select 'just to check
    For Each Cell In Range(Cells(2, 2), Cells(LastRow, LastCol)) '.Value 'do you need '.value'? (you've defined Cell as a range)
    '.ListItems.Add , , cell
    Next Cell
    End Sub
    [/VBA]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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