Macro to merge cells from two columns - excel

I would like to create a macro that merges cells in column A with the cell in column B only if there is data in column A. Please help

Do you need something like this?
I assumed that columns A and B are filled and I concatenated values from cells A and B in cell A.
Sub concatenate()
Dim row As Integer
Range("A1").Select
row = 0
Do Until ActiveCell.Offset(row, 0).Value = ""
ActiveCell.Offset(row, 0).Value = ActiveCell.Offset(row, 0).Value + ActiveCell.Offset(row, 1).Value
row = row + 1
Loop
End Sub

Related

Insert Row and Copy Row Above with different values

I am trying to write some VBA that does two things:
When a value J column = "XY" duplicate the row by inserting the same data into a row below
In the newly inserted row, change values in G, H & L to "0"
So far, I have found this, which works to insert a blank row but I cannot figure out how to do the rest:
Dim i As Range
Dim cell As Range
Set i = Range("J:J")
For Each cell In i.Cells
If cell.Value = "XY" Then
cell.EntireRow.Copy
cell.Offset(1).EntireRow.Insert
End If
Next
The above inserts a blank row but I also need to copy and paste the row above its values and change some.
When inserting/deleting rows it's usually best to loop from the bottom up.
That's what the following, simple, example does.
Sub InsertXY()
Dim idx As Long
For idx = Range("J" & Rows.Count).End(xlUp).Row To 1 Step -1
If Range("J" & idx).Value = "XY" Then
Rows(idx).Copy
Rows(idx + 1).Insert Shift:=xlDown
Intersect(Rows(idx + 1), Range("G:H, L:L")).Value = 0
End If
Next idx
End Sub
Before
After

Loop search text for same row specific range columns and once found, return value in a different row but same column

I am new to VBA and need some help to come up a code to solve this. eg. I want to loop search column B to H in each row and return a specific cell value.
eg. loop search Column B to H for row 7 and once "p20028" cell (E7) is located based on column J. Return/copy "sa2084" at D5 to cell I7.
Sub whereused()
Dim part_num_1 As String
Dim finalrow As Integer
Dim row As Integer
Dim column As integer
finalrow = Sheet("Sheet1").Range("J1400").End(x1up).row
For row = 3 To finalrow
For column = 1 To 8
part_num = Sheets("Sheet1").Cells(row, 10).Value
If Cells(row, column).Value = part_num Then
End If
Next column
Next row
End Sub

Sum cell values below until blank cell

I need the excel formula to add the cell values until blank cell.
I have tried with the below formula :
=IF(A4="",SUM(A4:INDEX(A$1:$A4,MATCH(TRUE,(A$1:$A4=""),A4))),A4)
But it shows the wrong result.
Please see the picture below to understand, what kind of result I am searching for.
With blocks of data in column A, in B2 enter:
=IF(A2="",SUM($A$1:A2)-SUM($B$1:B1),"")
and copy downwards:
Each value in column B is the sum of the A-block above it.Using column B allows us to avoid having to figure out where in column A to put a SUM() formula.
If you want the result on the same column, give a try with this macro,
Sub total()
Dim i As Long, temp As Long
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row + 1
If Cells(i, 1) <> "" Then
temp = temp + Cells(i, 1)
Else
Cells(i, 1) = temp
temp = 0
Cells(i, 1).Interior.Color = vbYellow
End If
Next i
End Sub

VBA Macro to insert Average in the last row of each column with dynamic ranges

I have a number of columns (in Excel file) that represent months of the year; each column has a number of records and I'd like to be able to run a macro that would insert a cell in the last row of each column with an Average. The first cell of each column is a header, so I need to be including cells from row 2 down to the last record in the column. The number of columns and rows will be different each time i use the macro . I would very much appreciate your help. Thanks!
Here's a solution that will work out of the box for you. It takes the average of each column, starting at row 2.
Sub AverageColumn()
Dim count As Integer
Dim sum As Integer
Dim lastCol As Integer
lastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.count).Column
For c = 1 To lastCol
sum = 0
count = 0
ActiveSheet.Cells(2, c).Select
Do While ActiveCell.Value <> ""
sum = sum + ActiveCell.Value
count = count + 1
ActiveCell.Offset(1, 0).Activate
Loop
ActiveCell.Value = sum / count
Next c
End Sub

Excel - copying text from one cell to another without deleting original content

Basically I have the following scenareo:
2 columns, with 600 rows of data.
I need to copy the data from column 2 and place it at the end of the content in column1 for the same rows. This would result in column 1 having its original content plus the additional content of column 2.
Any information in how I can do this will be greatly appreciated.
Thanks in advance!
Here's a VBA in a simple form. Just create a macro, add these lines to it. Then select your original column (what you're calling column 1), and run the macro.
a = ActiveCell.Value
b = ActiveCell(1, 2).Value
ActiveCell.Value = a + b
The bracketed cell reference is a relative statement - 1, 2 means "same row, one column to the right" so you can change that if you need. You could make it loop by expanding thusly:
Do
a = ActiveCell.Value
b = ActiveCell(1, 2).Value
ActiveCell.Value = a + b
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then
Exit Do
End If
Loop
That loop will carry on until it finds a blank cell, then it'll stop. So make sure you have a blank cell where you want to stop. You could also add extra characters into the line that combines.. so in the above example it's ActiveCell.Value = a + b, but you could make it ActiveCell.Value = a + " - " + b or anything else that may help.
This should take the values from column 2 and place them sequentially at the bottom of column 1.
Sub test()
Dim rng1 As Range
Dim rng2 As Range
Dim cl As Range
Dim r As Long
Set rng1 = Range("A1", Range("A1048576").End(xlUp))
Set rng2 = Range("B1", Range("B1048576").End(xlUp))
r = rng1.Rows.Count + 1
For Each cl In rng2
Cells(r, 1).Value = cl.Value
r = r + 1
Next
End Sub
Just keep it simple. Here is the code.
Sub copyCol()
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
Range("B1:B" & lasrow).Copy Range("A" & lastRow).Offset(1, 0)
End Sub
As this question received a number of views (10,000+) I thought it was important to also share another and far simpler solution:
In cell C1 use the formula:
=(A1 & B1)
This will copy the content of cell A1 and B1 into cell C1. Drag the formula to all other rows (row 600 in my case).
Then copy the column and paste using 'values only'.
You will then have cells in column C containing the content of column A and column B in a single cell on a row-to-row basis.

Resources