Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Copy Paste

0

I am working with US Zips and Canadian Zips.  I can code in to find the start of the Canadian zips (let's say row 150,000) and add a zero to the low end of the range and a 9 to the high end of the range, both in two cells immediately left of the first listed Canadian Province (NL).  I would like to copy those two cells that contain a formala down to the end of the data and copy nothing above.

Answer
Discuss

Discussion

Rappj

I've added an Answer below but please edit your question title to something more instructive (to others) .e.g referring to copying a formula in VBA  (rather than just "Copy Paste")
John_Ru (rep: 6142) Mar 7, '24 at 8:44 am
Add to Discussion

Answers

0
Selected Answer

Hi Rappj

You didn't attach a file but please do so in future- it nearly always helps us and you to get the right answer (when creating or editting your original question, you can use the Add Files... button to upload a representative Excel file -without any personal data- to show your existing data and any macros). 

Not sure what kind of formula you have but the VBA PasteSpecial method (for Range) can be used to copy formulae as follows:

The attached file has:

  1. some repeats of Canadian province codes, in blue cells starting with "NL" at D5 (you can add many more)
  2. green cells B5:C5 to the left of that, containing illustrative formulae only, as below
  3. B5 adds of "0-" in front of of the code using
    ="0-" & D5
    e.g. "0-NL"
  4. C5 adds "-9" after the code using
    ="=D5 & "-9"
    e.g. "NL-9"
  5. empty cells below that in columns B:C

The orange button titled "Copy green cell formulae" is linked to this code in Module 1 (with comments for guidance):

Sub CopyCanPostCode()

    Dim LstRw As Long, StrtCll As Range

    ' emulate user code to find first Canadian code...
    Set StrtCll = Range("D5")
    '... and find last row
    LstRw = Cells(Rows.Count, StrtCll.Column).End(xlUp).Row

    ' pick the cell 2 to the left of the first Canadian cell
    With StrtCll.Offset(0, -2)
        ' copy that and the cell to the right of it
        .Resize(1, 2).Copy
        ' copy their formulae from the next row down to end of range
        .Offset(1, 0).Resize(LstRw - StrtCll.Row, 2).PasteSpecial xlPasteFormulas
    End With

    ' remove the dotted copy lines
    Application.CutCopyMode = False

End Sub

(where the early lines in bold replace whatever code you use to find the first-used Canadian province).

If you click that orange button, you'll find the formulae from B5 and C5 are copied to the cells below. There could be thousands of entries in D (or wherever).

To use it, you'll need use your code to determine StrtCll (=start cell)

Hope this fixes your problem. If so, please be sure to mark this Answer as Selected.

Discuss

Discussion

Thanks for the advice.  I tried to add the file yesterday but was having issues with attaching it.  I also tried copying in vba code, but then realized I didn't know where to begin.  This is long code and the first part calls in a .txt. file.  The second part scrubs it which is where the copy and paste portion started.  Once again, thanks for your assistance.
rappj (rep: 4) Mar 7, '24 at 9:39 am
Glad that helped and thanks for selecting my Answer, Rappj. You can't attach .txt files (but can embed them within Excel files- which can be uploaded). There's a limit on Question (and Answer) length but I don't know what that is- this isn't my site.
John_Ru (rep: 6142) Mar 7, '24 at 11:00 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login