Move data onto blank row below Excel from multiple columns with VBA macro - excel

I currently have data in the following format:
Name 1 | Email 1 | ID 1 | Address 1 Street | Address 1 Suburb | Address 1 City | Address 2 Street | Address 2 Suburb | Address 2 City | Address 3 Street | Address 3 Suburb | Address 3 City
<NEW LINE>
Name 2 | Email 2 | ID 2 | Address 1 Street | Address 1 Suburb | Address 1 City | Address 2 Street | Address 2 Suburb | Address 2 City
etc.
And I need it to look like the following:
Name 1 | Email 1 | ID 1 | Address 1 Street | Address 1 Suburb | Address 1 City
<NEW LINE>
Name 1 | Email 1 | ID 1 | Address 2 Street | Address 2 Suburb | Address 2 City
<NEW LINE>
Name 1 | Email 1 | ID 1 | Address 3 Street | Address 3 Suburb | Address 3 City
<NEW LINE>
Name 2 | Email 2 | ID 2 | Address 1 Street | Address 1 Suburb | Address 1 City
<NEW LINE>
Name 2 | Email 2 | ID 2 | Address 2 Street | Address 2 Suburb | Address 2 City
etc.
What I have so far in my spreadsheet is in column A is a COUNTA formula to work out the number of rows we need to insert below each row to allow for the data to be duplicated which is calculating fine. From this I have used the following code to insert these number of lines which is also working fine.
Sub ProcessAddressLabels()
For N = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
If Cells(N, 1) <> "" And Cells(N, 1) <> 1 Then
Rows(N + 1 & ":" & N + Cells(N, 1) - 1).Insert
NumValues = Cells(N, 1)
End If
Next N
End Sub
What I am not sure from here is how to copy each set of 3 cells from the end and place it along with the duplicated user data onto the lines below!
Any help is much appreciated and I hope I explained this simply enough!

Give this a try:
Sub Test()
Dim rw As Range, n As Long, i As Long, x As Long
Set rw = ActiveSheet.Rows(1) 'starting row
Do While rw.Cells(1).Value <> ""
'how many sets of addresses to move?
x = Application.Ceiling((Application.CountA(rw) - 6) / 3, 1)
If x > 0 Then
'insert required rows
rw.Offset(1, 0).Resize(x).Insert
For i = 1 To x
'copy common cells
rw.Cells(1).Resize(1, 3).Copy rw.Cells(1).Offset(i, 0)
'cut each address block
rw.Cells(7 + ((i - 1) * 3)).Resize(1, 3).Cut rw.Cells(1).Offset(i, 3)
Next i
End If
'move to next "new" row
Set rw = rw.Offset(1 + x, 0)
Loop
End Sub

I cannot say I fully understood all your needs, however
would something like this help:
Dim sht As Worksheet
Set sht = ActiveSheet
' copy the range C1:F1 to the start of the third row.
sht.range("C1:F1").Copy Destination:=Worksheets("Sheet1").range("A3")

Related

remove duplicates but retain the first position in excel vba macro

