If VBA with copy

crazybuckeyeguy

New Member
Joined
Apr 15, 2017
Messages
49
Trying to write VBA that looks for blank cell in column A. if blank copy the cell directly and to column G above it and paste in the blank cells. Im a little stuck. here is what I have.

Sub Copyname()
Application.ScreenUpdating = False
For MY_ROWS = 1 To Range("A" & Rows.Count).End(xlUp).Row
If Range("A" & MY_ROWS).Value = "" Then
Range("A:G" & MY_ROWS).Value = Range("A:G" & MY_ROWS - 1).Value
End If
Next MY_ROWS
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this
When working with a range you need to specify the row reference for "both ends" of that range !!

Code:
Sub Copyname()
Application.ScreenUpdating = False
For MY_ROWS = 1 To Range("A" & Rows.Count).End(xlUp).Row
If Range("A" & MY_ROWS).Value = "" Then
Range("A" & MY_ROWS & ":G" & MY_ROWS).Value = Range("A" & MY_ROWS & ":G" & MY_ROWS - 1).Value
End If
Next MY_ROWS
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this
When working with a range you need to specify the row reference for "both ends" of that range !!

Code:
Sub Copyname()
Application.ScreenUpdating = False
For MY_ROWS = 1 To Range("A" & Rows.Count).End(xlUp).Row
If Range("A" & MY_ROWS).Value = "" Then
Range("A" & MY_ROWS & ":G" & MY_ROWS).Value = Range("A" & MY_ROWS & ":G" & MY_ROWS - 1).Value
End If
Next MY_ROWS
Application.ScreenUpdating = True
End Sub


Of Course! thank you for your help Michael.
 
Upvote 0
I think theres a typo in there though...try this

Code:
Sub Copyname()
Application.ScreenUpdating = False
For MY_ROWS = 1 To Range("A" & Rows.Count).End(xlUp).Row
If Range("A" & MY_ROWS).Value = "" Then
Range("A" & MY_ROWS & ":G" & MY_ROWS).Value = Range("A" & MY_ROWS[color=red]-1 [/color]& ":G" & MY_ROWS - 1).Value
End If
Next MY_ROWS
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
.. and really all the codes would be better to start at row 2, not row 1.
If row 1 always has something in column A, it is just a waste checking anyway
and if it is possible that A1 is blank then the current codes will error.
 
Upvote 0
I think theres a typo in there though...try this

Code:
Sub Copyname()
Application.ScreenUpdating = False
For MY_ROWS = 1 To Range("A" & Rows.Count).End(xlUp).Row
If Range("A" & MY_ROWS).Value = "" Then
Range("A" & MY_ROWS & ":G" & MY_ROWS).Value = Range("A" & MY_ROWS[COLOR=red]-1 [/COLOR]& ":G" & MY_ROWS - 1).Value
End If
Next MY_ROWS
Application.ScreenUpdating = True
End Sub
I had to go and make this more complicated. I need to change it to something like this. But I need it to paste as;
PasteSpecial Paste:=xlPasteValues.
when it just mirrors the values it is changing the formatting from general to time and I need it to stay as general and this was the only solution I found.

Sub Copyname()
Application.ScreenUpdating = False
For MY_ROWS = 1 To Range("A" & Rows.Count).End(xlUp).Row
If Range("A" & MY_ROWS).Value = "" Then
Range("A" & MY_ROWS).Value = Range("A" & MY_ROWS - 1).Value
Range("c" & MY_ROWS).Value = Range("h" & MY_ROWS).Value
Range("e" & MY_ROWS).Value = Range("j" & MY_ROWS).Value
Range("g" & MY_ROWS).Value = Range("l" & MY_ROWS).Value
End If
Next MY_ROWS
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
This should preserve the format
Code:
Sub Copyname()
Dim My_ROWS As Long
Application.ScreenUpdating = False
For My_ROWS = 2 To Range("A" & Rows.Count).End(xlUp).Row
If Range("A" & My_ROWS).Value = "" Then
Range("A" & My_ROWS - 1).Copy Range("A" & My_ROWS)
Range("h" & My_ROWS).Copy Range("c" & My_ROWS)
Range("j" & My_ROWS).Copy Range("e" & My_ROWS)
Range("l" & My_ROWS).Copy Range("g" & My_ROWS)
End If
Next My_ROWS
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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