How to Make Dynaminc Autofill and pasting as valuein VBA

ygoyal578

New Member
Joined
Apr 26, 2020
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to write a code such that it could work in dynamic range. But it is not executing. I have tried below code:

I'm trying to do:

1. Combine to columns (A and B) in to column C till last value in Column A. Then pasting the values of Column C in A and then Deleting column B and Column C.

Wanted to make it dynamic


Columns("C:C").Select
Range("C3").Activate
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C4").Select
ActiveCell.FormulaR1C1 = "=RC[-2]&""_""&RC[-1]"
Range("C4").Select
Selection.AutoFill Destination:=Range("C4")
Range("C4:C4").Select
Selection.Copy
Range("A4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Replace What:=" ", Replacement:="_", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("B:C").Select
Range("B3").Activate
Selection.Delete Shift:=xlToLeft
Range("A3").Select
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this:
VBA Code:
Sub MyCombine()

    Dim lr As Long
    
    Application.ScreenUpdating = False
    
'   Insert a blank column at column C
    Columns("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Write formulas to combine A and B in column C, starting at row 4
    Range("C4:C" & lr).Formula2R1C1 = "=RC[-2]&""_""&RC[-1]"
    
'   Make formulas into hard-coded values
    Range("C4:C" & lr).Value = Range("C4:C" & lr).Value
    
'   Delete columns A and B
    Columns("A:B").Delete
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
thanks
Try this:
VBA Code:
Sub MyCombine()

    Dim lr As Long
   
    Application.ScreenUpdating = False
   
'   Insert a blank column at column C
    Columns("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
   
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
   
'   Write formulas to combine A and B in column C, starting at row 4
    Range("C4:C" & lr).Formula2R1C1 = "=RC[-2]&""_""&RC[-1]"
   
'   Make formulas into hard-coded values
    Range("C4:C" & lr).Value = Range("C4:C" & lr).Value
   
'   Delete columns A and B
    Columns("A:B").Delete
   
    Application.ScreenUpdating = True

End Sub
thanks
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,914
Members
449,054
Latest member
luca142

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