+ Reply to Thread
Results 1 to 6 of 6

Selecting and editing a range of cells using VBA

  1. #1
    Registered User
    Join Date
    02-01-2019
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    2

    Selecting and editing a range of cells using VBA

    Hi i'm developing a Macro that formats a report, the number of columns and rows used in the document that will be formatted is variable so I can't use fixed ranges. What I need to achieve is to select the first row in column A that doesn't contain data at the bottom of the report and select a range as wide as the columns that have been used. e.g. if there is data 100 rows down and as wide as column E, I need the code to select from a101 to e101 and then:

    Merge the cells
    set the borders
    Wrap text
    fill in a fixed amount of text
    adjust the height of the row to fit the text in the newly merged cell

    I have been able to get to the last row and I have code to select to the last column when I fix the row, I just cant seem to combine them. Any assistance would be appreciated

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,461

    Re: Selecting and editing a range of cells using VBA

    Post the code you have and ...

    Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,461

    Re: Selecting and editing a range of cells using VBA

    Note: you do NOT have to select the cells in order to manipulate them.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Selecting and editing a range of cells using VBA

    Hi,

    Why not define and then use a dynamic range name that automatically adjusts. i.e.

    myrangename =

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    If there's stuff above A100 in column A then you'll need to adjust the COUNTA(A:A), i.e. COUNTA(A:A)-COUNTA(A1:A99)
    and ditto with the number of labels on row 100.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,743

    Re: Selecting and editing a range of cells using VBA

    Did You try use 'Developer/Record macro'?
    Do what You exactly describe:

    Merge the cells
    set the borders
    Wrap text
    fill in a fixed amount of text
    adjust the height of the row to fit the text in the newly merged cell

    stop recording ('Developer/Stop recording'), then go to VBA editor (Alt+F11) and move this recorded macro into Your existing macro which getting last row,, edit cells addresses and job is done.
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  6. #6
    Registered User
    Join Date
    02-01-2019
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    2

    Re: Selecting and editing a range of cells using VBA

    I managed to solve the issue but thought I would post the code here


    Private Sub Create_Disclaimer()

    Dim LastCol As Long
    Dim LastRow As Long

    With ActiveSheet
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Row
    LastCol = .Cells(12, .Columns.Count).End(xlToLeft).Column
    End With

    With ActiveSheet.Range(Cells(LastRow, 1), Cells(LastRow, LastCol))

    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .Name = "Calibri"
    .Merge
    .BorderAround Weight:=xlThin
    .RowHeight = 150
    .WrapText = True
    .Font.Size = 7
    .Value = "Text I wanted to insert"
    End With

    End Sub


    Thanks everyone for your input I got there in the end.
    Last edited by Maypop; 02-05-2019 at 07:27 AM.

+ 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. [SOLVED] Selecting 3 ODD and 3 Evven numbers when randomly selecting from a range of cells
    By UltimateNeo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-23-2019, 05:23 PM
  2. Format a range (table row) based on the value in one of its cells right after the editing
    By ilya Excel Forum in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2017, 08:15 AM
  3. Replies: 2
    Last Post: 09-14-2016, 09:55 AM
  4. Editing Macro To Exclude Range of Cells
    By sborah99 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-21-2015, 09:03 PM
  5. [SOLVED] Editing all Cells in a selected range.
    By Joachimjr in forum Excel General
    Replies: 9
    Last Post: 07-09-2014, 08:24 AM
  6. Replies: 4
    Last Post: 01-31-2012, 01:08 PM
  7. Selecting and editing unlocked cells problem
    By John Keith in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-09-2005, 05:32 PM

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