Consulting

Results 1 to 7 of 7

Thread: Find Next Column

  1. #1
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location

    Find Next Column

    Hi All

    The Code below works OK and puts the info in the correct cell - my problem is I don't want it to go in the same cells every time I need it to find the next empty column always on row 1.

    [VBA]
    Private Sub Add_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("DATA")
    ws.Cells(1, 10).Value = Tb5.Value
    Sheets("DATA").Select
    Range("J1:K1").Select
    With Selection
    .HorizontalAlignment = xlCenterAcrossSelection
    End With
    End Sub
    [/VBA]

    Regards

    Sooty8

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Add_Click()
    Dim iRow As Long
    Dim LastCol As Long
    Dim ws As Worksheet
    Set ws = Worksheets("DATA")
    With ws

    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column)
    .Cells(1, lastCol).Value = Tb5.Value
    .Range("J1:K1").HorizontalAlignment = xlCenterAcrossSelection
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location
    Hi Xld

    Need help getting a syntax error message on the line in Red Below.

    Private Sub Add_Click()
    Dim iRow As Long
    Dim LastCol As Long
    Dim ws As Worksheet
    Set ws = Worksheets("DATA")
    With ws

    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column)
    .Cells(1, lastCol).Value = Tb5.Value
    .Range("J1:K1").HorizontalAlignment = xlCenterAcrossSelection
    End With
    End Sub

    Regards

    Sooty8

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Try it w/o the ) at the end of that line.
    Peace of mind is found in some of the strangest places.

  5. #5
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location
    Hi Austenr


    Tried all different ways of inserting your comment in the line

    [VBA]lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column)
    [/VBA]
    No joy I'm afraid

    Sooty 8

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Nothing like an errant space or two to blow you out of the water
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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