+ Reply to Thread
Results 1 to 7 of 7

Code for making New columns of Data in a new file (from the data Copied)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-10-2022
    Location
    USA
    MS-Off Ver
    2021
    Posts
    190

    Code for making New columns of Data in a new file (from the data Copied)

    Hi,
    I have timestamps in column I from cell I2 onward. Dates in column I is in Ascending order. In cell I2, date is 02-Jan. This date continues until cell I10. I want that respective values of Range I2:I10 for date 02-Jan should be copied from columns B and C. Now this code should also generate a new Excel file and paste this copied value in cell A1. Since data copied by code had 2 columns so on pasting on newly created file in cell A1, these two columns will show up covering Column A and B.
    Same way after date 02-Jan the next date comes is 03-Jan. this date 03-Jan is in this range I11:I25. So, respective values of this range in column B and C should be copied. This copied data should be pasted in the generated file in cell D1. And so on.

    Another Example 27-Jan
    Now date 27-Jan covers this range in Column I I41:I53. This date also comes in column S in this range S2:S14. Now this code should copy respective values of Range I41:I53 and S2:S14.
    Respective values of Column I should be picked from Column B and C
    Respective values of Column S should be picked from Column L and M

    This copied data should be pasted in the generated file in cell G1.
    Same for other dates coming in column S.

    Name of the Excel file Generated by Code:
    In dates of Column I and S, month and year is same in all timestamps. So, it should pic month name and year from any cell let’s say I2. In this Case, Name should be in this way January 2022 of the newly generated file.

    (As data in columns I and S is already in ascending order, so code should pick first date and go only till it ends and then copy respective values. It should Not Search date value in all cell of Column I and S one by one. In this way it may create load on my CPU. This is a sample file. In actual file I have almost 1 million Rows in column I and 0.8 million Rows in column S)

    (Date format copied from source file should be exactly equal to the pasted in the newly generated file)

    I have attached the file from which data is to be copied (Named as Source File)
    And another file showing Expected Results (Named As January 2022)

    I am using Excel 2021

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Code for making New columns of Data in a new file (from the data Copied)

    I wrote this which should do what you want. It might prompt if the target file already exists - you need to answer "Yes" to overwrite it else the macro will fail:

    Option Explicit
    Public Sub CopyDataToMonthlySheet()
    
    Dim lastCol As Long
    Dim lastRow As Long
    Dim thisCol As Long
    Dim thisRow As Long
    Dim nextRow As Long
    Dim sourceBook As Workbook
    Dim sourceSheet As Worksheet
    Dim targetBook As Workbook
    Dim targetSheet As Worksheet
    
    Set sourceBook = ActiveWorkbook
    Set sourceSheet = ActiveSheet
    sourceSheet.Copy After:=sourceSheet
    Set sourceSheet = Worksheets(sourceSheet.Index + 1)
    
    lastCol = sourceSheet.Cells(2, sourceSheet.Columns.Count).End(xlToLeft).Column
    If lastCol > 9 Then
        For thisCol = 19 To lastCol Step 10
            lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, thisCol).End(xlUp).Row
            With sourceSheet.Range(sourceSheet.Cells(2, thisCol - 7), sourceSheet.Cells(lastRow, thisCol))
                .Copy sourceSheet.Cells(sourceSheet.Rows.Count, "B").End(xlUp).Offset(1, 0)
                .ClearContents
            End With
        Next thisCol
    End If
    
    lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "I").End(xlUp).Row
    
    Set targetBook = Workbooks.Add
    targetBook.SaveAs Replace(sourceBook.FullName, sourceBook.Name, "") & Format(sourceSheet.Range("I2").Value, "mmmm yyyy") & ".xlsx"
    Set targetSheet = targetBook.Worksheets(1)
    
    thisCol = 1
    nextRow = 2
    For thisRow = 3 To lastRow + 1
        If sourceSheet.Cells(thisRow, "I").Value <> sourceSheet.Cells(thisRow - 1, "I").Value Then
            sourceSheet.Range(sourceSheet.Cells(nextRow, "B"), sourceSheet.Cells(thisRow - 1, "C")).Copy
            targetSheet.Cells(1, thisCol).PasteSpecial xlPasteValues
            targetSheet.Cells(1, thisCol).PasteSpecial xlPasteFormats
            nextRow = thisRow
            thisCol = thisCol + 3
        End If
    Next thisRow
    
    targetSheet.Range("A1").Resize(1, thisCol).EntireColumn.AutoFit
    targetSheet.Range("A1").Select
    targetBook.Save
    Application.DisplayAlerts = False
    sourceSheet.Delete
    Application.DisplayAlerts = True
    
    End Sub
    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Forum Contributor
    Join Date
    07-10-2022
    Location
    USA
    MS-Off Ver
    2021
    Posts
    190

    Re: Code for making New columns of Data in a new file (from the data Copied)

    Hi Sir WideBoyDixon,

    Thank you so much. It Worked Exactly like what i wanted. Thank you So Much for this code.

  4. #4
    Forum Contributor
    Join Date
    07-10-2022
    Location
    USA
    MS-Off Ver
    2021
    Posts
    190

    Re: Code for making New columns of Data in a new file (from the data Copied)

    Hi Sir,
    Hi Sir,
    Last edited by rexcel548562; 08-07-2022 at 08:23 AM.

  5. #5
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Code for making New columns of Data in a new file (from the data Copied)

    Urrrgh. Try this instead then:

    Option Explicit
    Public Sub CopyDataToMonthlySheet()
    
    Dim lastCol As Long
    Dim lastRow As Long
    Dim thisCol As Long
    Dim thisRow As Long
    Dim startRow As Long
    Dim nextCol As Long
    Dim nextRow As Long
    Dim lastVal As String
    Dim thisVal As String
    Dim sourceBook As Workbook
    Dim sourceSheet As Worksheet
    Dim targetBook As Workbook
    Dim targetSheet As Worksheet
    
    Application.ScreenUpdating = False
    
    Set sourceBook = ActiveWorkbook
    Set sourceSheet = ActiveSheet
    
    Set targetBook = Workbooks.Add
    targetBook.SaveAs Replace(sourceBook.FullName, sourceBook.Name, "") & Format(sourceSheet.Range("I2").Value, "mmmm yyyy") & ".xlsx"
    Set targetSheet = targetBook.Worksheets(1)
    
    lastCol = sourceSheet.Cells(2, sourceSheet.Columns.Count).End(xlToLeft).Column
    nextCol = 1
    lastVal = ""
    For thisCol = 9 To lastCol Step 10
        lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, thisCol).End(xlUp).Row + 1
        startRow = 2
        For thisRow = 2 To lastRow
            thisVal = sourceSheet.Cells(thisRow, thisCol).Value
            If thisVal <> lastVal Then
                If lastVal <> "" Then
                    ' Copy this section to the target sheet
                    If targetSheet.Cells(1, nextCol).Value = "" Then
                        nextRow = 1
                    Else
                        nextRow = targetSheet.Cells(targetSheet.Rows.Count, nextCol).End(xlUp).Row + 1
                    End If
                    sourceSheet.Range(sourceSheet.Cells(startRow, thisCol - 7), sourceSheet.Cells(thisRow - 1, thisCol - 6)).Copy
                    targetSheet.Cells(nextRow, nextCol).PasteSpecial xlPasteValuesAndNumberFormats
                    Application.CutCopyMode = False
                    If thisVal <> "" Then nextCol = nextCol + 3
                End If
                If thisVal <> "" Then
                    startRow = thisRow
                    lastVal = thisVal
                End If
            End If
        Next thisRow
    Next thisCol
    
    targetSheet.Range("A1").Resize(1, nextCol + 3).EntireColumn.AutoFit
    targetSheet.Range("A1").Select
    targetBook.Save
    
    Application.ScreenUpdating = True
    
    End Sub
    WBD

  6. #6
    Forum Contributor
    Join Date
    07-10-2022
    Location
    USA
    MS-Off Ver
    2021
    Posts
    190

    Re: Code for making New columns of Data in a new file (from the data Copied)

    Hi Sir WideBoyDixon,
    Last edited by rexcel548562; 08-07-2022 at 08:24 AM.

  7. #7
    Forum Contributor
    Join Date
    07-10-2022
    Location
    USA
    MS-Off Ver
    2021
    Posts
    190

    Re: Code for making New columns of Data in a new file (from the data Copied)

    Hi Sir WideBoyDixon,
    Last edited by rexcel548562; 08-07-2022 at 08:24 AM.

+ 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. [SOLVED] VBA Amendment to split copied data across different columns
    By Spoonman667 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-22-2019, 08:11 AM
  2. [SOLVED] Help with Parsing data copied from a PDF into columns
    By Chriz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-10-2017, 08:34 AM
  3. Paste copied data in rows not columns
    By dgibney in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-12-2016, 04:46 AM
  4. [SOLVED] Data Copied Excess in columns
    By sagar007 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-28-2012, 04:27 AM
  5. Code to Close worksheet where data is being copied from
    By bherbert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2010, 12:19 PM
  6. [SOLVED] New data will not sort with existing data from copied file
    By Carolyn in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-05-2006, 10:15 AM
  7. format cells-copied data from the txt file
    By Eric in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-22-2005, 11:09 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