Excel convert column into row - excel

I want to convert Column data into 4 rows in excel.
I have data like this
A B C D
1
2
.
.
20000
ABCD are columns, only A has value till 1 to 20000.
I want to equally divide 4 values ABCD(1234), ABCD (5678)
How i can do in excel ?

You can use the following formula:
=IFERROR(INDEX($A:$A,ROW(A1)*4-4+COLUMN(A1)),"")
drag it in the same row 3 times and down
1 2 3 4 Drag in the same row 3 columns
5 6 7 8 and down in each column
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
25 26 27 28
29 30 31 32
33 34 35 36

Like this?
Sub divideby4()
Dim i As Integer
For i = 1 To 5
Range("D" & i).Value = Range("A" & i).Value / 4
Range("C" & i).Value = Range("A" & i).Value / 4
Range("B" & i).Value = Range("A" & i).Value / 4
Range("A" & i).Value = Range("A" & i).Value / 4
Next i
End Sub
Before and after:
Alternatively:
Sub equallydivideby4()
Dim i As Integer, j As Integer
j = 1
For i = 1 To 10
Range("D" & i).Value = Range("A" & j + 3).Value
Range("C" & i).Value = Range("A" & j + 2).Value
Range("B" & i).Value = Range("A" & j + 1).Value
Range("A" & i).Value = Range("A" & j).Value
j = j + 4
Next i
End Sub
Produces (Before/After):

Related

GroupBy and Sum rows using multiple columns in Excel

