I have a huge dataset consisting of multiple choice questions, which have to be sorted. Each question consists of a group of 10 rows, which has to be transformed into 10 columns. The sheet is now 1100 rows and I will have to do this with 16 other sheets of the same format.
I have created a macro in Excel by recording the necessary actions which result in this line of code:
Selection.End(xlDown).Select
Range("C21:C26").Select
Selection.Copy
Range("C19").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Rows("21:31").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("B27").Select
End Sub
Now I need the variables in the macro to change +1 each iteration, so the next iteration it will look like this.
Selection.End(xlDown).Select
Range("C22:C27").Select
Selection.Copy
Range("C20").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Rows("22:32").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("B28").Select
End Sub
I'm completely stuck there. Help is greatly appreciated!
Thanks so much in advance.
So I am a little unclear on your row counts.
You might be able to use arrays as follows (be sure to back up sheet as this clears data from the sheet)
Option Explicit
Public Sub Test()
Dim startRow As Long, endRow As Long, rng As Range, arr(), outputArr(), i As Long
startRow = 21
endRow = 1100
With ThisWorkbook.Worksheets("SheetA") '<== Change as required
Set rng = .Range("C" & startRow & ":C" & endRow)
arr = rng.Value
arr = Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Index(arr, 0, 1))
ReDim outputArr(1 To 5, 1 To Application.WorksheetFunction.RoundUp(UBound(arr, 1) / 5, 0))
outputArr = Application.WorksheetFunction.Transpose(outputArr)
Dim counter As Long, counter2 As Long
counter2 = 1
For i = LBound(arr) To UBound(arr) Step 12
For counter = 0 To 4
outputArr(counter2, counter + 1) = arr(i + counter)
Next
counter2 = counter2 + 1
Next
rng.ClearContents
.Range("C19").Resize(UBound(outputArr, 1), UBound(outputArr, 2)) = outputArr
End With
End Sub
Related
strange problem:
I read some Values from a sheet with a loop and paste them to another sheet.
I have a Control Module which calls one module after the other one.
My Problem: If I do the Call via control Module I run into the runtime error 1004.
When I start the macro manually it is no problem and everythin works fine..
This is my code:
[...]
rngname = 3
temp = 1
Do Until Cells(lngRow, 1).Value = "test"
lngLastRowOfSection = Cells(lngRow, 1).End(xlDown).Row
Set slcFind = Range(Cells(lngRow, 1), Cells(lngLastRowOfSection, 1))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(1, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngRow = Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngRow >= Rows.Count Then Exit Do
Loop
lngRow = 1
rngname = 3
i = 2
Do Until Cells(lngRow, 1).Value = "test"
lngLastRowOfSection = Cells(lngRow, 1).End(xlDown).Row
Set slcFind = Range(Cells(lngRow, 2), Cells(lngLastRowOfSection, 2))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(i, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngRow = Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngRow >= Rows.Count Then Exit Do
i = i + 1
Loop
[...]
Has anyone an idea?
This part of the code is marked:
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Best Regards,
Kalain
Scott Holtzman is correct, define your worksheet when setting ranges. I was able to recreate the bug by having 1 sheet with valid data, the sheet to paste and another entirely blank sheet. When I ran the macro from the valid sheet, it was fine, when I ran it with the blank sheet activated it tries to transpose a blank column to a row. Excel sheets have more columns then rows, so it crashes because it can't fit.
Try using this, change "First" to whatever your source sheet is called.
Sub test()
Dim Other As New Worksheet
Set Other = Worksheets("First")
lngrow = 1
rngname = 3
temp = 1
Do Until other.Cells(lngrow, 1).Value = "test"
lngLastRowOfSection = Other.Cells(lngrow, 1).End(xlDown).Row
Set slcFind = Range(Other.Cells(lngrow, 1), Other.Cells(lngLastRowOfSection, 1))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(1, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngrow = Other.Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngrow >= Rows.Count Then Exit Do
Loop
lngrow = 1
rngname = 3
i = 2
Do Until Other.Cells(lngrow, 1).Value = "test"
lngLastRowOfSection = Other.Cells(lngrow, 1).End(xlDown).Row
Set slcFind = Range(Other.Cells(lngrow, 2), Other.Cells(lngLastRowOfSection, 2))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(i, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngrow = Other.Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngrow >= Rows.Count Then Exit Do
i = i + 1
Loop
End Sub
I want to build a table on one Excel Sheet "Ship" by pulling data from another excel sheet "Efficiency." The row data on the "Efficiency" sheet is categorized by "Shipped", "Leave", "Import" and "Export".
Each category (shipped, leave, import, export) has several items and they're in no specific order. The table on the "Efficiency" sheet occupies columns A:H, and starts at row 2; the length can vary.
I want to be able to search the rows for "Shipped" and copy columns A, D:F and H of the matching rows and paste them beginning at cell B4 of the "Ship" sheet. Can anyone help me please?
Sub Ship()
ActiveSheet.Range("$A$1:$H$201").AutoFilter Field:=4, Criteria1:="Shipped"
' this is looking in a specific range, I want to make it more dynamic
Range("A4:A109").Select
'This is the range selected to copy, again I want to make this part more dynamic
Application.CutCopyMode = False
Selection.Copy
Range("A4:A109,D4:F109,H4:H109").Select
Range("G4").Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("Ship").Select
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
This code has been tested based on your the information as given in your question:
Sub Ship()
Dim wsEff As Worksheet
Dim wsShip As Worksheet
Set wsEff = Worksheets("Efficiency")
Set wsShip = Worksheets("Shipped")
With wsEff
Dim lRow As Long
'make it dynamic by always finding last row with data
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'changed field to 2 based on your above comment that Shipped is in column B (the code you posted has 4).
.Range("A1:H" & lRow).AutoFilter Field:=2, Criteria1:="Shipped"
Dim rngCopy As Range
'only columns A, D:F, H
Set rngCopy = Union(.Columns("A"), .Columns("D:F"), .Columns("H"))
'filtered rows, not including header row - assumes row 1 is headers
Set rngCopy = Intersect(rngCopy, .Range("A1:H" & lRow), .Range("A1:H" & lRow).Offset(1)).SpecialCells(xlCellTypeVisible)
rngCopy.Copy
End With
wsShip.Range("B4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
try the below code
Sub runthiscode()
Worksheets("Efficiency").Select
lastrow = Range("A" & Rows.Count).End(xlUp).Row
startingrow = 4
For i = 2 To lastrow
If Cells(i, 2) = "Shipped" Then
cella = Cells(i, 1)
celld = Cells(i, 4)
celle = Cells(i, 5)
cellf = Cells(i, 6)
cellh = Cells(i, 8)
Worksheets("Ship").Cells(startingrow, 2) = cella
Worksheets("Ship").Cells(startingrow, 5) = celld
Worksheets("Ship").Cells(startingrow, 6) = celle
Worksheets("Ship").Cells(startingrow, 7) = cellf
Worksheets("Ship").Cells(startingrow, 9) = cellh
startingrow = startingrow + 1
End If
Next i
End Sub
After few days learning VBA, I managed to get a simple macro to take some data from a sheet and transpose to another, then stack the columns together.
Macro
Sub pivotsourcedata()
Dim HeaderSelect As Range
Dim DataSelect As Range
Dim ID As Range
'Variabile Declaration for Progress bar
Dim x As Integer
Dim MyTimer As Double
For i = 1 To 7589
'Progress bar
Application.StatusBar = "Progress: " & i & " of 7589: " & Format(i / 7589, "0%")
'Copy ID Range
Sheets("Opps Closed FY15").Select
Range("A13").Offset(i, 0).Select
Set ID = Selection
'Copy Header Range
Range("EX13:HA13").Select
Set HeaderSelect = Selection
'Copy Data Range
Range("EX13:HA13").Offset(i, 0).Select
Set DataSelect = Selection
'Select ID and copy it to the next sheet and fill it down
ID.Copy
Sheets("Sheet1").Select
If i = 1 Then
Else
Selection.Resize(1, 1).Offset(0, 1).Select
End If
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Selection.Resize(HeaderSelect.Columns.Count).FillDown
'Select the Header, copy it in the adjacent column
Selection.Resize(1, 1).Select
Selection.Offset(0, 1).Select
Sheets("Opps Closed FY15").Select
HeaderSelect.Copy
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
'Same for the data, copy to the right of Header
Selection.Resize(1, 1).Select
Selection.Offset(0, 1).Select
Sheets("Opps Closed FY15").Select
DataSelect.Copy
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
'Stack the columns one over the other 3 by 3.
' take the 4th, 5th and 6th columns and stuck'em
' below 1st, 2nd and 3rd
If i = 1 Then
Else
Range("A1:C1").Offset(56 * (i - 1), 0).Resize(56, 3).Select
Dim PasteSelect As Range
Set PasteSelect = Selection
Range("D1:F56").Select
Selection.Cut Destination:=PasteSelect
Selection.Resize(1, 1).Offset(0, -1).Select
End If
Next i
Application.StatusBar = False
End Sub
As you can see for each of the 7589 times, I copy and transpose 3 times a range of 56 columns. This is taking a while, around 1.5h. Since I need to run it every week, I'm asking if I wrote badly some code portions...maybe I don't know I can seed it up in some areas...
any thoughts?
Update
After yours suggestions i get to tune up a bit the code, I'd like to know if there are others "imperfections"
Sub pivotsourcedata()
Dim OppsClosed As Worksheet
Set OppsClosed = Worksheets("Opps Closed FY15")
Dim Shadow2 As Worksheet
Set Shadow2 = Worksheets("Shadow2")
Dim ID As Range
Dim ID0 As Range
Set ID0 = OppsClosed.Range("A14")
Dim HeaderSelect As Range
Set HeaderSelect = OppsClosed.Range("EX13:HA13")
Dim DataSelect As Range
Set DataSelect = HeaderSelect
Dim PasteSelect As Range
Dim PasteSelect0 As Range
Set PasteSelect0 = Shadow2.Range("A1:C1").Resize(56, 3)
Dim CopySelect As Range
Set CopySelect = Shadow2.Range("D1:F56")
Dim Inizialize As Range
Set Inizialize = Shadow2.Range("D1:D1")
'Variabile Declaration for Progress bar
Dim x As Integer
Dim MyTimer As Double
'Set ScreenUpdating to False
Application.ScreenUpdating = False
For i = 1 To 7589
'Progress bar
Application.StatusBar = "Progress: " & i & " of 7589: " & Format(i / 7589, "0%")
'Copy ID Range
Set ID = ID0.Offset(i, 0)
'Copy Data Range
Set DataSelect = HeaderSelect.Offset(i, 0)
'Select ID and copy it to the next sheet and fill it down
ID.Copy
Shadow2.Select
If i = 1 Then
Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A1").Resize(HeaderSelect.Columns.Count).FillDown
Else
Range("D1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("D1").Resize(HeaderSelect.Columns.Count).FillDown
End If
'Select the Header, copy it in the adiacent column
HeaderSelect.Copy
If i = 1 Then
Shadow2.Range("B1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Else
Shadow2.Range("E1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End If
'Same for the data, copy to the right of Header
DataSelect.Copy
If i = 1 Then
Shadow2.Range("C1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Else
Shadow2.Range("F1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End If
'Stack the columns one over the other 3 by 3.
' take the 4th, 5th and 6th columns and stuck'em
' below 1st, 2nd and 3rd
If i = 1 Then
Else
Set PasteSelect = PasteSelect0.Offset(HeaderSelect.Columns.Count * (i - 1), 0)
Shadow2.Range("D1:F56").Cut Destination:=PasteSelect
End If
Next i
Application.StatusBar = False660858
'Set ScreenUpdating to True
Application.ScreenUpdating = True
End Sub
Take a look at this link for several other things that you can turn off, such as formula recalculation: http://datapigtechnologies.com/blog/index.php/ten-things-you-can-do-to-speed-up-your-excel-vba-code/
I agree that the multiple selects are unnecessary and likely slowing down the code significantly. In many cases, they can simply be combined - as in using
Selection.Resize(1, 1).Offset(0, 1).Select
instead of
Selection.Resize(1, 1).Select
Selection.Offset(0, 1).Select
But also, why not reference your ranges explicitly using your counter value, and avoid using resize and offset so frequently?
Another thought is to see if you can remove the final operation that stacks the columns after they are pasted to a new sheet - would it be possible to rearrange your source data, perhaps at the top of your macro before you get into the loop? That way you would have to perform that stacking once instead of 7589 times. Or, alternatively, find a way to combine the columns after the end of the loop.
The answer to my question was: "Use arrays" :)
The code now is this:
Sub pivotsourcedata()
'Set ScreenUpdating to False
Application.ScreenUpdating = False
Application.StatusBar = True
Dim OppsClosed As Worksheet
Set OppsClosed = Worksheets("Opps Closed FY15")
Sheets.Add.Name = "Shadow2"
Dim Shadow2 As Worksheet
Set Shadow2 = Worksheets("Shadow2")
Dim ID As Range
Dim ID0 As Range
Set ID0 = OppsClosed.Range("A13")
Dim HeaderSelect As Range
Set HeaderSelect = OppsClosed.Range("FB1")
Dim DataSelect As Range
Set DataSelect = OppsClosed.Range("FC14")
Dim RowSize As Integer
OppsClosed.Activate
Dim lastrow, records, nHeader As Integer
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row - 13
nHeader = 56
records = lastrow * nHeader
'Stack DataSelect on column C of Shadow 2
ReDim TempTableData(1 To nHeader, 1 To lastrow) As Variant
ReDim TempTableHeader(1 To nHeader, 1 To lastrow)
ReDim FixedHeaders(1 To nHeader, 1 To 1)
ReDim Temp_Array1(1 To records, 1 To 1) As Variant
ReDim Temp_Array2(1 To records, 1 To 1) As Variant
FixedHeaders = OppsClosed.Range("FC1").Resize(1, nHeader)
FixedHeaders = Application.Transpose(FixedHeaders)
For j = 1 To lastrow
'Progress bar
Application.StatusBar = "Progress: " & j & " of " & lastrow & ": " & Format(j / lastrow, "0%")
For i = 1 To nHeader
TempTableData(i, j) = DataSelect.Offset(j - 1, i - 1)
TempTableHeader(i, j) = FixedHeaders(i, 1)
Next i
Next j
For j = 1 To nHeader
For i = 0 To lastrow - 1
Temp_Array1((i * nHeader) + j, 1) = TempTableData(j, i + 1)
Temp_Array2((i * nHeader) + j, 1) = TempTableHeader(j, i + 1)
Next i
Next j
Shadow2.Range("C1:C" & records).Value2 = Temp_Array1
Shadow2.Range("B1:B" & records).Value2 = Temp_Array2
'Copy and Replicate ID
ReDim TempTableID(1 To records, 1 To 1)
k = 1
For i = 1 To records
'Progress bar
Application.StatusBar = "Progress: " & i & " of " & records & ": " & Format(i / records, "0%")
DoEvents
'FixedID = OppsClosed.Range("A13").Offset(k, 0)
TempTableID(i, 1) = OppsClosed.Range("A13").Offset(k, 0)
variable = i / nHeader
If Fix(variable) = variable Then
k = k + 1
End If
Next i
Shadow2.Range("A1:A" & records).Value2 = TempTableID
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub
I am working on an excel sheet and need to move the same range over and over again to the column "P" + 2
So the next range would be "C15:G15" to "P14". I'm looking for a slimmer solution than to repeat this code and change the ranges for hundreds of times..
ActiveWindow.SmallScroll Down:=-3
Range("C13:G13").Copy
Application.CutCopyMode = False
Selection.Copy
Range("P12").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
This quick snippet should walk down every second row in column C starting at row 13 till the last populated cell in column C.
Sub move_CG_to_PT()
Dim rw As Long
With Worksheets("Sheet4") '<~~set this worksheet reference properly!
For rw = 13 To .Cells(.Rows.Count, "C").End(xlUp).Row Step 2
.Cells(rw - 1, "P").Resize(1, 5) = _
.Cells(rw, "C").Resize(1, 5).Value
Next rw
End With
End Sub
This only transfers the values. If the formatting and/or theme is critical then those could be adjusted for with the following.
Sub move_CG_to_PT_w_Formatting()
Dim rw As Long
With Worksheets("Sheet4") '<~~set this worksheet reference properly!
For rw = 13 To .Cells(.Rows.Count, "C").End(xlUp).Row Step 2
.Cells(rw, "C").Resize(1, 5).Copy _
Destination:=.Cells(rw - 1, "P")
Next rw
End With
End Sub
I have an Excel dataset that has animals in column a, and numbers in columns b, c, and d.
I would like to find a vba code that will take this dataset and do two things: transpose the numbers into a column, and then put the name of the associated animal into the adjacent cell. If you follow the link, sheet one shows the dataset that I have, and sheet 2 shows the dataset I would like to have.
you can see the dataset here: https://drive.google.com/file/d/0B8ss18LQyoQrdDVIQ2JMZmdPNVU/view?usp=sharing
This code will get me partway, but it doesn't do quite what I want it do to:
Sub moveandinsert()
Dim start_cell As Range
For i = 1 To 3
Set start_cell = Sheets("sheet1").Cells(i, 2)
Range(start_cell, start_cell.End(xlToRight)).Copy
Sheets("Sheet2").Select
lastRowA = Range("A" & Rows.Count).End(xlUp).Row + 1
Range("A" & lastRowA).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
For j = 1 To 12
If Cells(j, 1).Value > 0 Then
Sheets("Sheet1").Cells(i, 1).Copy
Sheets("Sheet2").Cells(j, 2).Select
Selection.PasteSpecial xlPasteAll
j = j + 1
End If
Next j
Next i
End Sub`
Any help will be appreciated
Try the following:
Sub moveandinsert()
Dim start_cell As Range
For i = 1 To 3
Set start_cell = Sheets("sheet1").Cells(i, 2)
Range(start_cell, start_cell.End(xlToRight)).Copy
Sheets("Sheet2").Select
lastRowA = Range("A" & Rows.Count).End(xlUp).Row + 1
Range("A" & lastRowA).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
For j = lastRowA To lastRowA + 4
If Cells(j, 1).Value > 0 Then
Sheets("Sheet1").Cells(i, 1).Copy
Sheets("Sheet2").Cells(j, 2).Select
Selection.PasteSpecial xlPasteAll
'j = j + 1
End If
Next j
Next i
End Sub
1) j = j + 1 is not required cuz j will increment itself in a for loop
2) you can use lastrowA as the starting point of your paste instead of hardcoded for j = 1 to 12