Excel - VBA - Concatenate - Multiple Cells in a column - excel

I have a code to concatenate three columns in a difference sheet for one cell -
Sheets("Sheet2").Range("D2") = Sheets("Sheet2").Range("A2") &
Sheets("Sheet2").Range("B2") & Sheets("Sheet2").Range("C2")
I want to repeat this cell for n number of time in the excel sheet - not able to do that

Let's suppose that you have stored your numbers in columns A, B, C.
First, use this code to determine last row (1 stands for A column):
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Then use For loop to achieve what you after (1 stands for A column, 2 - B, 3 - C, D - 4):
For i = 1 To lastRow
Cells(i, 4).Value = Cells(i, 1).Value + Cells(i, 2).Value + Cells(i, 3).Value
Next

In this routine n is the last row you want to be filled:
Sub KonKat()
Dim n As Long, i As Long
n = 5
With Sheets("Sheet2")
For i = 2 To n
.Range("D" & i).Value = .Range("A" & i).Value & .Range("B" & i).Value & .Range("C" & i).Value
Next i
End With
End Sub

Related

Add the sum of the total amount from the sheet

Thanks for opening my thread. I need help from you.
So in this sheet the total amount should be sum of fuel + surge + delivery_charge should be added in Total amount column.
Ex:- 1st order = 456777 this should add 109.49+303.41+25966.51 = 26379.41
2nd order = 23213213 should add 10+11318+65 = 11393
Dim i As Long, lastrow As Long, rng As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, "A") <> "" Then
rng = Cells(i, "A").End(xlDown).Row - 1
Cells(i, "B").Value = WorksheetFunction.Sum(Range("C" & i & ":C" & rng), Range("E" & i & ":E" & rng), Range("G" & i & ":G" & rng))
End If
Next i
I'm getting output from this logic :
But the issue is for 2nd order id. It should take only that row. But here its taking 5th and 6th row for addition.
2nd order id= 23213213 total_amt should be 11393.
So anyone could you please help me to find out an issue.
Thanks and Regards,
Ranger
The issue is in how you find rng.
rng = Cells(i, "A").End(xlDown).Row - 1
You're using xlDown to find the start of the next block of data. This works as you'd expect, except where the data changes every cell. If you click on cell A5 and press CTRL-Shift-Down, you'll see that it selects three cells as it jumps to the end of that block of data (A7). This is normal Excel behaviour.
You could re-write your code to loop through all the data until it finds the right scenario, but in this case I believe the single cell issue can just be trapped with the right If statement.
If Cells(i + 1, "A") <> "" Then rng = i
Try this:
Dim i As Long, lastrow As Long, rng As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, "A") <> "" Then
rng = Cells(i, "A").End(xlDown).Row - 1
If Cells(i + 1, "A") <> "" Then rng = i
Cells(i, "B").Value = WorksheetFunction.Sum(Range("C" & i & ":C" & rng), Range("E" & i & ":E" & rng), Range("G" & i & ":G" & rng))
End If
Next i

Is it possible to combine three rows having the same color together?

Here is an Image to support explanation.
Column D values are highlighted with the same color if the entries are identical in this column.
In this case, for each "group" of color I should:
add all the values of column E for a given color and enter the amount in the column J.
As you see in the picture, the amount in the column J is perfect when the given color occurs only in two rows (works perfectly for color red and green).
But as soon as the color covers more than two rows, the sum doesn't reflect the sum of all the rows, but the sum of two entries at a time only.
Here is the code I wrote for this calculation:
Sub test()
Dim LastRow As Long, I As Long, j As Long
Dim arr As Variant
Dim Total_Payments As Single
Dim Total_CashBalance As Single
With ThisWorkbook.Worksheets("Master")
'Find Last row of column L
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
'Set array starting from row 2 to LastRow of column L
arr = .Range("D2:D" & LastRow)
For I = LBound(arr) To UBound(arr)
If .Range("D" & I).Interior.Pattern <> xlNone Then
For j = LBound(arr) To UBound(arr)
If (.Range("D" & j).Interior.Pattern <> xlNone) And (I <> j) Then
If .Range("D" & I).Interior.Color = .Range("D" & j).Interior.Color Then
.Range("N" & I).Value = "Cell L" & I & " has the same background color with cell/s L" & j
Total_Payments = .Range("E" & I).Value + .Range("E" & j).Value
Range("J" & I).Value = Total_Payments
End If
End If
Next j
End If
Next I
End With
End Sub
I would be really happy if you could tell me how to make this work for a number of cells in one group greater than 2 :) Thank you!!
You can use a SUMIF formula, inserted via VBA if necessary.
Formula in C1 copied down
=SUMIF($A$1:A11,A1,$B$1:$B$11)

