+ Reply to Thread
Results 1 to 14 of 14

Overwriting cells when re-running macro

  1. #1
    Registered User
    Join Date
    01-22-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    7

    Overwriting cells when re-running macro

    Hello,

    I am using the following macro to split data from one worksheet into multiple worksheets within the same file. Basically, the macro takes the data that I have in column A and creates a separate tab for each different item found. It then copies all rows with that item name into the new tab. The first issue I have is that I want the data to copy to row 15 instead of row 2 in the new worksheets...in order to make room for header information. Secondly, I would like the previous data to be overwritten each time I re-run the macro instead of going down to the next available blank line. It is causing me to have duplicated data. Can someone help me add/change the code in this macro to do these things?

    Thank you.



    Sub Splitdatatosheets()

    ' Splitdatatosheets Macro

    Dim rng As Range

    Dim rng1 As Range

    Dim vrb As Boolean

    Dim sht As Worksheet

    Set rng = Sheets("Query").Range("A2")

    Set rng1 = Sheets("Query").Range("A2:I2")

    vrb = False

    Do While rng <> ""

    For Each sht In Worksheets

    If sht.Name = Left(rng.Value, 31) Then

    sht.Select

    Range("A2").Select

    Do While Selection <> ""

    ActiveCell.Offset(1, 0).Activate

    Loop

    rng1.Copy ActiveCell

    ActiveCell.Offset(1, 0).Activate

    Set rng1 = rng1.Offset(1, 0)

    Set rng = rng.Offset(1, 0)

    vrb = True

    End If

    Next sht

    If vrb = False Then

    Sheets.Add After:=Sheets(Sheets.Count)

    ActiveSheet.Name = Left(rng.Value, 31)

    Sheets("Query").Range("A2:I2").Copy ActiveSheet.Range("A2")

    Range("A2").Select

    Do While Selection <> ""

    ActiveCell.Offset(1, 0).Activate

    Loop

    rng1.Copy ActiveCell

    Set rng1 = rng1.Offset(1, 0)

    Set rng = rng.Offset(1, 0)

    End If

    vrb = False

    Loop

    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Overwriting cells when re-running macro

    Try the attached file. I had to make a few changes to eliminate some merged cells. They almost always give Excel problems and should be avoided if possible. You also had some named ranges which I deleted because for some reason they were causing problems. Do not delete the "Blank Form" as this is the sheet that is copied. Please let me know if this works for you.
    Attached Files Attached Files
    Last edited by Mumps1; 01-22-2015 at 05:03 PM.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Overwriting cells when re-running macro

    You could try this code

    Please Login or Register  to view this content.
    Alf

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Overwriting cells when re-running macro

    Hi mumps1

    Your code much better than mine!!

    But time difference is a pain ... I checked before I posted and then there was no answer to this thread but when I now check I see that you posted 24 minutes earlier than me??? Strange

    Took me a while before I found out there was a hidden sheet named "Misc." that the OP never mentioned. Tsk, tsk

    Alf

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Overwriting cells when re-running macro

    Hi Alf. It's always good to get more than one option. Thank you for your feedback.

  6. #6
    Registered User
    Join Date
    01-22-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    7

    Re: Overwriting cells when re-running macro

    Worked great! Thanks guys.

  7. #7
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Overwriting cells when re-running macro

    Glad it worked out.

  8. #8
    Registered User
    Join Date
    01-22-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    7

    Re: Overwriting cells when re-running macro

    Hello. It's me again. The macro works great! However, I was wondering if someone could help me add one more thing. I need to add another worksheet called "Recon" that will add certain cells of the new worksheets together. So could I have the macro add the new worksheets between two pre-existing worksheets called "Beg" and "End"?

    Thanks

  9. #9
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Overwriting cells when re-running macro

    If you now have two additional sheets , called "Beg" and "End", and you want to add another called "Recon", then the previous macro has to be changed. It might be a good idea if you could attach a copy of your updated file and include a detailed explanation of what you want to do.

  10. #10
    Registered User
    Join Date
    01-22-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    7

    Re: Overwriting cells when re-running macro

    Here is my updated file...

    Thank you

  11. #11
    Registered User
    Join Date
    01-22-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    7

    Re: Overwriting cells when re-running macro

    Let me try that again
    Attached Files Attached Files

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Overwriting cells when re-running macro

    Replace this line:
    Please Login or Register  to view this content.
    with this line:
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    01-22-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    7

    Talking Re: Overwriting cells when re-running macro

    Perfect! Thank you!

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Overwriting cells when re-running macro

    My pleasure.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Stop overwriting cells - Copy and paste macro
    By juhaszp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2013, 05:37 PM
  2. Macro to hide/unhide cells not running or running slow
    By mbp727 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2013, 04:22 PM
  3. Copying cells after last cell, with out overwriting data in cells above.
    By Cyberpawz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2012, 02:23 PM
  4. VBA not overwriting cells
    By mdavid800 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-07-2011, 12:43 PM
  5. Overwriting Cells
    By Keith bedford in forum Excel General
    Replies: 4
    Last Post: 03-09-2005, 08:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1