+ Reply to Thread
Results 1 to 4 of 4

Having a macro select a column

Hybrid View

  1. #1
    Registered User
    Join Date
    11-16-2010
    Location
    Sacramento, CA
    MS-Off Ver
    Excel 2007
    Posts
    2

    Having a macro select a column

    I want to write a macro which will select a particular column.

    If I have Jan - Dec in row 1, and I want to find the column which has, say, Oct. I can write the macro to do that, but I don't know how to select Col J, which is the Col with Oct in it.

    Simply put, this is what I want to do.

    Target Month = sheet 1, cell A1
    Select Sheet 2, A1. If = Target month then Select A:A, copy/paste special/values, then end sub, if <> target month, move 1 col to right.


    Can someone help?

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Having a macro select a column

    Hello,

    Selecting all of column J would be done this way
    Sheets("sheet1").Range("J:J").Select
    Target Month = sheet 1, cell A1
    Select Sheet 2, A1. If = Target month then Select A:A, copy/paste special/values, then end sub, if <> target month, move 1 col to right.
    What?!? Simple terms friend.

  3. #3
    Registered User
    Join Date
    11-16-2010
    Location
    Sacramento, CA
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Having a macro select a column

    Thanks.. but selecting J:J is just an example.. the target month will change every month. I will update the target month on sheet 1, then run the macro. I want the macro to 'read' the target month, find it in row one and Sheet 2 and when it finds it, select that column, and c/ps/values. Thanks for the help

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Having a macro select a column

    This should work for you. You did not say where you wanted the copied column to go so I arbitrarily set it to sheet 1 range R1. This can be changed to meet your needs. Just so you know this macro will search the values in sheet 2 row 1. If any of those values match the value on sheet 1 range A1 then it will copy that entire column and put it in sheet 1 column R. Let me know if this works for you.
    Option Explicit
    
    Sub Macro1()
    Dim c As Variant
    
    For Each c In Sheets("Sheet2").Range("1:1")
    
    If c.Value = Sheets("Sheet1").Range("A1").Value Then
        c.EntireColumn.Copy Destination:=Sheets("Sheet1").Range("R1")
    End If
    Next c
    
    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