Combine data from different rows base on column value

I have a excel file that contain daily order record and I need to summarize the order detail base on different staff.
I would like to combine the rows base on same Staff ID and divide to different group.
All order with the same Staff ID(Column B) will divide to a same group, the quantity of item A to item X of the order within a group will sum individual, each group will retain the first order record only and the other order id(Column A) within the group will mark in the remark column(Column G).
I have a macro with many for-loop & if statement to finish the task, but I don't have any idea how to simplify or modified it. Could someone can give me some suggestion?
Please let me know if I can clarify anything for you.
Private Sub test()
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
'sum the quantity of item
For Z = 2 To lastrow
If Range("F" & Z) <> "" Then
ordercount = 2
For c = 2 To lastrow
If Z <> c Then
If Range("F" & Z) = Range("F" & c) Then
For i = 0 To 9
temp = Cells(Z, 3 + i) + Cells(c, 3 + i)
If temp <> 0 Then
Cells(Z, 3 + i) = temp
End If
Next i
Range("G" & Z) = Range("G" & Z).Value & "No." & ordercount
& " " & Range("A" & c).Value & Chr(10)
ordercount = ordercount + 1
End If
End If
Next c
End If
orderno = Range("G" & Z).Value
If orderno <> "" Then
Range("G" & Z) = Left(orderno, Len(orderno) - 1)
End If
Next Z
'delete the other record within the same group
For Z = 2 To lastrow
If Range("F" & Z) <> "" Then
For c = 2 To lastrow
If Z <> c Then
If Range("F" & Z) = Range("F" & c) Then
Rows(c).Delete
c = c - 1
End If
End If
Next c
End If
Next Z
End Sub
Sample:
The following will do what you expect, but the loop starts from the bottom of the data and moves to the second row, as when deleting rows it is advisable to work upwards, so as not to miss the comparison against any row when deleting rows:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = LastRow To 2 Step -1 'loop from last row to second (missing headers)
CheckID = ws.Cells(i, 2) 'get the Staff ID to check
For x = (i - 1) To 2 Step -1 'second loop for comparison
If ws.Cells(x, 2) = CheckID Then 'if ID's match
ws.Cells(i, 3) = ws.Cells(i, 3) + ws.Cells(x, 3) 'add values for Item A
ws.Cells(i, 4) = ws.Cells(i, 4) + ws.Cells(x, 4) 'add values for Item B
ws.Cells(i, 5) = ws.Cells(i, 5) + ws.Cells(x, 5) 'add values for Item C
ws.Cells(i, 6) = ws.Cells(i, 6) 'get the Group Number
ws.Cells(i, 7) = ws.Cells(i, 7) & Chr(10) & ws.Cells(x, 1) 'Add remark
ws.Rows(x).Delete Shift:=xlUp 'delete row
i = i - 1 'adjust counter
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'get new last row
End If
Next x
Next i
End Sub

Copy and Insert Row Based on Cell Time Value

