VBA - After new row pasted, make value in particular cell zero

sdh966

New Member
Joined
May 8, 2018
Messages
2
I have the below code working perfectly but I need value in cell column "N" of the new pasted row(s) to be zero. But I need the value in "N" of the original copied row to retain it's value. below is my current code. You can ignore the comments. I was trying different places.
Code:
Sub CopyData()
'Updateby Extendoffice 20160922
    Dim xRow As Long
    Dim VInSertNum As Variant
    xRow = 1
    Application.ScreenUpdating = False
    Do While (Cells(xRow, "A") <> "")
        VInSertNum = Cells(xRow, "J")
        If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
           Range(Cells(xRow, "A"), Cells(xRow, "AN")).Copy
       '    Cells(xRow, 14).Value = 0 this did all rows
           Range(Cells(xRow + 1, "A"), Cells(xRow + VInSertNum - 1, "AN")).Select
       '    Cells(xRow, 14).Value = 0
           'this did all rows
           Selection.Insert Shift:=xlDown
        '   Cells(xRow, 14).Value = 0 this did the first row only
           xRow = xRow + VInSertNum - 1
        'Cells(xRow - 1, 14).Value = 0
        End If
'         Cells(xRow - 1, 14).Value = 0
        xRow = xRow + 1
 '        Cells(xRow + 1, 14).Value = 0
    Loop
    'Cells(xRow, 14).Value = 0 this did no rows
    Application.ScreenUpdating = False
End Sub
 
Last edited by a moderator:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi & welcome to MrExcel.
How about
Code:
Sub CopyData()
    Dim xRow As Long
    Dim VInSertNum As Variant
    xRow = 1
    Application.ScreenUpdating = False
    Do While Cells(xRow, "A") <> ""
        VInSertNum = Cells(xRow, "J")
        If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
           Rows(xRow).Copy
           Rows(xRow + 1).Resize(VInSertNum - 1).Insert shift:=xlDown
           Cells(xRow + 1, 14).Resize(VInSertNum - 1).Value = 0
           xRow = xRow + VInSertNum - 1
        End If
        xRow = xRow + 1
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Excellent. Thanks so much. I added one more line in bold to zero out column "O" also.

Sub CopyData()
Dim xRow As Long
Dim VInSertNum As Variant
xRow = 1
Application.ScreenUpdating = False
Do While Cells(xRow, "A") <> ""
VInSertNum = Cells(xRow, "J")
If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
Rows(xRow).Copy
Rows(xRow + 1).Resize(VInSertNum - 1).Insert shift:=xlDown
Cells(xRow + 1, 14).Resize(VInSertNum - 1).Value = 0
Cells(xRow + 1, 15).Resize(VInSertNum - 1).Value = 0
xRow = xRow + VInSertNum - 1
End If
xRow = xRow + 1
Loop
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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