Code to copy formula to last row of data

usui

Board Regular
Joined
Apr 20, 2020
Messages
55
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi Guys,

I'm trying to do a concatenate on some data that would base on how many data present on column A (header not included) and column B1-F1 (header not included), I tried recording a macro but unfortunately even though column A is only counts until A16(not fix number and may changes depends on data needed), I needed the cells starting from B2(fix)-F1000(can be change) and then remove the formula after (paste Value only).

The issue is since range was set to B2:F1000, the concatenate did not stop on the last data on column A (A16) and continued until it reach cell 1000.

I wanted a code that if concatenate reach the last data on column A (A16-can be change) will automatically stop there instead.

Sample here: I don't want the cells on yellow
1664436155947.png


Here is the code I got after recording macro, I changed the range until F1000 since data needed may changes on column A

Sub Concat()

ActiveCell.FormulaR1C1 = "=CONCATENATE(RC1,""-"",R1C)"
Range("B2").Select
Selection.Copy
Range("B2:F1000").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

I hope someone can help modify this code or if anyone who has a better code instead, this should be the formula use: =CONCATENATE($A2,"-",B$1)
Thank you in advance
 
Try this
VBA Code:
Option Explicit
Sub usui_V1()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<< change to suit
  
    With ws.Range("B2:F" & ws.Cells(Rows.Count, "A").End(xlUp).Row)
        .FormulaR1C1 = "=RC1&""-""&R1C"
        .Value = .Value
    End With
End Sub

Before
usui.xlsm
ABCDEF
1SquareWF-CAN-12x12WF-CAN-20x20WF-CAN-30x30WF-CAN-36x36WF-CAN-48x48
21
32
43
54
65
76
87
98
109
1110
1211
1312
1413
1514
1615
17
Sheet1


After
usui.xlsm
ABCDEF
1SquareWF-CAN-12x12WF-CAN-20x20WF-CAN-30x30WF-CAN-36x36WF-CAN-48x48
211 - WF-CAN-12x121 - WF-CAN-20x201 - WF-CAN-30x301 - WF-CAN-36x361 - WF-CAN-48x48
322 - WF-CAN-12x122 - WF-CAN-20x202 - WF-CAN-30x302 - WF-CAN-36x362 - WF-CAN-48x48
433 - WF-CAN-12x123 - WF-CAN-20x203 - WF-CAN-30x303 - WF-CAN-36x363 - WF-CAN-48x48
544 - WF-CAN-12x124 - WF-CAN-20x204 - WF-CAN-30x304 - WF-CAN-36x364 - WF-CAN-48x48
655 - WF-CAN-12x125 - WF-CAN-20x205 - WF-CAN-30x305 - WF-CAN-36x365 - WF-CAN-48x48
766 - WF-CAN-12x126 - WF-CAN-20x206 - WF-CAN-30x306 - WF-CAN-36x366 - WF-CAN-48x48
877 - WF-CAN-12x127 - WF-CAN-20x207 - WF-CAN-30x307 - WF-CAN-36x367 - WF-CAN-48x48
988 - WF-CAN-12x128 - WF-CAN-20x208 - WF-CAN-30x308 - WF-CAN-36x368 - WF-CAN-48x48
1099 - WF-CAN-12x129 - WF-CAN-20x209 - WF-CAN-30x309 - WF-CAN-36x369 - WF-CAN-48x48
111010 - WF-CAN-12x1210 - WF-CAN-20x2010 - WF-CAN-30x3010 - WF-CAN-36x3610 - WF-CAN-48x48
121111 - WF-CAN-12x1211 - WF-CAN-20x2011 - WF-CAN-30x3011 - WF-CAN-36x3611 - WF-CAN-48x48
131212 - WF-CAN-12x1212 - WF-CAN-20x2012 - WF-CAN-30x3012 - WF-CAN-36x3612 - WF-CAN-48x48
141313 - WF-CAN-12x1213 - WF-CAN-20x2013 - WF-CAN-30x3013 - WF-CAN-36x3613 - WF-CAN-48x48
151414 - WF-CAN-12x1214 - WF-CAN-20x2014 - WF-CAN-30x3014 - WF-CAN-36x3614 - WF-CAN-48x48
161515 - WF-CAN-12x1215 - WF-CAN-20x2015 - WF-CAN-30x3015 - WF-CAN-36x3615 - WF-CAN-48x48
17
Sheet1
1664442178709.png
error sir
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You are very welcome
And thank you for the feedback
BE happy and safe
 
Upvote 0
Try
VBA Code:
Sub Concat()
    With Range("B2:F" & Cells(Rows.Count, 1).End(xlUp).Row)
    .Select
        .FormulaR1C1 = "=CONCATENATE(RC1,""-"",R1C)"
        .Value = .Value
    End With
End Sub
Works great sir, thank you so much for this.
 
Upvote 0
Hi Sir, it works but the issue is that I need to RUN the macro twice in order for it to work..1st RUN it just shows highlighted cells then if I RUN it again (2nd Run) then everything shows up and good.

Is this normal that I need to RUN it twice before the data appears?

1st Run:
View attachment 75027
2nd Run:
View attachment 75028
Hi.. not sure why but I see you did get sorted with a much shorter code... have a good day further:cool:
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,114
Members
453,021
Latest member
Justyna P

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