Transpose rows to columns on irregular and inconsistent data - excel

I have to transpose rows to columns in excel using vba and the data is of around 500000.
The problem is that the data is not regular and is consistent.
Like there will be 4 rows then a blank then it can be three rows or one as well.
I want to transpose the group of data separated by a blank cell to be transposed to the the respective column in-front of the first entry.
Sub Transpose()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Loop
End Sub
I used this code but the problem is that it is skipping the data which is present in the single row.

Then this should do it, beware that I'm assuming where your data is and where is going to get paste, don't forget to change that:
Option Explicit
Sub Transpose()
Dim LastRow As Long 'last row on the sheet
Dim TransposeRow As Long 'row where we transpose
Dim x As Long 'columns
Dim C As Range 'faster looping through cells with For Each C in range
With ThisWorkbook.Sheets("MySheet") 'change this to your sheet
'To assign the last row im gonna assume your data is in column A or 1(B would be 2 and so...)
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Last row with data
TransposeRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 'on column B will be pasting the data
x = 2 'initialize x being 2 as for B column
For Each C In .Range("A2:A" & LastRow)
If C = vbNullString Then 'in case the cell is blank we jump a row
TransposeRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 ' recalculate row for transposing data
x = 2 'reinitialize column counter
Else
.Cells(TransposeRow, x) = C 'we copy the value to the row and column empty
x = x + 1 'add 1 column
End If
Next C
End With
End Sub

I have edited your code to show an approach that can work for you. You need to add a condition for one cell data.
Sub Transpose2()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
If IsEmpty(ActiveCell.Offset(1, 0).Value) Then
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Else
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
End If
Application.CutCopyMode = False
Selection.End(xlDown).Select
Loop
End Sub
Note: Using select is not generally a good idea. An example of cutting down select would be:
Sub Transpose3()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
If IsEmpty(ActiveCell.Offset(1, 0).Value) Then
ActiveCell.Copy ActiveCell.Offset(0, 1)
Else
Range(ActiveCell, ActiveCell.End(xlDown)).Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").End(xlDown).Select
End If
Application.CutCopyMode = False
Selection.End(xlDown).Select
Loop
End Sub

Related

Append data to last row

