Move data from multiple columns into single row - excel

This formatting issue is confusing me. I'm not an Excel king and would greatly appreciate the solution to my problem.
I'm trying to format data from multiple columns into a single row by date. i.e.:
I've tried to search for solutions regarding transpose, but this seems a bit more involved. I have 4x data results for each date (as seen in the before column).

Here is a simple loop that works bottom up in the sheet, shifts last line over 4 columns, copies line above down and then deletes the line above.
Sub TransposeData()
Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long
Set WS = Worksheets("Sheet1")
With WS
Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
LastCellRowNumber = LastCell.Row
End With
'Loop through column A bottom up
For i = LastCellRowNumber To 2 Step -1
'Shift current values over
Range("A" & i & ":D" & i).Insert Shift:=xlToRight
'Copy new values down
Range("A" & i & ":D" & i).Value = Range("A" & i - 1 & ":D" & i - 1).Value
'Delete row
Rows(i - 1).Delete Shift:=xlUp
Next i
End Sub
Before:
After:

You can use NotePad++ to unite the rows as from a .csv file, and then, import the new formatted information on Excel again.
1- Save your spreadsheet in comma separated value format (.csv).
2- In NotePad++, click on: Edit->Line Operations->Join Lines.
3- Replace spaces (" ") by commas(","). You should get 3 replacements.
Now you have just one line with all values separated by commas.
4- Save and import this new file on Excel.

As per this, there are several options:
Item 3: enter in A8
=OFFSET($A$2,INT((COLUMN()-COLUMN($A$8))/4),MOD(COLUMN()-COLUMN($A$8),4))
Copy to the right as needed.
You could also use INDEX.

Related

Copy range between sheets to last row

Looking for simple, elegant solutions using the range in its current form to append under another Worksheet's last row.
Range:
Sheets("conf_9").Columns(5).Copy Destination:=Sheets("Rec_9").Columns(9)
Sheets("conf_9").Columns(6).Copy Destination:=Sheets("Rec_9").Columns(10)
Sheets("conf_9").Columns(7).Copy Destination:=Sheets("Rec_9").Columns(7)
Sheets("conf_9").Columns(8).Copy Destination:=Sheets("Rec_9").Columns(8)
Sheets("conf_9").Columns(12).Copy Destination:=Sheets("Rec_9").Columns(6)
Several responses to a similar question seem overly complicated and incompatible to the Columns usage - yes, I am new to VBA. For example, inserting the expression Columns(number) & last row syntax is rejected.
Many thanks.
Try the next code, please. It is not necessary to copy whole columns. The code calculates the last filled row of the sheet to copy the column and the first empty row of the target sheet column:
Sub AppendToOtherSheet()
Dim shC As Worksheet, wsR As Worksheet, lastRowC As Long, lastRowR As Long
Set shC = Sheets("conf_9")
Set wsR = Sheets("Rec_9")
lastRowC = shC.Range("E" & Rows.Count).End(xlUp).row
lastRowR = wsR.Range("I" & Rows.Count).End(xlUp).row + 1
shC.Range("E2:E" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 9)
shC.Range("F2:F" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 10)
shC.Range("G2:G" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 7)
shC.Range("H2:H" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 8)
shC.Range("L2:L" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 6)
End Sub
The above code assumes that all columns have the same number of rows, and the sheet where from the columns are copied has column headers.
If the columns in discussion do not have the same number of rows, lastRowC and lastRowR must be calculated for each column to be copied. If no column headers, the range to be copied will start (for instance) from "E1" instead of "E2" in the first copying code line.

Separating duplicate values VBA

I have a worksheet that contains 3 columns: name, city and education.
In column A (Names) I have duplicate values.
I want to separate it from other duplicate values in column A something like this shown in image below below.
Is it possible using VBA?
This code will add a blank row where Names is different. Assume Names in column A:
Sub SeparateDuplicates()
Dim lastCell As Integer
lastCell = Range("A" & Rows.Count).End(xlUp).Row
For i = lastCell To 2 Step -1
If Range("A" & i) <> Range("A" & i).Offset(-1, 0) Then
Range("A" & i & ":C" & i).Insert Shift:=xlDown
End If
Next i
End Sub
Excel has alredy has remove duplicates option on the ribbon. Data->RemoveDuplicates. You can also do this with advanced filters.
If you need to do it with VBA, for example:
Sub RemoveDuplicates()
ActiveSheet.Range("$A$1:$A$5").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
Where you need to put your range and your column.
A very useful tool to retrieve VBA code for determined action is the macro recorder, in the ribbon, Developer -> RecordMacro, perform you action and stop recording and then you can check the code generated for the actions you recorded. Its not the cleanest code but you can find there the lines of code for the specific actions you want.
Hope that helps

