Macro to Copy and Paste Cell Contents

T

Thorson

I am new to writing VBA code in Excel:

I have a simple spreadsheet with 2 columns. The first column has an animal
ID, the second has the animal data. The first column has 1 cell filled in
for a set of 2-6 cells in the second column, therefore below the filled in
cell are blank cells until the next animal ID.

I would like to create a simple macro that will search the first column (A),
when it finds a cell that is blank I would like it to copy the cell above it
and paste it into that cell. If the cell is not blank then leave it as is.
This is all on the first sheet of my workbook (Sheet1).

If someone could help me write the code for this that would be great, I'm
assuming it should be that hard. Thanks!
 
P

Per Jessen

Hi

Try this:

Sub CopyID()
Sheets("Sheet1").Select
FirstRow = 2 ' Headers in row 1
LastRow = Range("B" & Rows.Count).End(xlUp).Row
For r = 2 To LastRow
If Cells(r, 1) = "" Then
Cells(r, 1) = Cells(r - 1, 1).Value
End If
Next
End Sub

Regards,
Per
 
T

Thorson

Works perfectly Thank you!
--
Thorson


Per Jessen said:
Hi

Try this:

Sub CopyID()
Sheets("Sheet1").Select
FirstRow = 2 ' Headers in row 1
LastRow = Range("B" & Rows.Count).End(xlUp).Row
For r = 2 To LastRow
If Cells(r, 1) = "" Then
Cells(r, 1) = Cells(r - 1, 1).Value
End If
Next
End Sub

Regards,
Per
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top