I am looking to remove duplicate rows but leave the first line
Using vba macros in excel 2010.
This is the initial information
A | B
1. A | 1
2. A | 1
3. A | 1
4. A | 1
5. B | 2
6. B | 2
7. B | 2
after running the macro
A | B
1. A | 1
2. | 1
3. | 1
4. | 1
5. B | 2
6. | 2
7. | 2
Can you help me,please!
Not elegant, but quick and dirty:
Dim iLastRow As Integer
iLastRow = 13
Range("h1:h" & iLastRow).Formula = "=if(countif(a$1:a1,a1)>1,"""",a1)"
Range("a1:a" & iLastRow).Value = Range("h1:h" & iLastRow).Value
Range("h1:h" & iLastRow).Clear

Draw 16 players into 4 groups where no one comes from same earlier group

I got between 16 and 40 players in groups of 4/5 players. meaning 4 to 10 groups.
I want to draw top16 (i got this list already formatted as:)
Name | Former Group (Ex. With 4 Groups)
Player1 | 1
Player2 | 2
Player3 | 3
Player4 | 4
Player5 | 4
Player6 | 3
Player7 | 2
Player8 | 1
Player9 | 2
Player10 | 1
Player11 | 3
Player12 | 4
Player13 | 1
Player14 | 2
Player15 | 4
Player16 | 3
This list i want to put into 4 Groups with a click of a button. Where no one comes from the same earlier group. This group is listed with players getting 1st in their former group first and then 2nd place and so on. So if its 10 groups of 4 my list could look like. As its 10 1st places and 6 2nd places in.
Name | Former Group (Ex. With 10 Groups)
Player1 | 1
Player2 | 2
Player3 | 3
Player4 | 4
Player5 | 5
Player6 | 6
Player7 | 7
Player8 | 8
Player9 | 9
Player10 | 10
Player11 | 3
Player12 | 4
Player13 | 7
Player14 | 5
Player15 | 9
Player16 | 1
I want to draw these top16 players into 4 new groups where they dont come into a group with a player they already played in the first round.
So i thought i would create a function and call that on a button click.
onClick i want to collect these players from
AA6 to AA21 (is there name)
AB6 to AB21 (is there former group number)
run them thru my function.
Private Sub CommandButton11_Click()
ReDim playerNames(0 To 16) As String
ReDim playerGroups(0 To 16) As Integer
For i = 1 To 16
playerNames(i) = Cells(i + 5, 27).Value
playerGroups(i) = Cells(i + 5, 28).Value
Next i
Dim txt As String
Dim txt2 As String
Dim ii As Long
For ii = LBound(playerNames) To UBound(playerNames)
txt = txt & playerNames(ii) & vbCrLf
txt2 = txt2 & playerGroups(ii) & vbCrLf
Next ii
MsgBox txt + txt2
End Sub
How can i create a logic that never gives me groups where players come from the same group ?
and then i want to past these into
AC6->AC9 (GroupA)
AD6->AD9 (GroupB)
AE6->AE9 (GroupC)
AF6->AF9 (GroupD)

How to get values related to another from multiple sheets in excel

I have an excel sheet containing employee details
EmployeeID | Name
1 | Name1
2 | Name2
and another sheet which has the address details
Name | Address
Name1 | Address1
Name2 | Address2
Is there a way I can get the address details from the second sheet and put it in the first. The output that I need is
EmployeeID | Name | Address
1 | Name1 | Address1
2 | Name2 | Address2
Example:
Sheet1
EmpID Name Addresss
1 1 El Chapo =VLOOKUP(B2,Sheet2!$A$2:$B$4,2, FALSE) // "Mazatlan, Sinoloa"
2 2 El Mencho =VLOOKUP(B3,Sheet2!$A$2:$B$4,2, FALSE) // "Tierra Caliente, Michoacan"
3 3 El Ondeado =VLOOKUP(B4,Sheet2!$A$2:$B$4,2, FALSE) // "Culiacan, Sinoloa"
Sheet2
Name Addresss
1 El Chapo Mazatlan, Sinoloa
2 El Mencho Tierra Caliente, Michoacan
3 El Ondeado Culiacan, Sinoloa
Add the VLOOkUP formula into the address column of Sheet1

Split a single Excel row into multiple based on columns

I need to create multiple Excel rows based off of a single row. For example, I currently have a single row for each personnel and there are dozens of columns that are "grouped" so to speak. So say column K is its own group, then columns M, N, O are a group, P, Q, R, are a group, etc. I need that single row to become multiple rows - one row per group of columns. So the current situation is:
1 | Smith, John | Column K | Column M | Column N | Column O | Column P | Column Q| Column R
And I need that to become:
1 | Smith, John | Column K
2 | Smith, John | Blank | Column M | Column N | Column O
3 | Smith, John | Blank | Blank | Blank | Blank | Column P | Column Q | Column R
Here's a solution. You probably can do that math a bit smarter. I can't right now. ;)
Sub splitRows()
Dim i As Integer
With Sheets(1)
For i = 2 To (.UsedRange.Columns.Count / 3)
.Range(Cells(i, 1), Cells(i, 3)).Value = .Range(Cells(1, 1), Cells(1, 3)).Value
.Range(Cells(i, (i - 1) * 3 + 1), Cells(i, (i - 1) * 3 + 3)).Value = .Range(Cells(1, (i - 1) * 3 + 1), Cells(1, (i - 1) * 3 + 3)).Value
Next
End With
End Sub
This will only work correctly for sheets where there are always groups of three columns. If not, you have to change that .UsedRange.Columns.Count / 3 part.
Cheers.

Shuffle values to a new row two cells at a time

To explain it in the easiest way possible:
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
... needs to look like:
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |
I've tried using the TRANSPOSE() function, but it doesn't seem to work in this situation.
How can I accomplish this simple task?
In an unused cell to the right use this formula,
=OFFSET($A$1, INT((ROW(1:1)-1)/2), COLUMN(A:A)-1+MOD((ROW(1:1)-1), 2)*2)
Fill right one column and fill down as far as necessary. Your results should resemble the following.
      
You put excel-vba in your tags, so I'll post the vba code for you. I don't know how to do it with simple cell formulas. Hopefully it's configurable enough to get what you want, beyond the simple example you gave:
START_ROW = 1
START_COL = 1
STEP_COL = 2
OUTPUT_ROW = 3
OUTPUT_COL = 10
Row = START_ROW
Col = START_COL
Out_Row = OUTPUT_ROW
While Cells(Row, Col).Value <> ""
While Cells(Row, Col).Value <> ""
For step = 0 To STEP_COL - 1
Cells(Out_Row, OUTPUT_COL + step).Value = Cells(Row, Col + step).Value
Cells(Out_Row, OUTPUT_COL + step).Value = Cells(Row, Col + step).Value
Next step
Out_Row = Out_Row + 1
Col = Col + STEP_COL
Wend
Col = START_COL
Row = Row + 1
Wend

Resources