Need help with VBA Macro for copy / pasting a cell value into other select cells

shukero

Board Regular
Joined
Dec 3, 2015
Messages
64
Office Version
  1. 365
Platform
  1. Windows
Good afternoon everyone,

I need help with developing a macro which will paste a value from within a cell into all other cells which have values within the column to the immediate left. (Please see screenshot below)

An example would be:
I have a "trim" formula in Cell D2, and I need to copy that formula (using a VBA macro) into any cell in column D which has a value in the cell to the immediate left. (please see screenshot below)

6idCTEs.png


Can someone please help me with this?

Thanks!
~Mike
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Code:
Sub copyFormula()
Dim lr As Long, c As Range
With ActiveSheet
    lr = .Cells(Rows.Count, 3).End(xlUp).Row
    For Each c In .Range("D3:D" & lr)
        If c.Offset(, -1).Value > "" Then
            .Range("D2").Copy c
        End If
    Next
End With
End Sub
 
Upvote 0

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,707
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Try:
Code:
Sub Copyformula()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Range("C3:C" & LastRow)
        If rng <> "" Then
            rng.Offset(0, 1)= Range("D2").FormulaR1C1
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

shukero

Board Regular
Joined
Dec 3, 2015
Messages
64
Office Version
  1. 365
Platform
  1. Windows
Thank you both very much, I ended up using JLGWhiz's code since it seemed to be a bit less complex and used a bit less code. (That and I can somewhat understand the logic in it for tweaking if needed)

I will like both of your replies. THANK YOU!
 
Upvote 0

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Thank you both very much, I ended up using JLGWhiz's code since it seemed to be a bit less complex and used a bit less code. (That and I can somewhat understand the logic in it for tweaking if needed)

I will like both of your replies. THANK YOU!
Thanks for acknowledging all the effort.
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,191,197
Messages
5,985,231
Members
439,952
Latest member
djharter

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
Top