Creating Macro: Copy/Paste info from current sheet>Opens new sheet>Returns to previous sheet

%3CLINGO-SUB%20id%3D%22lingo-sub-2082186%22%20slang%3D%22en-US%22%3ECreating%20Macro%3A%20Copy%2FPaste%20info%20from%20current%20sheet%26gt%3BOpens%20new%20sheet%26gt%3BReturns%20to%20previous%20sheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2082186%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20guys!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20recorded%20a%20macro%20that%20applies%20to%20my%20current%20sheet%20open.%20It%20then%20copies%202%20columns%20(uses%20absolute%20reference)%2C%20creates%20a%20new%20sheet%2C%20pastes%20the%202%20columns%2C%20%3CEM%3Eand%20the%20troublesome%20part%3A%20%3C%2FEM%3EI%20want%20it%20to%20return%20to%20the%20previous%20sheet%20and%20copy%201%20more%20column%20from%20there.%20%3CU%3E%3CSTRONG%3EThe%20problem%3C%2FSTRONG%3E%3C%2FU%3E%3A%20it%20uses%20the%20original%20sheet%20name%20that%20I%20had%20open%20when%20I%20recorded%20the%20macro%20to%20copy%20the%20cell%20values%20and%20returns%20to%20(opens)%20the%20%22previous%22%20sheet%20(the%20original%20sheet%20that%20I%20used%20when%20the%20macro%20was%20recorded)%20and%20then%20it%20shows%20a%20dialogue%20box%20saying%20something%20went%20wrong.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20I%20need%20help%20with%20is%20editing%20the%20VBA%20script%20to%20make%20it%20versatile%20and%20applicable%20with%20any%20sheet%2C%20and%20not%20use%20the%20absolute%20reference.%20(Btw%2C%20I%20recorded%20the%20macro%20as%20relative%20cell%20reference)%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22JohnnyB12_0-1611233600172.png%22%20style%3D%22width%3A%20716px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F248027iDA4A112A94D4CBC4%2Fimage-dimensions%2F716x171%3Fv%3D1.0%22%20width%3D%22716%22%20height%3D%22171%22%20role%3D%22button%22%20title%3D%22JohnnyB12_0-1611233600172.png%22%20alt%3D%22JohnnyB12_0-1611233600172.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20is%20GREATLY%20appreciated!!%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fhtml%2Femoticons%2F1f64f_1f3fb.png%22%20alt%3D%22%3Afolded_hands%3A%22%20title%3D%22%3Afolded_hands%3A%22%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2082186%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

Hello guys!

 

I've recorded a macro that applies to my current sheet open. It then copies 2 columns (uses absolute reference), creates a new sheet, pastes the 2 columns, and the troublesome part: I want it to return to the previous sheet and copy 1 more column from there. The problem: it uses the original sheet name that I had open when I recorded the macro to copy the cell values and returns to (opens) the "previous" sheet (the original sheet that I used when the macro was recorded) and then it shows a dialogue box saying something went wrong.

 

What I need help with is editing the VBA script to make it versatile and applicable with any sheet, and not use the absolute reference. (Btw, I recorded the macro as relative cell reference)

JohnnyB12_0-1611233600172.png

 

Any help is GREATLY appreciated!!

1 Reply

@JohnnyB12  because you inserted an image instead of text of sample file, instead of recreating your code I just wrote a new function:

 

Function Step71()
    Dim source, dest As Worksheet
    
    Set source = ActiveSheet
    Set dest = ActiveWorkbook.Sheets.Add(, source)
    dest.Range("a:b").Formula = source.Range("f:g").Formula
    dest.Range("j:j").Value = source.Range("o:o").Value
    dest.name = "My New Sheet"
    
End Function

 

notice how I declare "source" and "dest" as Worksheet type

then set source to present activesheet and then dest as the new added sheet (note the part in () is to add the new sheet after the source sheet, you could delete that if you want it before that sheet)

then I assign the columns "a:b" in the destination to be a copy of the formulas found in "f:g" of the source (WARNING: it will copy the formula as they are and not shift them so formula in g2 looking at f2 in the new sheet will still be looking at f2)

then I assign the column "j:j" to be the VALUES from column O:O

and for fun I set the NAME of the new sheet = "My New Sheet"

I hope that makes it easier for you and you learn a little about VBA