Consulting

Results 1 to 4 of 4

Thread: Insert empty columns

  1. #1

    Insert empty columns

    Hi all.
    I have tables in Excel.
    The names of the columns are on a separate row, such as: VA001, VA002, VA003 ... VA038, etc.
    But there are cases where some names are missing, for example VA001, VA002 and then VA004.
    Different ways in different tables.
    I need to add empty columns in place of each missing number.
    I have a code that does everything perfectly.
    But the code works when the range of column names starts from the first column.
    And I have a range starting from the fourth or fifth column, it does'n matter.
    Example in attachment.
    Please. Help me find a solution. Thanks in advance.
    PS. Sorry for my english. I hope that everything is clear.
    Attached Files Attached Files
    • File Type: xls 1.xls (37.5 KB, 10 views)

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    [VBA]Sub snb()
    sn = Cells(1).CurrentRegion.Rows(1)

    For j = UBound(sp, 2) To 1 Step -1
    Columns(Val(Mid(sp(1, j), 3))) = Columns(j).Value
    Columns(j).ClearContents
    Next
    End Sub[/VBA]

  3. #3
    Thanks for your reply, but it desn't solve the problem.
    At runtime in any situation, I've got an error - "Run-time error 13. Type mismatch"
    Please, tell me what to fix to make it work?

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You had to remove the typo first.
    After analysing this utterly simple code you could have fixed it yourself easily.

    [VBA]
    Sub snb()
    sn = Cells(1).CurrentRegion.Rows(1)

    For j = UBound(sn, 2) To 1 Step -1
    Columns(Val(Mid(sn(1, j), 3))) = Columns(j).Value
    Columns(j).ClearContents
    Next
    End Sub
    [/VBA]

Posting Permissions

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