+ Reply to Thread
Results 1 to 12 of 12

VBA Centre Across Selection Help!

  1. #1
    Registered User
    Join Date
    02-13-2014
    Location
    happy
    MS-Off Ver
    Excel 2010
    Posts
    6

    VBA Centre Across Selection Help!

    Hi,

    I have created a system where users can book pcs for people but i would like to add to my vba coding so when a booking is made, it is automatically set to Centre Across Selection for the text, i have tried " .HorizontalAlignment = xlCenterAcrossSelection" and it kind of worked in the sence that the text was centered but only accross the the first cell in the selection.

    Any reasons or advice?

    Many thanks,

    Nick.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA Centre Across Selection Help!

    Syntax is:
    Please Login or Register  to view this content.
    How are you trying to do it?
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    02-13-2014
    Location
    happy
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA Centre Across Selection Help!

    Ok, I will try this, does this go at the beggining or end? thanks

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: VBA Centre Across Selection Help!

    Hi, Nick,

    make sure that there is only text in one of the cells (most favourable the most left handed one) otherwise the centering will only be done within each single cell.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA Centre Across Selection Help!

    Quote Originally Posted by Happy Booking View Post
    Ok, I will try this, does this go at the beggining or end? thanks
    Beginning or end of what?

    You've not shared either your workbook or your code, so I have no idea what you are already doing...!

  6. #6
    Registered User
    Join Date
    02-13-2014
    Location
    happy
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA Centre Across Selection Help!

    ok heres my current code:
    Sub Booking()
    'Make red
    Selection.Interior.ColorIndex = 33
    'Make Bold
    Selection.Font.Bold = True

    End Sub

    Its not much but it works, to this where would i add the following:
    Range("A1:E1").HorizontalAlignment = xlCenterAcrossSelection

    Many thanks,
    Nick

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA Centre Across Selection Help!

    What is your selection? A single cell, or a range of cells? Do you know? Does it matter?

    Which range of cells do you want to center across? Whatever is selected? A fixed number of columns to the right? All the empty cells until the next value is reached?

    What do you want to do if there is already content in those cells? Ignore it, delete it?

    Your requirement is too vague to give you an answer.

  8. #8
    Registered User
    Join Date
    02-13-2014
    Location
    happy
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA Centre Across Selection Help!

    I want a user to be able to drag accross from left to right (the number of cells will always vary) and then make that booking, this bit is fine but the next bit, after the booking has been made i want it so the user can double click anywhere in the booking and type a name that will be centered across the selection.

    Thanks

  9. #9
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA Centre Across Selection Help!

    Okay - to achieve your requirement of double clicking the range, and entering a label which is centered, you actually need to merge the cells, not just center across selection.

    So try:
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    02-13-2014
    Location
    happy
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA Centre Across Selection Help!

    Ok, That wont work in a shared workbook though will it?

  11. #11
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA Centre Across Selection Help!

    No.

    Do you know how frustrating it is to get information about your requirements drip fed, bit by bit?

  12. #12
    Registered User
    Join Date
    02-13-2014
    Location
    happy
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA Centre Across Selection Help!

    I'm sorry, is there anyway of fitting my requirments while being able to share the workbook? or near enough hitting my requirments and being able to share?

+ 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. Centre across selection - strange behaviour when wrapping.
    By talksalot81 in forum Excel General
    Replies: 0
    Last Post: 04-27-2010, 12:34 PM
  2. centre across selection shortcut
    By Martin B in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-17-2006, 04:15 PM
  3. [SOLVED] How do I get "centre across selection" to centre properly?
    By Marjon in forum Excel General
    Replies: 5
    Last Post: 03-16-2006, 08:24 PM
  4. Icon for "Wrap" and for "Centre Across Selection"
    By BeSmart in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-16-2005, 02:50 PM
  5. Replies: 3
    Last Post: 09-27-2005, 05:05 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