Data from 2 cells into one, with specific format

happydonut

Board Regular
Joined
Nov 28, 2019
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have this code to add the value from two cells into one;
VBA Code:
Range("C1").Value = Range("A1").Value & " " & Range("B1").Value

The cell in A1 has a date, e.g. 2021-01-25
The cell in B1 has a number, e.g. 123456789
Goal is to have the value in C1 as MMDD from cell A1 followed by the value in B1, e.g. 0125 123456789

Questions:
1. How do I format the C1 to get the value there as explained?
2. In real life, there could be many rows in A1 (all dates) and B1 (all numbers). How can I make a code that takes all rows into consideration, e.g. A2+B2=C2, A3+B3=C3 etc until last row with a value?

Thanks.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi,

1. How do I format the C1 to get the value there as explained?

Range("C1").Value = FORMAT(Range("A1").Value,"MMDD") & " " & Range("B1").Value

2. In real life, there could be many rows in A1 (all dates) and B1 (all numbers). How can I make a code that takes all rows into consideration, e.g. A2+B2=C2, A3+B3=C3 etc until last row with a value?

You could try something like.

VBA Code:
Sub m()
Dim R As Range
Set R = Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row)
R.Value = Evaluate("TEXT(" & R.Offset(0, -2).Address & ",""MMDD "")&" & R.Offset(0, -1).Address)
End Sub
 
Upvote 0
Hi,



Range("C1").Value = FORMAT(Range("A1").Value,"MMDD") & " " & Range("B1").Value



You could try something like.

VBA Code:
Sub m()
Dim R As Range
Set R = Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row)
R.Value = Evaluate("TEXT(" & R.Offset(0, -2).Address & ",""MMDD "")&" & R.Offset(0, -1).Address)
End Sub
Hi, and thanks for reply.

I just tested it. It works, but it jumps over the first row. So it starts with the second.
 
Upvote 0
Just a follow up question,

What if I want the value to be added in another workbook?

So for instance, Value in A1 and B1 from workbook 1
to value C1 in workbook 2
 
Upvote 0
This assumes that workbook 1 is the active workbook when the macro runs, if that cannot be guaranteed then some changes will need to be made to full qualify all of the ranges.

VBA Code:
Sub m()
Dim R As Range
Set R = Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row)
Workbooks("Book2").ActiveSheet.Range("A1").Resize(R.Rows.Count) = Evaluate("TEXT(" & R.Offset(0, -2).Address & ",""MMDD "")&" & R.Offset(0, -1).Address)
End Sub
 
Upvote 0
Thank you.
Sorry to ask another question, but..

What if there is a filter in workbook 1 (i.e. not all dates visible). How can the macro only take the filtered data from Workbook 1 to Workbook 2?
 
Upvote 0
Hi, it would need a different technique altogether - you'd probably need to loop through the visible cells one by one.
 
Upvote 0
I have this code to add the value from two cells into one;
VBA Code:
Range("C1").Value = Range("A1").Value & " " & Range("B1").Value
Hi, just obviously use Text rather than Value for the source part, not for the destination …​
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,301
Members
448,885
Latest member
LokiSonic

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top