Cut cells within a named range, containing a specific value and paste them to specified column

i have multiple rows that contain multiple columns of data, the data in each row is almost the same but the columns are in different orders.
What im trying to do is to align all of the data in the columns with the other rows so that each column contain the same value, maybe a screen shot will help explain better.
Here is an example.
This is only a small section but the sheet has many more columns and rows, i have named all active cells with a named range (allcells44), this is the area where i want to search.
I have managed to successfully complete this task which has taken me 2 days to put together through trial and error but i'm running multiple subs (254 subs together) all in one module which is extremely long and it is taking some time complete. here is what i have at the moment
Sub Findandcut1()
Dim row As Long
For row = 1 To 267
' Check if "att_base_name" appears in the value anywhere.
If Range("I" & row).Value Like "*att_base_name:*" Then
' Copy the value to the destination column.
Range("I" & row).Cut
' move the original data in column to the right.
Range("H" & row).Insert Shift:=xlToRight
End If
Next
Call Findandcut2
End Sub
Sub Findandcut2()
Dim row As Long
For row = 1 To 267
' Check if "att_base_name" appears in the value anywhere.
If Range("J" & row).Value Like "*att_base_name:*" Then
' Copy the value to the destination column.
Range("J" & row).Cut
' move the original data in column to the right.
Range("H" & row).Insert Shift:=xlToRight
End If
Next
Call Findandcut3
End Sub
Sub Findandcut3()
Dim row As Long
For row = 1 To 267
' Check if "att_base_name" appears in the value anywhere.
If Range("K" & row).Value Like "*att_base_name:*" Then
' Copy the value to the destination column.
Range("K" & row).Cut
' move the original data in column to the right.
Range("H" & row).Insert Shift:=xlToRight
End If
Next
Call Findandcut4
End Sub
This is only a small section of the vba code but it is just repeating it self and changing a few variables each time, so here is one section without calling the next sub.
This is what i have.
Sub Findandcut1()
Dim row As Long
For row = 1 To 267
' Check if "att_base_name" appears in the value anywhere.
If Range("I" & row).Value Like "*att_base_name:*" Then
' Copy the value to the destination column.
Range("I" & row).Cut
' move the original data in column to the right.
Range("H" & row).Insert Shift:=xlToRight
End If
Next
End Sub
And This is what i want
Sub Findandcut1()
Dim row As Long
For row = 1 To 267
' Check if "att_base_name" appears in the value anywhere.
If Range("allcells44").Value Like "*att_base_name:*" Then
' Copy the value to the destination column.
Range("allcells44").Cut
' move the original data in column to the right.
Range("H" & row).Insert Shift:=xlToRight
End If
Next
End Sub
so i want to be able to search all cells in "named range" for value, Then cut and paste them to the specified column, but every variation i try seems to break my code, any help please. Thanks.
If "allcells44" is already an range you could try the following:
For row = 1 To 267
allcells44.Find("*" & att_base_name & ":*")
If Not allcells44 Is Nothing Then
allcells44.Find("*" & att_base_name & ":*").Copy 'copy the value
allcells44.Find("*" & att_base_name & ":*").Insert Shift:=xlToRight 'move the original data
End if
Next row
I'm hoping i got your question right...

How to make a sheet scroll to the last row with data once a button is clicked

