I have an Excel spreadsheet with over 60 columns. Each column contains data from a SharePoint survey. The column headers are the actual survey questions that have been imported from a SharePoint data connection. I'm trying to do several things here.
First, I want to copy each column to a new worksheet. (The reason I need to do this is so that I can add the data in each column to a PowerPivot Data Model. PowerPivot recognizes the entire worksheet as one table, so it won't let me select only "Column A" to add to the Data Model, it automatically adds the entire table with all 60 columns).
Since I don't want to manually add 60 new sheets first before this, I'd like the code to copy each column to a newly created worksheet. (the worksheets don't need to be named, I could do that manually, unless someone has an easy way to do this as well!)
Then, I'd like the code to loop through each column performing the copy and paste to the newly created sheet.
I've found some examples of the code here for several related topics, but I'm so new to VBA that I'm having a hard time putting it all together. Thank you all so much for your time!
I've tried the following which does copy it over, but I'm not sure how to add a new sheet and loop through the columns
Sub CopyColumnToNewSheet()
Dim lastRow As Long
lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A1:A" & lastRow).Value = Sheets("Sheet1").Range("A1:A" & lastRow).Value
End Sub
You should do this instead, please note, there is no error handling, suit to your needs
Sub CopyColumnToNewSheet()
Const MySourcheSheetName = "Sheet1"
Dim CounterColumn As Long
For CounterColumn = 1 To Sheets(MySourcheSheetName).Cells.SpecialCells(xlCellTypeLastCell).Column
Sheets.Add
ActiveSheet.Name = Sheets(MySourcheSheetName).Cells(1, CounterColumn).Value 'ideally the title is unique and it's in the first row of each column
Sheets(MySourcheSheetName).Columns(CounterColumn).Copy Destination:=ActiveSheet.Columns(1)
Next CounterColumn
End Sub
Related
first of all I've read many topics but none has my solution.
I have a data of 4 columns and N Rows (as N is unknown number).
Here is a sample:
When a new row entered I want it to automatically create new sheet has the name which is written in title column of that row (for the first row, create sheet with name "M")
also copy all data in row M to the new sheet
i know there is a way
=<SheetName>!<cell> but it isn't really helps if i have for example 1000 columns,
i have to create 1000 sheet then copy 1000 times!!
that's all, thanks in advance.
also feel free to use any methods (such as VBA).
I think this could be messy especially if you end up with a lot of Data. The code below will allow you to run a Macro that will quickly create sheets based on the data. This macro will only work once but it does the job
Sub CopyRowsToSheet()
' Variables needed for the formula
Dim NofRows As Long
Dim i As Long
Dim iActive As Boolean
' Counting the number of Rows in the active sheet
With ActiveSheet
NofRows = .Range("A" & Rows.Count).End(xlUp).Row
' Cycling through the number of rows on the active sheet
' we have set i = 2 as there is a header on the first page. If there is no header then set i = 1
For i = 2 To NofRows
' Creating the new sheet
Worksheets.Add(, Sheets(Sheets.Count)).Name = "Row " & i
' Copy Data to new sheet
.Rows(i).Copy Sheets("Row " & i).Range("A1:D1")
Next i
End With
End Sub
Apologies not 100% sure how to make this automatic but if I find a way i will add it here for you
I have looked at similar questions but they don't seem to fit my problem. I have two sheets in excel; one has a type of header/overview data for some tasks, the other has several lines of details for each task.
I need help writing a macro that will match the Task ID between the two sheets and copy the entire row of header data from sheet one into a blank row above the group of detail rows in sheet two.
(Before this code will run, I will run a macro that will insert a blank line between each group of detail data. When I get this working, I will combine the two pieces of code.)
The copy statement is where I am stuck. It returns Application defined or object-defined error.
Sub InsertMWITitles()
‘copies the MWI titles above the correct MWI Steps group
Dim lngLastRowSht1, lngLastRowSht2, counterSht1, counterSht2As Long
With Worksheets(“MWI Titles”)
lngLastRowSht1 = .Cells(.Rows.Count, 8).End(xlUp).Row
lngLastRowSht2 = Worksheets(“MWI Steps”).Cells(Worksheets(“MWI Steps”).Rows.Count, 1).End(xlUp).Row
For counterSht1 = 1 To lngLastRowSht1
For counterSht2 = 1 To lngLastRowSht2
‘if the Task ID in column H of the MWI Titles sheet matches the Element ID in column A of
‘the MWI Steps sheet, copy the entire title row to the steps sheet above the group
If Worksheets(“MWI Titles”).Range("H" & (counterSht1)).Value = Sheets(2).Range("A" & counterSht2).Value Then
Worksheets(“MWI Titles”).Range("A" & (counterSht2).EntireRow.Copy Sheets(“MWI Steps”).Range("A" & (counterSht2 – 1))
End If
Next counterSht2
Next counterSht1
End With
End Sub
I also tried to copy and insert with shift down, but couldn't get that to work either.
The top section of the sample data is sheet 1/MWI Titles (the header data) and the bottom section is sheet 2 /MWI Steps (the detail data).
Sample of data in both sheets
I am new to VBA coding, I am struggling with a problem, need help with coding.
Question is: Image Links is above and described below
There are two columns, column A and B on Sheet1. Column A has 500 rows with the different person name but the same name is repeated serval times. column B has task status( i.e completed, open, closed) against each person name.
The task is to create a new worksheet which is kind of report of the sheet1 It has unique names from that 500 rows and checks how many tasks is completed or delayed or opened.
This my code to remove duplicated and create unique.
Sub CreateUniqueList()
Dim lastrow As Long
Dim ws As String
ws = "Sheet1"
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Sheets.Add.Name = "mysheet"
Sheets(ws).Range("A1:A" & lastrow).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Sheets("mysheet").Range("B1"), _
UNIQUE:=True
End Sub
#AdityaPal You can create a Pivot Table. To update it, after changing the source data, you can right'click the pivot table and click Refresh.
If you want to have a macro setup to automatically create a pivot table for you - then there a couple of StackOverFlow threads about this (i.e. How to create pivot table using vba
I read multiple Q&A's, however it's difficult for me to really understand all the long scripts with the usage of different objects.
I am trying to select a range from one worksheet to an other.
I want to keep the range dynamic, because the range can vary every time. By doing so I have used the following script:
Dim range As Long
For range = ActiveCell To ActiveCell.End(xlDown)
Sheets("Stock Report").range("A4" & range).Value =
Sheets("Unique File").range("Y8" & range).Value
However it doesn't do anything.
This script does work, but I would like to keep the last cell dynamic:
Sheets("Stock Report").Range("A4:A9000").Value =
Sheets("Unique File").Range("Y8:Y90004").Value
I have to do this for multiple columns which are calculated by using other files in a different worksheets and need to sort them finally without all the calculations in an other one.
Any suggestions?
OK try.
Sheets("Unique File").Range("Y8:Y" & Sheets("Unique File").Range("A4").End(xlDown).Row + 4).Copy
Sheets("Stock Report").Range("A4").PasteSpecial Paste:=xlPasteValues
Range("A4").End(xlDown).Row gets the last row of the data on the Stock Report sheet and is used to copy down to the same row on the Unique File sheet. I added 4 because your range starts on 8 on the Unique File sheet.
I think your code didn't work because you were trying to use the "range" you created from the Stock Report sheet on the Unique values sheet.
I have two worksheets within the same workbook, namely sheet1 ("rawdata") and sheet2 ("Overview).
I copy downloaded data into sheet1 ("rawdata"). Here the number of rows vary but heading/columns are always the same. After this I need to copy specific cells into another worksheet.
Here are the "rules" I was thinking about:
1) Always copy cells from the rawdata sheet E9, W9, X9 and Y9 into a specific cell in the target sheet. I had something like this (which worked):
Worksheets("overview").Range("X10").Value = Worksheets("rawdata").Range("E9").Value
2) Always copy the value within column E in the lastrow. However, the last row is varying from rawdata to rawdata while the column (E) stays the same. I tried something like this: (not working)
....= Worksheets("rawdata").Range("E1").End(xlDown).Value
3) The script should be linked to the button, when I click the button again to insert the data from the sheet rawdata, the data should be inserted in the next (following) column of worksheet overview.
Assumes column E always has data. Which in this case should be true.
Sorry tried to simplify and broke it.
LastRow_WithDataInColumnE = Worksheets("rawdata").Range("E" & .Rows.Count).End(xlUp).Row
Should be
With Worksheets("rawdata")
LastRow_WithDataInColumnE = .Range("E" & .Rows.Count).End(xlUp).Row
End With
Now .Rows.Count should refer to Worksheets("rawdata")
Worksheets("overview").Range("X10").Value = Worksheets("rawdata").Range("E" & .Rows.Count).End(xlUp).Row.Value
Should be
With Worksheets("rawdata")
Worksheets("overview").Range("X10").Value = .Range("E" & .Rows.Count).End(xlUp).Row.Value
End With
There is a discussion here Error in finding last used cell in VBA. Suggests a better solution for situations where there is no data in Column E or where rows have been deleted.
You could do something like this to get the last data range in column E:
Public Function FindLastColumnECellAvailable()
FindLastColumnECellAvailable = "E" & WorksheetFunction.CountA(Range("E:E"))
End Function
Then you will have this:
At the end just read the cell value:
Range(FindLastColumnECellAvailable).Value
Greetings
Sorry
An apologize "in advance", I just read the date, hope this will help you yet or anyone else, it's my second day