+ Reply to Thread
Results 1 to 4 of 4

Please show me how to automatically add a blank column before each table

Hybrid View

  1. #1
    Registered User
    Join Date
    12-25-2010
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    1

    Exclamation Please show me how to automatically add a blank column before each table

    Hi everybody,

    I have a task which needs automation with use of macro and I have written code to do it; however, my code could not do the task successfully. Therefore, I need your help now.

    I attached a sample file, including 3 sheets: “start_1”, “start_2” and “result”. “start_1” and “start_2” are identical. I need to format tables in these two sheets “start” so that they look like exactly the “result” sheet.

    In detail, here are tasks which need automating:
    - Add 1 blank row at top (I have done it)
    - Freeze title rows (I have done it)
    - This is what I could not do: Add 1 blank column before each table. The added column must have no fill color and no border at all. (Each table here is identified by merged cells in the top title row)
    - Also please show me how to determine the last column with data and the last row with data so that the blank outer space is hidden.

    Thank you very much for your help.

    Below is my code (in ThisWorkbook module):
    Option Explicit
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim bScrUpdate As Boolean
    Dim ws As Worksheet
    Dim rng As Range
    
    Application.EnableCancelKey = xlDisabled 'disable ESC key
    bScrUpdate = Application.ScreenUpdating
    If bScrUpdate = True Then Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "result" Then
            ws.Select
            Rows("1:1").Insert Shift:=xlDown
            
            Range("A4").Select
            ActiveWindow.FreezePanes = True
            
            For Each rng In Rows("2:2").Cells
                If rng.MergeCells Then
                    rng.MergeArea.Cells(1, 1).Select
                    Selection.Offset(-1, 1).EntireColumn.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                End If
            Next rng
        End If
    Next ws
    
    Application.DisplayAlerts = True
    If Not Application.ScreenUpdating = bScrUpdate _
        Then Application.ScreenUpdating = bScrUpdate
    Application.EnableCancelKey = xlInterrupt 'enable ESC key
    End Sub
    Attached Files Attached Files
    Last edited by vijnanamatrata; 07-02-2013 at 01:46 AM.

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Please show me how to automatically add a blank column before each table

    Hi vijnanamatrata!

    Please avoid using Cross-Post or provide us the link, so that we dont have to work on a solved (if already) query..

    http://chandoo.org/forums/topic/plea...ore-each-table
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  3. #3
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Please show me how to automatically add a blank column before each table

    The problem your have, is you work with merged cells.

    to add an column A you have to choose an NON merged cell e.g. A2 and add the column.

     Range("A2").EntireColumn.Insert
    This code you can implement in your own code.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Please show me how to automatically add a blank column before each table

    Does this help?

    Sub vijnanamatrata()
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "result" Then ws.Activate
    Dim i As Long
    For i = ActiveSheet.UsedRange.Columns.Count + 1 To 1 Step -1
    Cells(2, ActiveSheet.UsedRange.Columns.Count + 3).EntireColumn.Delete xlToLeft
        If Cells(2, i).Value = "Top" Then
            Cells(2, i).EntireColumn.Insert xlToRight
            Cells(2, i).EntireColumn.Clear
        End If
    Next i
    Rows(1).Insert xlDown
    Next ws
    Application.ScreenUpdating = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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