I have looked through the forum, as well as Google, and I wasn't able to find an answer to what I'm trying to do.
One of the users [here][1] helped me get a code to copy data in a specific row (upon clicking a button) to the last row of a table that is found further down in the same sheet.
However, I'd like the sheet to scroll down to the last non-empty row of the table instead of scrolling manually. I understand that this can be accomplished through a combination of CTRL+SHIFT+"DOWN ARROW". However; the excel users are not very good with computers, so I am trying to make it as simple as possible to them.
Is there a VBA code that can do the job?
Thanks
So far, I am using Erin's code, which takes me to the last row of the spread sheet, instead of the last Non-Blank row. This could be because column A has formulas in all its cells, all the way down to the last cell. However, column A cells formulas are set to give blank unless there's data entered in their adjacent cells in column E.
Here's the code that I am using, which is pasted in the module.
Option Explicit
Sub copyRow()
Dim ws As Worksheet
Dim lRow As Long
' define which worksheet to work on, i.e. replace Sheet1 with the name of
your sheet
Set ws = ActiveWorkbook.Sheets("1. Clients Details")
' determine the last row with content in column E and add one
lRow = ws.Cells(Rows.count, "E").End(xlUp).Row + 1
' copy some cells into their ranges
ws.Range("E3:G3").Copy ws.Range("E" & lRow)
ws.[E1].Select
' combine H3, I3, J3, K3 and L3 and copy into column E, next empty row
ws.Range("H" & lRow) = ws.[H3] & " " & ws.[I3] & " " & ws.[J3] & ", " & ws.
[K3] & " " & ws.[L3]
' copy the other cells into their ranges
ws.Range("M3:Q3").Copy ws.Range("M" & lRow)
ws.[M1].Select
' combine H3 & I3
ws.Range("AA" & lRow) = ws.[H3] & " " & ws.[I3]
' combine J3, K3 & L3
ws.Range("AB" & lRow) = ws.[J3] & " " & ws.[K3] & " " & ws.[L3]
' copy Q3 into column Q only, if F3 = "Company"
If Worksheets("1. Clients Details").Range("F3").Value = "Company" Then
ws.Range("Q3").Copy ws.Range("Q" & lRow)
End If
Call scrollToEnd
End Sub
Sub scrollToEnd()
Dim lastrow As Long
Dim numRows As Long
numRows = Range("E1:E1000").Rows.count
lastrow = Range("E1:E1000").Columns(5).Rows(numRows).Row
ActiveSheet.Range("E" & lastrow).Activate
End Sub
Dim lastrow as long
Dim numRows as long
numRows = Range ("TableName").Rows.Count
lastRow = Range ("TableName").Columns (1).Rows(numRows).Row
Activesheet.Range("A" & lastRow).Activate
I can't test it right now, but I believe this will work. If you know the offset for your table, you can just do numRows + offset (mine are usually in A1, so I just add 1 for the header - numRows is data rows) to get your row number for the .Activate. :-)
Or to reach the same row as CTRL+SHIFT+"DOWN ARROW", regardless of the table:
With Activesheet
.Range("A" & .UsedRange.Rows(.UsedRange.Rows.Count).Row).Activate
End With
EDITED: I was thinking CTRL+END in the above code. To simulate CTRL+"DOWN ARROW" (adding SHIFT selects everything in its path...), you would actually use:
Range("A1").End(xlDown).Activate
You could simply paste this at the end of your sub since it is one line, or keep it as its own little sub if you are wanting to call it from a button-click. If it is column E that you want selected, you would simply replace "A1" with "E1".
This does assume that there are no blank cells in column E between "E1" and the last non-blank row. Otherwise, you will need to use the same logic as in your copyRow sub to find the last non-blank row:
ActiveSheet.Cells(Rows.Count, "E").End(xlUp).Activate
This will scroll down till the last row's cell is at the top left of the screen.
Sub test()
Application.Goto Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp), True 'where 'A' is the column of the last row
End Sub
You can paste the code at the bottom of your current procedure or assign it to a button.
EDIT:
Alternatively, you can try this. This will find the last row of any column.
Sub test()
Dim lastrow As Range
Set lastrow = Sheets("Sheet1").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious)
If Not lastrow Is Nothing Then Application.Goto lastrow, True
End Sub

Excel VBA select new usedrange portion of a column

Hi All.
I need your help please.
I want to apply a formula to the new rows that I've just added. I have a line of code I use for a full set of data, but I'm not sure how to modify it, or if there is a better way to do it.
I'm a bit out of my league with this line of code, I understand what it's doing but I didn't write it.
Sheets("Closed Loop").Range(varPastePos).Offset(0, 2).Resize(ActiveSheet.UsedRange.Rows.Count - 1, columnsize:=1).FormulaR1C1 = "My Formula"
varPastePos is the position the new data was pasted, in this case $F$28
Any ideas?
Use Range().FillDown to extend a pre-existing formula.
Dim lastRow As Long
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Range("A2", "A" & lastRow).FillDown
You can FillDown multiple columns at once like this:
Range("A2", "D" & lastRow).FillDown
You can do it all in one line like this:
Range("A2", Range("F" & Rows.Count).End(xlUp)).FillDown
.UsedRange would also include formatted blank cells so it is not reliable for finding the last used row.
Set ws = Sheets("Closed Loop")
Set cell1 = ws.Range(varPastePos) ' F28
Set cell2 = cell1.End(xlDown) ' F32 ? the last cell with data below F28. Similar to clicking on F28 and pressing Ctrl + down
Set rngF = ws.Range(cell1, cell2) ' F28:F32
Set rngH = rngF.Offset(, 2) ' H28:H32
rngH.FormulaR1C1 = "My Formula"

Resources