I am quite new to VBA, hence unable to understand the scripts at this moment.
I tried recording a macro and it does quite a good job. However, it's not dynamic.
Here is my use case:
I have an excel workbook and it has two sheets named "Sheet1" & "FinalData". All I want to do is copy the data from a specific cell range of let's say C2:P2 from "sheet1" and append it to the "FinalData" sheet.
Basically, find the last empty row and paste the data there. Below is my recorded piece of vba code that indicates this function.
Can anyone help me with fixing the below code or sharing a new code, please? I will be grateful to you.
Thanks!
Application.CutCopyMode = False
Range("C2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("FinalData").Select
Range("A2").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Try this code, to select the last empty row, just sum + 1 to the total used rows
Application.CutCopyMode = False
Range("C2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("FinalData").Select
Cells(Sheets("FinalData").UsedRange.Rows.Count + 1, 1).Select ' Select last empty row
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sub copyData()
Dim sheet1 As Worksheet
Dim finalDataSheet As Worksheet
Set sheet1 = Sheets("Sheet1")
Set finalDataSheet = Sheets("FinalData")
'Get the last column based on the second row.
lastcolumn = sheet1.Cells(2, Columns.Count).End(xlToLeft).Column
'Get last row based on column C.
lastrow = sheet1.Cells(Rows.Count, 3).End(xlUp).Row
'Option A:
'Only transfer row 2 with dynamic columns from C to end column.
finalDataSheet.Range(finalDataSheet.Cells(2, 3), finalDataSheet.Cells(2,
lastcolumn)).Value = _
sheet1.Range(sheet1.Cells(2, 3), sheet1.Cells(2, lastcolumn)).Value
'Option B:
'Transfer entire dynamic table structure starting at c2.
'Get the values from sheet 1 data range and copy values over to final data
sheet into the same position.
finalDataSheet.Range(finalDataSheet.Cells(2, 3),
finalDataSheet.Cells(lastrow, lastcolumn)).Value = _
sheet1.Range(sheet1.Cells(2, 3), sheet1.Cells(lastrow, lastcolumn)).Value
End Sub

Select range instead of specific cell value with VBA

I got little project in VBA and stuck on below topic
I need to Sum selected range in first empty cell in B column. I tried a small macro in which it sums the same row which mentioned in the vba.
This is what I've found and try to use
Sub Macro9()
'
' Macro9 Macro
'
'
Range("A3").Select
Selection.Copy
Range("A2").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=+SUM(R[1]C:R[3]C)"
Range("B2").Select
Selection.Copy
Range("B3").Select
Selection.End(xlToRight).Select
Range("S2").Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveSheet.Paste
Selection.End(xlToLeft).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Final.xlsx").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False
Range("A6").Select
Windows("copy.xlsm").Activate
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Selection.End(xlToLeft).Select
End Sub
I tried to searched for last non-empty cell in selected range so it won't search the whole column
To get the sum of all the non empty cells in Column "B" and return it to Column "B2" as I can understand with the image, following is the code which help you:
Range("B2").FormulaR1C1 = "=Sum(R3C:R" & ActiveSheet.UsedRange.Rows.Count + 1 & "C)"
You may use following code to get the sum of till first empty cell:
Range("B2").FormulaR1C1 = "=Sum(R3C:R" & Range("B3").End(xlDown).Row & "C)"
use SpecialCells method of Range object to get not empty cells in a given range
then use Areas property of Range object to loop through each group of consecutive not empty cells
Option Explicit
Sub SumThem()
Dim c As Range
For Each c In Intersect(ActiveSheet.UsedRange, Columns(2)).Offset(1).SpecialCells(xlCellTypeConstants).Areas ' loop through each "area" (i.e.: group of consecutive cells with "constant" values) of column B from row 2 downwards
c.Resize(1).Offset(-1, 0).Formula = "=+SUM(" & c.Address & ")" ' place the sum of current "area" in the cell right above it
Next
End Sub

How to add the most top & bottom row on VBA

I want to merge the first and the last row using the =cell1&cell2 function of the table but was unable to as the number of row can be dynamic.
Tried using the relative distance using ctrl+up but to no avail.
Ideally a VBA code where I can use the "&" function to merge the most top and last row of the table then paste special on top as text
Sub Macro9()
ActiveCell.FormulaR1C1 = "=R[-9]C&R[-2]C"
ActiveCell.Select
Selection.Copy
Selection.End(xlUp).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(2, 0).Range("A1").Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
If I understand correctly , what you want is a way to address the last cell in a column.
You can do this as follows:
Set sht = Sheets("main")
column = 1
lastRow = sht.Cells(sht.Rows.Count, column).End(xlUp).Row
Set lastCell = sht.Cells(lastRow, column)
lastCell is a range variable referencing the last cell in the column specified by the column variable. I explicitly referenced the sheet to avoid problems with active sheets.
Sub MergeCells()
col = 1 // column A
lastRow = Cells(Rows.Count, 1).End(xlUp).row
Mcell = Cells(1, col) & Cells(lastRow, col)
End Sub

how to generate cells using macros in excel sheets?

Dim i, last As Integer
last = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To last
If Cells(i, 1) <> "" Then
Range("A1:C").Select
Selection.Copy
Sheets("Sheet2").Select
Application.Run "updatecc"
Range("A1:c").Select
Selection.Insert Shift:=xlDown
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Application.Run "updatecc"
End If
Next i
End Sub
Help needed here, I want to add cells dependent upon data in cells of sheet1 to sheet2.
It is copying only first a1:c1 values.
I need to copy from sheet1 and paste it in sheet2 and generate cells.
Replace the first Range("A1:C").Select by Range(Cells(i,1),Cells(i,3))
This will select columns A to C of the row that you are checking.

insert formula in column until next non blank cell

In my macro, i need to insert a formula in columns AA and AB, AB is empty so no problem there. Column AA has data which starts in AA10954, my problem is AA10954 changes each week as my data either increases or decrease as i import from another book, can someone help me to set the last empty cell before my data starts in AA?
Sub ClassVisit()
'
' ClassVisit Macro
'
'
Dim lr As Long
With ActiveWorkbook
With ActiveSheet
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
Range("AA2").Formula = "=IFERROR(VLOOKUP(A2,[Data.xlsb]Stores!$A:$AA,27,0),VLOOKUP(A2,'[Salesinfo.xlsb]Packs'!$C:$E,3,0))"
Range("AB2:AB" & lr).Formula = "=IFERROR(VLOOKUP(A2,Attribute!D:F,3,0),""Not Visited"")"
Range("AA2").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A10953")
ActiveCell.Range("A1:A10953").Select
Range("AA2:AB" & lr).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.End(xlUp).Select
Application.CutCopyMode = False
ActiveCell.Offset(1, -26).Range("A1").Select
End With
End With
End Sub

Resources