Macro to Copy and Paste Cell Contents

  • Thread starter Thread starter Thorson
  • Start date Start date
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!
 
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
 
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
 
Back
Top