+ Reply to Thread
Results 1 to 5 of 5

Macro to copy data under appropriate heading

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    72

    Macro to copy data under appropriate heading

    Hi,

    I've attached a spreadsheet of what I'm working on. I have 2 tabs, "Sheet 1" (which is where I want to execute the macro) and "How it should look like" (after the macro has been executed).

    I'm trying to create a macro that copies range A18:A20 under the appropriate heading date in "Sheet1". For example, if the month is September 2013, I want it to copy the cells into C2:C3.

    You might ask why I'm not using a lookup function. The reason is that this is only a small scale example of what I'm working on. At the start of every new month, cell A17 is going to change, but I still want to be able to have previous months' records. So for example, in October, cell A17 will be October 2013, but I still want to keep September's values. Like I said, this is a really simplified example of what I'm working on.

    The script I'm working on is shown below. I've just started playing around with VBA a few days ago, so this is all sort of new to me. Any assistance as to what I'm doing wrong would be appreciated.
    Sub CopyPaste()
    Dim I As Integer
    I = 1
    If Cells(1, I).Value <> Range("A17").Value Then
    I = I + 1
    ElseIf Cells(1, I).Value = Range("A17").Value Then
    Range("A18:A20").Copy
    Cells(2, I).PasteSpecial
    End If
    End Sub
    Thanks

    Sample.xlsx
    Last edited by anthony_91; 08-02-2013 at 08:38 PM.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to copy data under appropriate heading

    Bit long but
    Range(Cells(Rows.Count, col).End(xlUp).Offset(1, WorksheetFunction.Match(Range("A17"), Range("A1:L1"), 0)), Cells(Rows.Count, col).End(xlUp).Offset(3, WorksheetFunction.Match(Range("A17"), Range("A1:L1"), 0))).Value = Range("A18:A20").Value

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Macro to copy data under appropriate heading

    anthony_91,

    Thanks for the workbook.

    Here is an update to your macro with code line explanations:

    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    
    Option Explicit
    Sub CopyPasteV2()
    ' stanleydgromjr, 08/01/2013
    ' http://www.excelforum.com/excel-programming-vba-macros/944072-macro-to-copy-data-under-appropriate-heading.html
    '
    ' instead of using Integer, try using Long
    ' lc is used to find the laste used column in row 1
    
    
    ' i is a loop counter
    Dim lc As Long, i As Long
    
    
    With Sheets("Sheet1")
    
    
      ' lc is used to find the last used column in row 1
      lc = .Cells(1, Columns.Count).End(xlToLeft).Column
     
     
      ' loop from column A = 1, to the last column in row 1
      '   Step 1 says to increment i by 1 each loop
      For i = 1 To lc Step 1
    
      
        '  if the values are not equal 'do nothing
        If Cells(1, i).Value <> Range("A17").Value Then
          'do nothing
    
          
        '  if the valuse are equal, then copy and PasteSpeical
        ElseIf Cells(1, i).Value = Range("A17").Value Then
          .Range("A18:A20").Copy
          .Cells(2, i).PasteSpecial
    
          
          '  after copy and paste
          '    stop looping, and, exit the Sub
          Exit Sub
    
    
        End If
      Next i
    End With
    End Sub
    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the CopyPasteV2 macro.



    The following macro is much faster because it does not loop thru the columns in row 1, one column at a time, and, it does some error checking.

    Range("A18:A20") does not contain formulae.

    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    
    Option Explicit
    Sub CopyPasteV3()
    ' stanleydgromjr, 08/01/2013
    ' http://www.excelforum.com/excel-programming-vba-macros/944072-macro-to-copy-data-under-appropriate-heading.html
    Dim fc As Long
    Application.ScreenUpdating = False
    With Sheets("Sheet1")
      fc = 0
      On Error Resume Next
      fc = Application.Match(.Cells(17, 1), .Rows(1), 0)
      On Error GoTo 0
      If fc = 0 Then
        Application.ScreenUpdating = True
        MsgBox "The date in cell A17 can not be found in row 1 - macro terminated!!!!!"
        Exit Sub
      ElseIf fc > 0 Then
        .Range("A18:A20").Copy Destination:=.Cells(2, fc)
      End If
    End With
    Application.ScreenUpdating = True
    End Sub
    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the CopyPasteV3 macro.
    Last edited by stanleydgromjr; 08-01-2013 at 10:50 AM.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  4. #4
    Registered User
    Join Date
    02-21-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    72

    Re: Macro to copy data under appropriate heading

    Quote Originally Posted by stanleydgromjr View Post
    anthony_91,

    Thanks for the workbook.

    Here is an update to your macro with code line explanations:

    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the CopyPasteV3 macro.
    Thank you so much. That worked perfectly.

  5. #5
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Macro to copy data under appropriate heading

    anthony_91,

    You are very welcome. Glad I could help.

    Thanks for the feedback.

    And, come back anytime.

+ 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. macro help with Data list sorted by date then add a month Heading in between the sections
    By Researcher007 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-27-2013, 03:57 PM
  2. Macro to filter based on column heading then copy and paste to new sheet
    By macattackr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-01-2012, 05:14 PM
  3. Code Problem Macro to copy specific cell based on the heading it is next to.
    By Mush1138 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2011, 12:07 PM
  4. macro to find text heading then select underlying data?
    By excelRookie06 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-07-2011, 12:31 PM
  5. Copy data from different files into 1 workbook under relevant column heading.
    By novice81 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2009, 01:19 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