Macro for copying cell from column B if column A is not blank

stevewood1

New Member
Joined
Oct 11, 2018
Messages
16
Hello,

I wonder if you can help.

I have a worksheet with a list of dates is column A and an amount in Cell B2.

I need a Macro that will copy the value of B2 and paste it into every cell in column B where the corresponding cell in column A is not blank.

I have seen various code for doing similar things but not one for this exact scenario and I haven't managed to alter any of the ones that I have found to do this.

Many thanks,

Steve
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Code:
Sub copyValues()

    Dim s As String, c As Range
    
    s = Range("B2").Value
    For Each c In Range("A1", Cells(Rows.Count, 1).End(xlUp))
        If LenB(c.Value) Then c(1, 2).Value = s
    Next c
End Sub
 
Upvote 0
Thank you for the really quick response.

I have another sheet where I have to do the same thing but the dates are in row E10 and down and the value to copy is F10. I thought I would just be able to duplicate the code and amend the range but when I try the numbers are going in random cells.

Thanks you for all your help.

Steve
 
Upvote 0
Are your dates hard values, or the result of a formula?
 
Upvote 0
Ok, I've just changed it a bit so you can adapt it as you see fit.

Code:
Sub copyValues()

    Dim c As Range, pasteValue As String, dateCol As String, pastCol As String
    
    dateCol = "A"
    pastecol = "B"
    pasteValue = Range("B2").Value
    
    For Each c In Range(dateCol & "1", Cells(Rows.Count, dateCol).End(xlUp))
        If LenB(c.Value) Then Range(pastecol & c.Row).Value = pasteValue
    Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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