12-30-2020 05:10 PM
12-30-2020 05:10 PM
Hello, so I am new to VBA and am struggling to perform a particular task. I have performed some machine learning on some stock data and have transferred them to excel. But the thing is this data can be buggy and sometimes not load. So I want to copy and paste the data onto another sheet in the same workbook.
I want to avoid certain columns and only copy others only due to the nature of feature engineering when I am building my machine learning model, I don't quite know which columns. But I am certain of two columns that will always be present. 1. the Date, (second to last column) 2. "Fold" (the last column). These headers will always be in place.
How do I copy and paste the data from a range of sheets starting at date which will be in the second to last column on the right-hand side? And avoid the three columns before the last column (which is the Fold) last column
This is what I have so far
Sub CopyColumnsSource() ' Define a variable called last row and the end row Dim lastrow As Long, erow As Long ' Define the column headers that I want to copy by name, the first array and the second one I want to use as a meter to avoid the three columns before it (Date and Fold respectively) Dim ColumnHeaderArr(0 To 1) As String ColumnHeaderArr(0) = "Date" ColumnHeaderArr(1) = "Fold" If VerifyHeaders(ColumnHeaderArr) = True Then ' find the last row in the source sheet lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row ' We start with the first row because the first row will be headers For i = 1 To lastrow ' Copy data from the first row of sheet 1 Sheet1.Cells(i, 0).Copy ' Go to sheet 2, look for the last row that is used then go to the last row below it erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row ' Copy data from the first column to the the end of the column 1 but not column 1 and skip the second column before it ' in sheet 1 and paste it in the second column
12-30-2020 05:38 PM
Do you have a defined function called VerifyHeaders in one of the modules? At a first glance, the VBA is a bit overcomplicated for a simple Copy and Paste function. I'm a bit confused by your last two notes in the macro, is there any data appending?:
' Go to sheet 2, look for the last row that is used then go to the last row below it ' Copy data from the first column to the the end of the column 1 but not column 1 and skip the second column before it in sheet 1 and paste it in the second column
Regarding the column order, will the Date and Fold columns always be in the same place and/or retain the header?
12-30-2020 06:16 PM
@adversi Thanks for responding.
First q - So with that, I just wanted to confirm their presence before running the script. I got this idea from a youtube video - she saved the column names as an array so I followed through on it.
Second q - yes, they will always be in the same position. The last two columns will always be Date and Fold will always be the last two columns in the data and will always have the same header title "Date" and "Fold".
12-31-2020 02:08 AM
Maybe this is along the lines of what you're looking for (attached)? I'm assuming the headers are unique (if not, it can be modified). It sounds like you may want to add additional fields later on, so you should be able to just add the header captions to the headers array.
12-31-2020 10:42 AM
Thanks for this. So yes this is what I want but with some adjustments. I have included the comments in the excel sheet and also a sample of my data.
But thank you for this!
01-04-2021 09:22 PM
Sorry it took me a bit to get back to you. I think I understand better now what you want to do - the columns you want to copy are entirely based on position (relative to the date column).
See attached (Module 1). But, note that I'm making some assumptions about which row the "Date" header will appear in (I'm assuming Row 2, but change if necessary). Also, I'm assuming the macro should run on the active sheet and is copying the data to a new worksheet.