+ Reply to Thread
Results 1 to 9 of 9

Macro to create columns by referencing a variable column

  1. #1
    Registered User
    Join Date
    09-12-2010
    Location
    Saratoga, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Macro to create columns by referencing a variable column

    I've only done a small amount of macros with VBA.

    I want to create a macro to insert 12 cells left of a column labeled "This Year"
    As the spreadsheet grows (by 12 columns @ year) the "This Year" column moves to the right. Thus I need to reference the range off of that column and then insert 12 columns directly to the left of it each year. Can I somehow reference the label "This Year"?

    I then need to enter the month labels in the new columns row 8.

    My problem is trying to reference off the "This Year" column.

    Can someone help me out with this?

    Thank you!!!
    Mike

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Macro to create columns by referencing a variable column

    This code will give you the basic idea about how to locate the This Year Header column.

    I used Row 1 as search range. you can replace the 1:1 to your desired row

    Please Login or Register  to view this content.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Registered User
    Join Date
    09-12-2010
    Location
    Saratoga, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to create columns by referencing a variable column

    Okay I tried this:

    Please Login or Register  to view this content.
    It did insert a column - except in inserted it left of column #8, while the label "This Year" is in column 17.

    What am I doing wrong?

    Thanks,
    Mike

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Macro to create columns by referencing a variable column

    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Macro to create columns by referencing a variable column

    This macro inserts 12 columns to the left of "This Year" cell.

    Sub Macro()
    Dim Frng As Range
    Set Frng = Cells.Find("This year")
    Range(Frng, Frng.Offset(0, -12)).EntireColumn.Insert
    End Sub

  6. #6
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Macro to create columns by referencing a variable column

    kvsrinivasamurthy

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  7. #7
    Registered User
    Join Date
    09-12-2010
    Location
    Saratoga, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to create columns by referencing a variable column

    Quote Originally Posted by kvsrinivasamurthy View Post
    This macro inserts 12 columns to the left of "This Year" cell.

    Please Login or Register  to view this content.
    Thank you for taking the time to reply.

    I used your code, and it does insert columns (I changed the -12 to 11, as it was inserting 13 columns), however the columns are inserted about 8 columns left of the "This Year" column. I'm not sure what controls the placement.

    I'll attach a sample file with the macro.

    Thanks for taking a look at this again,
    Mike
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    09-12-2010
    Location
    Saratoga, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to create columns by referencing a variable column

    I've played with this some more and found it I set the Offset to (, -2), 3 columns are inserted directly to the left of "This Year"
    If I set the Offset to (, -3), 4 columns are inserted beginning with the second column to the left. (Skips one over)
    As I add to the column offset, more columns are skipped and then the columns are inserted.

    What would cause the offset code to skip over several columns before inserting?

    Thanks again,
    Mike

  9. #9
    Registered User
    Join Date
    09-12-2010
    Location
    Saratoga, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to create columns by referencing a variable column

    Problem solved with this code:

    Please Login or Register  to view this content.
    Now I just have to figure out how to have the month labels abbreviated (Jan, Feb, Mar) etc . ..

+ 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. Need a macro to format data and delete columns and create new column
    By aacod in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-12-2013, 12:15 PM
  2. Need a macro to format data and delete columns and create new column
    By aacod in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-12-2013, 12:14 PM
  3. Referencing column as a variable using Range?
    By MikeSD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2012, 07:22 PM
  4. Replies: 2
    Last Post: 10-07-2011, 12:41 AM
  5. [SOLVED] Referencing a Value in variable length columns.
    By Kevin in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 01-28-2006, 07:01 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