Consulting

Results 1 to 8 of 8

Thread: Simple VBA code to calculate ColumnWidth

  1. #1

    Simple VBA code to calculate ColumnWidth

    Dear all,

    I have just begun VBA coding and require some help.

    In my spreadsheet, B2, C2, D2 are merged. The rest of the cells are not.

    A B C D E
    1
    2

    I would like to write a simple VBA code that would calculate the total width of the merged cells. As of now, I am using:
    MsgBox Columns("B").ColumnWidth + _
    Columns("C").ColumnWidth + _
    Columns("D").ColumnWidth + _

    But it would be a problem should the number of merged cells increase. Is there any other VBA codes that could handle this calculation?

    A huge thank you in advance.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Avoid merged cells in VBA.

  3. #3
    Dear snb,

    Yes, I avoid merged cells as much as possible. However, I am working with a template from my supervisor and it is not convenient to make any changes.

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    first, try convincing your boss to not use merged cells...

    if you fail try below

    change A1 to what you see in address bar when you select merged area.

    Sub vbax_53185_merged_area_column_width()
        
        Dim cll As Range
        Dim MrgdColWidth As Double
        
        For Each cll In Range("A1").MergeArea
            MrgdColWidth = MrgdColWidth + cll.ColumnWidth
        Next
        
        MsgBox MrgdColWidth
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    Wow mancubus, that worked perfect. Thank you so much. Could you briefly explain the code, I would very much like to learn how it is done.

    Once again, thanks for your help.

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.

    it loops all the cells in merge area and adds their columwidths to a variable.


    when you declare a numeric variable (such as MrgsColWidth As Double in the code i posted), its value is 0 before assigning any value.

    in the first loop, MrgdColWidth = MrgdColWidth + cll.ColumnWidth >> MrgdColWidth = (0) + A1's ColumnWidth
    in the second loop, MrgdColWidth = MrgdColWidth + cll.ColumnWidth >> MrgdColWidth = (0 + A1's ColumnWidth) + B1's ColumnWidth
    ...
    ...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    Dear mancubus,

    Thanks for your explanation on the loop, I understand it now (:

    Have a great day mancubus, your help has been invaluable.

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    please mark the thread as Solved from Thread Tools for future references...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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