Macro to copy data

G

Gustavo Strabeli

Hi!

Can anybody help me with a macro that checks all empty cells in a column and copies the data from the column beside?
I'll show you an example

A B
1 MYPKG IDJKT
2 ZADUR
3 KRPUS KRICH
4 CNSHA
5 HKHKG


So, I want a macro that checks all empty cells in column A, i.e., A2, A4 and A5, and then copy the data from column B.
In this sense, A2 would have the same as B2.
A4 would have the same as B4
A5 would have the same as B5
and so on, and so on...

Thanks beforehand.

Gustavo.
 
G

Guest

Try this
Sub copyColumB()
Const testCol = "A"
Const copyCol = "B"
Dim MySheet As Worksheet, i As Long
Dim lastrow As Long

Set MySheet = ActiveSheet
lastrow = MySheet.Cells(Rows.Count, testCol).End(xlUp).Row
For i = 1 To lastrow
If MySheet.Range(testCol & i).Value = "" Then
MySheet.Range(testCol & i).Value = MySheet.Range(copyCol & i).Value
End If
Next i
End Sub
 
G

Guest

Try this one also
Sub copyColumB()
Const testCol = "A"
Const copyCol = "B"
Dim MySheet As Worksheet, i As Long
Dim lastrow As Long

Set MySheet = ActiveSheet
lastrow = MySheet.Cells(Rows.Count, copyCol).End(xlUp).Row
For i = 1 To lastrow
If MySheet.Range(testCol & i).Value = "" Then
MySheet.Range(testCol & i).Value = MySheet.Range(copyCol & i).Value
End If
Next i
End Sub
 
G

Gustavo Strabeli

Mike,
It's working perfectly.
Thanks a lot for the help.

Regards,
Gustavo.


"Mike" <[email protected]> escreveu na mensagem Try this one also
Sub copyColumB()
Const testCol = "A"
Const copyCol = "B"
Dim MySheet As Worksheet, i As Long
Dim lastrow As Long

Set MySheet = ActiveSheet
lastrow = MySheet.Cells(Rows.Count, copyCol).End(xlUp).Row
For i = 1 To lastrow
If MySheet.Range(testCol & i).Value = "" Then
MySheet.Range(testCol & i).Value = MySheet.Range(copyCol & i).Value
End If
Next i
End Sub
 

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