Avatar of Braulio cordova
Braulio cordova

asked on 

copy same as above data

Hi All,

I am almost there by I need to do some tweaking to a code. Currently I have a macro that copies that above information so that the end user doesn't have to type the info twice. My problem is that the code copies the entire row above but I need it to only copy from column "B" to column "R". Attached is a sample file. Your help is greatly appreciated
Sample-Request-Form.xlsm
Microsoft OfficeMicrosoft ExcelVBA

Avatar of undefined
Last Comment
Shums Faruk
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try add:

Cells(Selection.Row, "A") = ""
  Cells(Selection.Row, "S") = ""

Open in new window


after:

Rows(Selection.Row - 1).Copy
  Rows(Selection.Row).Insert Shift:=xlDown

Open in new window

You may try something like this....
Sub New_Delta()
    Dim r As Long
    ' Go to last cell
    r = Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
    
    ' Copy formula from cell above
    Range(Cells(r - 1, "B"), Cells(r - 1, "R")).Copy
    
    Range("B" & r).Insert shift:=xlDown
End Sub

Open in new window

Avatar of Shums Faruk
Shums Faruk
Flag of India image

Hi,

Another approach would be :
Sub InsertCopyRow()
Dim Rng As Range
Set Rng = Range("B12").EntireColumn.Find("*", SearchOrder:=xlRows, SearchDirection:=xlPrevious)
If Not Rng Is Nothing Then Application.Goto Rng
    Rng.Offset(1, 0).EntireRow.Insert
    Rng.EntireRow.Copy Rng.Offset(1, 0).EntireRow
End Sub

Open in new window

Avatar of Braulio cordova
Braulio cordova

ASKER

Hi All,

Thanks for looking into this. As of now Shums' suggestion works great., however the reason I wanted to only copy from B to R is because there is formula in column "S" that I did not want to alter. For some reason the when using Shum's code the formula on column S is shifting down one row. Any thoughts?

Thanks!
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Hi Braulio,

I did included Column S formula to shift down, so you don't need to add manually.
Avatar of Shums Faruk
Shums Faruk
Flag of India image

You can delete all the manual formula which you currently have down the row. My code will pull down the formula from above row.
Avatar of Braulio cordova

ASKER

My Apologies,

I meant to say that Subodh Tiwari (Neeraj)'s suggestion is what that I am using. Shums when I use your suggestion I get this message

User generated image
Avatar of Shums Faruk
Shums Faruk
Flag of India image

My Macro name was not New_Delta
My code is below:
Sub InsertCopyRow()
Dim Rng As Range
Set Rng = Range("B12").EntireColumn.Find("*", SearchOrder:=xlRows, SearchDirection:=xlPrevious)
If Not Rng Is Nothing Then Application.Goto Rng
    Rng.Offset(1, 0).EntireRow.Insert
    Rng.EntireRow.Copy Rng.Offset(1, 0).EntireRow
End Sub

Open in new window

Avatar of Braulio cordova

ASKER

Shums,

Thanks almost there but your code is including column Columns "S" .
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Please try attached. I deleted formula in S, when you click same as above, it will insert new row and copy down with formula.
Sample-Request-Form_v1.xlsm
Avatar of Braulio cordova

ASKER

Hi Shums,

See image below.. It's on an image do I can illustrate the red 2User generated image
Avatar of Shums Faruk
Shums Faruk
Flag of India image

OK, I got it, it was bringing the wrong formula down.

Please try attached...
Sample-Request-Form_v2.xlsm
Avatar of Braulio cordova

ASKER

it is still overlapping the formula... if you look at column S  starting at S14 formula shows +2, S15 +3  S16 +3 and so on...
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Neeraj's Code is working perfect, just use his code and credit him
Sub New_Delta()
    Dim r As Long
    ' Go to last cell
    r = Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
    
    ' Copy formula from cell above
    Range(Cells(r - 1, "B"), Cells(r - 1, "R")).Copy
    
    Range("B" & r).Insert shift:=xlDown
    Application.CutCopyMode = False
    
End Sub

Open in new window

Avatar of Braulio cordova

ASKER

All,

Just following up here... I am still having issues with this code. While Nerraj's is working great I need the function to be copy and paste instead of insert. If we use insert my formulated column "S" gets shifted. Is there a way to accomplish this using copy and paste with code?

Thanks in advance for looking into this. File attached
Sample-Request-Form.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Braulio cordova

ASKER

Thank you Shums!!!

This worked like a charm.
Avatar of Shums Faruk
Shums Faruk
Flag of India image

You're Welcome Braulio! Glad I was able to help.
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo