+ Reply to Thread
Results 1 to 24 of 24

Macro to create a looping macro to populate another worksheet.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-05-2004
    MS-Off Ver
    Office 365
    Posts
    651

    Re: Macro to create a looping macro to populate another worksheet.

    This is looking good. Let ask if this is working the following way as I don’t understand the code.

    On the Inputs page:
    Start Date using the calendar option. LOVE IT!
    End Date looks like it auto populates to the last line in the Settlement Prices sheet? I tried to change it to 6/10/16 and when I ran the macro it still showed 6/15/16.
    When you click the process button
    Does it place the first date in cell B4 on Inputs? I need that to happen. Also at that time can you note where in the code where that happens so I know where to CALL MACRO2.

    Next it will go to the Output page:
    And copy K2:M2 to the next available line in A:C. (your will see I changed the data1 and data2 lines to be “0” as I just need it to copy whats in K2:M2). Once I know where to place my CALL MACRO2 IT will give the math that will be fed into K2 and M2.
    I might have confused everyone but showing the data. Perhaps I should have JUST shown the dates.

    This will complete what i need to be able to finish the program.
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to create a looping macro to populate another worksheet.

    Hi sungen99

    Would you WISH to choose the End Date? Currently the Code defaults to the last available date.

    Here's the Workbook modified to allow you to choose the Start Date AND the End Date.
    Attached Files Attached Files
    Last edited by jaslake; 06-21-2016 at 03:36 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to create a looping macro to populate another worksheet.

    Hi sungen99

    I've annotated the Code...let me know what you do not understand.

    Option Explicit
    Sub Rectangle1_Click()
       Dim ws           As Worksheet
       Dim ws1          As Worksheet
       Dim ws2          As Worksheet
       Dim LR           As Long
       Dim NR           As Long
       Dim StartRow     As Long
       Dim Rng          As Range
       Dim cel          As Range
    
       Set ws = Sheets("Settlement Prices")
       Set ws1 = Sheets("Output")
       Set ws2 = Sheets("Inputs")
    
       If ws2.Cells(1, "B").Value > ws2.Cells(2, "B").Value Then
          MsgBox "Start Date is Greater than End Date"
          Exit Sub
       End If
    
    
       'Clear Output Sheet except for headers
       With ws1 'Sheets Output
          .UsedRange.Offset(2, 0).ClearContents
          NR = 3
       End With
    
       With ws  'Sheets Settlement Prices
          LR = .Range("A" & .Rows.Count).End(xlUp).Row
    
          Set Rng = .Range("A4:A" & LR)
    
          'Search for the Start Date in Settlement Prices Column A
          'and capture it's Row Number (StartRow)
          StartRow = Rng.Find(ws2.Cells(1, "B").Text, , xlValues, xlWhole, xlByRows, xlNext, False).Row
          For Each cel In .Range(.Cells(StartRow, "A"), .Cells(LR, "A"))
             Application.EnableEvents = False
             
             'Copy that Date (cel.text) to inputs B4
             ws2.Cells(4, "B").Value = cel.Text
             
             'Copy that Date (cel.Value) to Outputs K2
             ws1.Range("K2").Value = cel.Value
             
             'Copy that Date (cel.Value) to Outputs L2
             ws1.Range("L2").Value = cel.Offset(0, 13).Value
             
             'Copy that Date (cel.Value) to Outputs M2
             ws1.Range("M2").Value = cel.Offset(0, 8).Value
             
             'Copy Outputs Range("K2:M2").Value to the next available row
             'in Outputs Colimn A
             ws1.Cells(NR, "A").Resize(1, 3).Value = ws1.Range("K2:M2").Value
             NR = NR + 1
             Application.EnableEvents = True
             
          'Move on to the next Cell In .Range(.Cells(StartRow, "A"), .Cells(LR, "A"))
          'and repeat the process until the last row
          Next cel
       End With
    End Sub
    Attached Files Attached Files

+ 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. How to create Macro that will auto populate the next 7 days
    By Gryphon87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-30-2015, 08:32 PM
  2. Create a looping macro that stops at pre determined row?
    By trade in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2015, 04:29 PM
  3. [SOLVED] Looping Worksheet Macro - Skip a Sheet
    By tudley in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-03-2015, 12:32 AM
  4. [SOLVED] Multiiple Worksheet Loop Macro only Looping on 1 sheet
    By lscarstens in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-20-2012, 02:44 PM
  5. Can you create a macro that creates a macro and saves to a worksheet?
    By rob.parson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2012, 07:49 PM
  6. Looping Macro to split one worksheet into multiple
    By DLE2885 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-06-2012, 03:16 AM
  7. Create a Looping macro which copies and inserts rows
    By BenR in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2007, 02:13 PM

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