+ Reply to Thread
Results 1 to 2 of 2

Pasting Columns of Values and Rows of Dates in a Macro

Hybrid View

cheembucket Pasting Columns of Values and... 11-12-2024, 06:05 PM
jdelano Re: Pasting Columns of Values... 11-13-2024, 08:40 AM
  1. #1
    Registered User
    Join Date
    11-12-2024
    Location
    Canada
    MS-Off Ver
    Office 2021
    Posts
    1

    Pasting Columns of Values and Rows of Dates in a Macro

    Hi there, I'm currently trying to take a source sheet of values and use a macro to paste a subset of the original sheet to a new sheet.

    I want to make a Dim kpi_var for the columns of KPIs, and then I want to paste the rows of dates and their respective values for the kpi_vars.

    I'm trying to loop through the kpi_var columns to paste them into the target sheet, and loop through the dates to paste them in the rows.

    The button for the macro will clear the sheet and start pasting the columns and rows.

    Is anyone able to help me with some sample code that I can try to implement in my code? It doesn't seem to work properly right now.

    Source Sheet:

    sourcemacro1.PNG

    Target Sheet (for ingestion to a SQL server):

    sourcemacro2.PNG

  2. #2
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    318

    Re: Pasting Columns of Values and Rows of Dates in a Macro

    You can just loop through the rows on one sheet and write them to the other - it might be something like

    
    Dim sourceRow as Long
    Dim sourceCol as Integer
    Dim destinationRow as Long
    dim lastRowCol as String 
    
    ' clear the data on the destination sheet
    lastRowCol = Split(Sheet1.UsedRange.Address, "$")(3) ' extract the last row and col used on sheet2
    Sheet2.Range("A2:" & lastRowCol).Clear
    
    destinationRow = 2
    For sourceRow = 5 to Sheet1.UsedRange.Rows.Count
        For sourceCol = 1 to Sheet1.UsedRange.Columns.Count
            Sheet2.Cells(destinationRow, sourceCol).Value = Sheet1.Cells(sourceRow, sourceCol).Value
            destinationRow = destinationRow + 1
        Next sourceCol
    Next sourceRow
    This was just typed straight in here and might not work for you from the start

+ 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. Pasting values for non-adjacent rows/columns
    By tvewijck in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-22-2020, 02:23 PM
  2. [SOLVED] Macro for copying only rows with values and pasting them at the end of another spreadsheet
    By rassiusluine in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2013, 06:18 PM
  3. [SOLVED] Macro for copying values and pasting it for variable rows
    By Anonym216 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-24-2013, 04:43 AM
  4. Replies: 1
    Last Post: 02-14-2013, 01:12 AM
  5. Pasting dates as values
    By chuckmg in forum Excel General
    Replies: 3
    Last Post: 10-16-2012, 11:34 PM
  6. Macro Help (pasting values into several columns)
    By juliorevka in forum Excel General
    Replies: 4
    Last Post: 03-05-2012, 12:25 PM
  7. Macro to compare multiple columns then delete rows with older dates
    By azurememories in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-23-2011, 05:43 PM

Tags for this Thread

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