VBA help: copy select characters in string and paste until blank row

snajam23

New Member
Joined
Mar 18, 2018
Messages
7
Hello all,

Hoping someone would know the answer

I would like to copy and paste data within the same sheet like below.

Company: ABC123 in cell A1

I would like to copy the last 6 characters in this string meaning the characters starting from A.

Assume theres data in rows 2 through 4 in columns A B and C. Row 5 is blank.

I would like to paste the data as paste values in column D starting in 2 and stop when it reaches a blank row. Meaning the paste values should stop in row 4.

Hope my description is clear.

Thanks in advance.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:
VBA Code:
Sub Last6()
Dim lR As Long
lR = Range("A" & Rows.Count).End(xlUp).Row
With Range("D1").Resize(lR, 1)
    .Value = Right(Range("A1").Value, 6)
End With
End Sub
 
Upvote 0
Thank you @JoeMo

Looks like the code is copying the value as intended but its pasting the data all way down to the last row in the sheet. It is not stopping before the first blank row.
 
Upvote 0
Thank you @JoeMo

Looks like the code is copying the value as intended but its pasting the data all way down to the last row in the sheet. It is not stopping before the first blank row.
Post your data using XL2BB so we can see the data layout. In the worst case I can think of, col A is empty, including A1, the "paste" would stop after cell D1. Did you modify the code in any way? Did you copy it from your browser and paste it to the VBE or did you re-type it?
 
Upvote 0
so i think i figured out why he paste is going down more than expected rows. According to my explanation i believe my data set looks as image 1 below. i thought the paste values would stop before the blank rows but if there is additional data after a set of blank rows then the paste continues.

Does it make more sense to define the range to paste the values instead of paste until it reaches a blank row?

Image 1
Asset Additions Report 8.18.xlsx
ABCDE
1Company: ABC123ABC123
2abcdefghiABC123
3abcdefghiABC123
4abcdefghiABC123
5
Sheet2


Image 2
Asset Additions Report 8.18.xlsx
ABCDE
1Company: ABC123ABC123
2abcdefghiABC123
3abcdefghiABC123
4abcdefghiABC123
5ABC123
6ABC123
7ABC123
8ABC123
9ABC123
10Company: DEF123ABC123
11abcdefghiABC123
12
Sheet2
 
Upvote 0
You should include details like the possibility of full blank rows within the data.
See if this fixes the problem:
VBA Code:
Sub Last6()
Dim lR As Long
lR = Range("A1").CurrentRegion.Rows.Count
With Range("D1").Resize(lR, 1)
    .Value = Right(Range("A1").Value, 6)
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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