Copy Numbers in Visual Basic (with a line break)

pecsenye

New Member
Joined
Jan 17, 2023
Messages
14
Hello everyone,
I would like to solve the following in Visual Basic: In column A, there are two numbers one below the other (with a line break). I want to create a copy of them. Only the top number should be in column C, and only the bottom number should be in column D. It's IMPORTANT that the numbers can have different lengths (for example, 2, 31, 153, etc.) and they are not always the same length.
Thanks for helping!

exc2.png
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hello! I think the formula would also be appropriate, but I will write it after I have tried it. :)
 
Upvote 0
Then maybe like below?
Book1
ABCDE
1543 29995432999<<< Suitable for other than Excel 365
2
312 2563122563<<< Suitable for Excel 365
Sheet6
Cell Formulas
RangeFormula
C1C1=LEFT(A1,FIND(CHAR(10),A1)-1)
D1D1=MID(A1,FIND(CHAR(10),A1)+1,99)
C3C3=TEXTBEFORE(A3,CHAR(10))
D3D3=TEXTAFTER(A3,CHAR(10))

HTH
 
Upvote 0
Change references as and where required.
Code:
Sub Try()
Dim i As Long
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Cells(i, 1).Offset(, 2).Resize(, 2).Value = Split(Cells(i, 1), Chr(10))
    Next i
End Sub
 
Upvote 0
Then maybe like below?
Book1
ABCDE
1543 29995432999<<< Suitable for other than Excel 365
2
312 2563122563<<< Suitable for Excel 365
Sheet6
Cell Formulas
RangeFormula
C1C1=LEFT(A1,FIND(CHAR(10),A1)-1)
D1D1=MID(A1,FIND(CHAR(10),A1)+1,99)
C3C3=TEXTBEFORE(A3,CHAR(10))
D3D3=TEXTAFTER(A3,CHAR(10))

HTH
Due to the versions and language differences, Visual Basic would be better. These don't work for me. Although the Excel is old.
 
Upvote 0
Due to the versions and language differences, Visual Basic would be better. These don't work for me. Although the Excel is old.

There's no reason why that formula shouldn't work. I have a very old version of Excel and it works perfectly. Which version of Excel are you using? Did you check to make sure that you copied & pasted the formula correctly? You should update your forum profile to display the version of Excel you're using.
 
Upvote 0
If you want the best of both worlds, half Post#4 (using formulae) and half Post#5 (using macro) you can try this.
Code:
Sub With_Formula()
Application.ScreenUpdating = False
With Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    .Offset(, 2).Formula = "=Left(RC[-2], Find(CHAR(10), RC[-2])-1)"
    .Offset(, 3).Formula = "=Mid(RC[-3], Find(CHAR(10), RC[-3])+1, 99)"
    .Offset(, 2).Resize(, 2).Value = .Offset(, 2).Resize(, 2).Value
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Change references as and where required.
Code:
Sub Try()
Dim i As Long
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Cells(i, 1).Offset(, 2).Resize(, 2).Value = Split(Cells(i, 1), Chr(10))
    Next i
End Sub
This solution is good, thank you very much!
 
Upvote 0
If you want the best of both worlds, half Post#4 (using formulae) and half Post#5 (using macro) you can try this.
Code:
Sub With_Formula()
Application.ScreenUpdating = False
With Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    .Offset(, 2).Formula = "=Left(RC[-2], Find(CHAR(10), RC[-2])-1)"
    .Offset(, 3).Formula = "=Mid(RC[-3], Find(CHAR(10), RC[-3])+1, 99)"
    .Offset(, 2).Resize(, 2).Value = .Offset(, 2).Resize(, 2).Value
End With
Application.ScreenUpdating = True
End Sub
Thank you very much, I can use this better for the task! Everything works fine! Thank you so much to you and everyone!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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