VBA Code to Paste Data based on end row of coumn b

fishandtril

New Member
Joined
Nov 15, 2022
Messages
13
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello

I need some help with my macro to paste data.
I am copying a value from C2 into Column A but only want to paste values to cells which contain data in column B
I normally use Shift Ctrl arrow down to find last cell but as this is column A it returns the very last cell in Excel (A1048575)

What do I need to do to only paste as far as the last cell in column B

Code:
Range("C2").Select
    Selection.Copy
    Range("A17").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range("A17:A1048575").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
 

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).
Hi,

would something like this fit your needs (if I understood your question well) ?

Essentially it finds the last row of data in ColB, then checks each row (from 2 to the last row) for some data in "C". If data is found, it puts value of cell "C2" into the relevant Row in ColA.

VBA Code:
Sub copy_data()

last_row = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row  'check for last row used in Col 2 ("B")

For x = 2 To last_row

    If ActiveSheet.Range("B" & x).Value <> "" Then
   
        Range("A" & x) = Range("C2")
   
    End If

Next x

End Sub

Book1
ABCD
1
2mydata
3mydatadata
4mydatadata
5
6mydatadata
7
8mydatadata
Sheet1
 
Upvote 0
Thank you for your assistance.

Is there away to Ignore data in rows 1-16?

My headings are row 16 and I only want to paste from A17 to last row.
 
Upvote 0
yes, of course - in this case you would change the count to start from 17 instead of (2). That number is effectively the row number you'll start from.

VBA Code:
Sub copy_data()

last_row = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row  'check for last row used in Col 2 ("B")

For x = 17 To last_row

    If ActiveSheet.Range("B" & x).Value <> "" Then
   
        Range("A" & x) = Range("C2")
   
    End If

Next x

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,243
Members
449,093
Latest member
Vincent Khandagale

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