My main problem is that I am trying to add a row directly beneath another row based on the time value of that row. Here's an example of what I'm trying to do:
column F ========> new column F
2 1
2
2 1
2
1 1
1 1
2 1
2
To better explain, if the value in the first column F is a 2, that represents a time value that is greater than 0:59:00 and another row is added beneath it. If it is a 1, then it represents a time value that is equal to or less than 0:59:00and no row gets added.
I have multiple coding attempts at fixing this, and this first one is by someone more well-versed in VBA than I and includes some of his comments:
Public Sub ExpandRecords()
Dim i As Long, _
j As Long, _
LR As Long
'set variable types
LR = Range("A" & Rows.Count).End(xlUp).Row
'setting variable LR as number of rows with data
Application.ScreenUpdating = False
Columns("F:F").NumberFormat = "hh:mm:ss"
'sets number format in column b to text
For i = LR To 1 Step -1
'Executes following code from last row with data to row 1 working backwards
'If CLng(Left(Range("F" & i).Value, Len(Range("F" & i).Value) - 6)) > 0 Then
If CLng(Hour(Range("F" & i))) > 0 Then
'If the hour value in column F is greater than 1, then...
With Range("F" & i)
'starting with column F, loop through these statements...
'.Offset(1, 0).Resize(CLng(Left(Range("F" & i).Value, Len(Range("F" & i).Value) - 6)) - 1, 1).EntireRow.Insert Shift:=xlDown
.Offset(1, 0).Resize(CLng(Hour(Range("F" & i))).Value, Len(Range("F" & i).Value) - 1, 1).EntireRow.Insert Shift:=xlDown
'return the value of column F's hour value, change the range to insert the number of rows below based on hour value
'.Resize(CLng(Left(Range("F" & i).Value, Len(Range("F" & i).Value) - 6)), 1).EntireRow.Value = Range("A" & i).EntireRow.Value
.Resize(Hour(Range("F" & i)), 1).EntireRow.Value = Range("A" & i).EntireRow.Value
'Get value of row to be copied
'For j = 0 To CLng(Left(Range("F" & i).Value, Len(Range("F" & i).Value) - 6))
For j = 0 To Hour(Range("F" & i))
Range("H" & i).Offset(j - 1, 0).Value = Application.Text(j, "0")
Next j
End With
Else
Range("H" & i).Value = Application.Text(1, "0")
End If
Next i
Application.ScreenUpdating = True
End Sub
Here is a similar question from a previous user
Any help would be greatly appreciated.
Use this instead:
Public Sub ExpandRecords()
Dim i As Long, s As String
Const COL = "F"
For i = 1 To Cells(Rows.Count, COL).End(xlUp).Row
If Cells(i, COL) = 2 Then s = s & "," & Cells(i, COL).Address
Next
If Len(s) Then
Range(Mid$(s, 2)).EntireRow.Insert
For i = 1 To Cells(Rows.Count, COL).End(xlUp).Row
If Cells(i, COL) = vbNullString Then Cells(i, COL) = 1
Next
End If
End Sub

copy row if ID blank with VBA

Hi I want to make the data in data base below
ID Name Card No
1 A 3
2
2 B 1
3 C 1
2
to be like
ID Name Card No
1 A 3
1 A 2
2 B 1
3 C 1
3 C 2
Using excel macros.. please help thanks :D
Update, my data is like this and the debugger only copy A & B column
Col->A B C D
Row
7 ID Name Address Card No
8 1 A 3
9 2
10 2 B X road 1
11 3 C Y road 1
12 2
How to fix this? please help :( and it started in row 7
Simple but below code will do the work:
Note: Below code assumes your "Column C" do not have any empty cells. If you do please add comment so I can update.
Sub copyPaste()
Dim lastRow As Long
lastRow = Range("C" & Rows.Count).End(xlUp).Row '<- Column that there is no empty cells
For i = 1 To lastRow
If Range("A" & i).Value = "" Then
Range("A" & i).Value = Range("A" & i).Offset(-1, 0).Value
End If
If Range("B" & i).Value = "" Then
Range("B" & i).Value = Range("B" & i).Offset(-1, 0).Value
End If
Next
End Sub
UPDATE: Below code will do the same job for all columns in the workbook. Code assumes that in the first row of worksheet you have headers and it is using this information to calculate how many iterations to do for columns.
Sub copyPaste()
Dim lastRow As Long
Dim lastCol As Long
lastRow = Range("C" & Rows.Count).End(xlUp).Row '<- Column that there is no empty cells
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For j = 1 To lastCol
For i = 1 To lastRow
If Cells(i, j).Value = "" Then
Cells(i, j).Value = Cells(i, j).Offset(-1, 0).Value
End If
Next i
Next j
End Sub

Resources