I am trying to groupby and sum the rows of an Excel file and sum the amount.
Sample Data:
Name
Age
NetPay
Gross Value
Manz
36
260
1200
Nerz
26
760
1480
Manz
36
140
1290
Nerz
26
160
1495
Manz
36
880
1140
Manz
16
260
1200
Kiks
24
470
1700
Rats
31
290
1760
Manz
36
260
1200
Expected Output:
Name
Age
NetPay
Gross Value
Manz
36
1540
4830
Nerz
26
920
2975
Manz
16
260
1200
Kiks
24
470
1700
Rats
31
290
1760
Sub Output_Final_Validation(strval As String)
Dim wrkbokval As Workbook
Dim shtval As Worksheet
Dim shterror As Worksheet
Dim Lastrow As Long
Dim LastCol As Long
Set wrkbokval = Workbooks.Open(strval)
Set shtval = wrkbokPRval.Sheets("Sheet4")
Lastrow = shtval.Cells(Rows.Count, 1).End(xlUp).Row
shtval.Range("A2:AP" & Lastrow).Value = shtval.Range("A2:Z" & Lastrow).Value
Please suggest how we can resolve it,
I need to groupby on column Name and Age and ̀ Sum NetPayandGross Value`.
I have tried using Pivot as well but couldn't worked.
You can do it using a Dictionary to store name concatenaded with age (it looks like it's your unique ID, but consider using something else because 2 people can share name AND age easily) and then apply SumIfs to get your calculus:
Sub test()
Dim rngSource As Range
Dim i As Long
Dim Dict As Object
Dim LR As Long
Dim MyStr As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Set Dict = CreateObject("Scripting.Dictionary")
Set rngSource = Range("A2:D" & LR)
For i = 2 To LR Step 1
If Dict.Exists(Range("A" & i).Value & "|" & Range("B" & i).Value) = False Then
Dict.Add Range("A" & i).Value & "|" & Range("B" & i).Value, 0
End If
Next i
'destination. I'm pasting in cell G2
Range("G2").Resize(Dict.Count, 1) = Application.WorksheetFunction.Transpose(Dict.Keys)
Dict.RemoveAll
Set Dict = Nothing
i = 2
Do Until Range("G" & i).Value = ""
Range("H" & i).Value = Split(Range("G" & i).Value, "|")(1) 'age
Range("G" & i).Value = Split(Range("G" & i).Value, "|")(0) 'name
With Application.WorksheetFunction
Range("I" & i).Value = .SumIfs(rngSource.Columns(3), rngSource.Columns(1), Range("G" & i).Value, rngSource.Columns(2), Range("H" & i).Value) 'NetPay
Range("J" & i).Value = .SumIfs(rngSource.Columns(4), rngSource.Columns(1), Range("G" & i).Value, rngSource.Columns(2), Range("H" & i).Value) 'Gross Value
End With
i = i + 1
Loop
Set rngSource = Nothing
End Sub

For loop in excel vba not showing values to cell

I'm trying to use "for loop" to get some specific values, code runs perfect but values do not print to destination.
subcode, where the problem is, looks like this :
lastRow = Range("H" & Rows.Count).End(xlUp).Row
For i = 5 To lastRow
radius = Range("C" & i).Value
spacing = Range("L" & i).Value
Select Case radius
Case 0 To 450
spacing = 6
Case 451 To 750
spacing = 9
Case 751 To 2000
spacing = 18
End Select
Next i
In your code you are storing a value from Range("L" & i).Value to the variable spacing. Then, in your For loop you are assigning a new value to the variable, but you are not setting the range (cell) to that value.
The below, albeit shoddy, will work for you.
lastRow = Range("H" & Rows.Count).End(xlUp).Row
For i = 5 To lastRow
radius = Range("C" & i).Value
Select Case radius
Case 0 To 450
Range("L" & i).Value = 6
Case 451 To 750
Range("L" & i).Value = 9
Case 751 To 2000
Range("L" & i).Value = 18
End Select
Next i

While loop to input from multiple texbox

I have 20 textBoxes on a vba userform these textBoxes are supposed to take their values from a barcode reader and i created a while loop to take the values from those textboxes and input them on the next empty row , but when i check the results i get 2 problems
J = 0
While J < 20
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("A" & LastRow).Value = Now()
ws.Range("B" & LastRow).Value = Me.Controls("TextBox" & J + 1).Value
ws.Range("D" & LastRow).Value = Me.Controls("TextBox" & J + 2).Value
ws.Range("I" & LastRow).Value = TextBox21.Value
J = J + 1
Wend
The Quantity inserted on column D is Repeated on the Next Row Column B
Even If the TextBoxes are Empty It is still placing data As you can see on the highlighted in Red
Will post as an answer so it can be marked as such, though I listed this in a comment:
J = 0
While J < 20
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("A" & LastRow).Value = Now()
ws.Range("B" & LastRow).Value = Me.Controls("TextBox" & J*2 + 1).Value
ws.Range("D" & LastRow).Value = Me.Controls("TextBox" & J*2 + 2).Value
ws.Range("I" & LastRow).Value = TextBox21.Value
J = J + 1
Wend
May want to check your J max after this...
row 1 uses J = 0, so textbox 1, textbox 2
row 2 uses j = 1, so textbox 1*2+1 (3) and textbox 1*2+2 (4)
row 3 uses j =2, so textbox 2*2+1 (5) and textbox 2*2+2 (6)
etc.

Insert data based on another cell content

I know a little bit about VBA but I can't seem to be able to work my way around this programming question.
I have a sheet where I want to program how many days it will take before the end of the tasking. Each Status are equal to a number of days, for exemple if a file is at the Pending stage it will take 180 total to be completed. But what i want is at each stage to write the number of days it will take. For example
Status is written in range E3:E160
If cell in range= Pending then
Offset 4 columns over and write 20, and offset 5 columns over and write 35 and offset 6 columns over and write 50, and offset 7 columns over and write 25, and offaet 8 columns over and write 15 and offset 9 columns over and write 15 and finally, offset 10 columns over and write 20
However if cell in range = "Planning" then offset 5 columns over and write 35, and offset 6 columns over and write 50 and so on until offset 10 columns over and write 20
The goal is tha for each status, the number of offset is based on the status.
Hope this help
I'm assuming ther will be a loop or something but I really can't figure it out.
Also it needs to be able to capture any new rows inserted within the range or outside the range.
Thanks to anyone who will be able to help me
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim LastRow As Long
Dim i As Long
LastRow = Range("E" & Rows.Count).End(xlUp).Row
For i = 3 To LastRow
If Range("E" & i).Value = "Pending" Then
Range("I" & i).Value = "20" And Range("J" & i).Value = 35 And Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Planning" Then
Range("J" & i).Value = 35 And Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Screening" Then
Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Exam" Then
Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Interview" Then
Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "References" Then
Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Closing" Then
Range("O" & i).Value = 20
End If
Next i
End Sub
If Range("E" & i).Value = "Pending" Then
Range("I" & i).Value = 20
Range("J" & i).Value = 35
Range("K" & i).Value = 50
Range("L" & i).Value = 25
Range("M" & i).Value = 15
Range("N" & i).Value = 15
Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Planning" Then
Range("J" & i).Value = 35
Range("K" & i).Value = 50
Range("L" & i).Value = 25
Range("M" & i).Value = 15
Range("N" & i).Value = 15
Range("O" & i).Value = 20`
You need to get rid of all the And statements in your Then clause. This is an example. You can change the rest. You might want to look into the Case Select method as well.

Macro for duplicates with conditions

I have some datas where there are multiple duplicates in column E and its dept no. in column S. What I want to do is, for any duplicate value in col E if the values of Col S are same, then it should retain the same value in the 1st duplicate and delete other duplicates. If the Col S values are not same, it should have the value as "18" in it. Eg
Col E Col S Ans
1515A 10 Retain no changes
1515AA 12 Retain as 1515AA in Col A and 12 as Col S
1515AA 12 Delete
1515AA 12 Delete
5151B 8 Retain no changes
515BB 5 Take 515BB with 18
515BB 3 Delete
I have nearly 800-1500 line items. Can anyone help me with a macro. It will be very useful for me, instead of manually finding and deleting datas.
You can use something like:
Sub EraseR()
i = 1
While Range("E" & i).Value <> ""
If (Range("E" & i + 1).Value = Range("E" & i).Value) And (Range("S" & i + 1).Value = Range("S" & i).Value) Then
Range(i + 1 & ":" & i + 1).Delete
ElseIf (Range("E" & i + 1).Value = Range("E" & i).Value) And (Range("S" & i + 1).Value <> Range("S" & i).Value) Then
Range(i + 1 & ":" & i + 1).Delete
Range("S" & i).Value = 18
Else
i = i + 1
End If
Wend
End Sub

Resources