+ Reply to Thread
Results 1 to 12 of 12

copy cell to another cell, increment and copy back.

  1. #1
    Registered User
    Join Date
    04-16-2012
    Location
    adelaide
    MS-Off Ver
    Excel 2010
    Posts
    12

    Question copy cell to another cell, increment and copy back.

    Hi Folks,

    Ill apologise straight up as Im sure this is pretty simple, but I have been playing with this for hours and searching the net with no luck yet.

    I am trying to make a purchase order spreadsheet. As a part of this I want the purchase order number to increment each time the master is used - intention is to use the master to fill in details, then save it as another sheet. Values such as PO number and date can increment in the master but not in the saved copy.

    So I want to have a cell (J2) with a static value (starting at 0).
    When a button is pressed, the macro copies the value in J2 to cell G2, increments the value by +1 and then copies the returned value in G2 back to J2.
    At the end of the process above cell G2 will show '1' and cell J2 will show '1'.

    Ive managed to do this with the date range. I entered the value =TODAY() into cell Z1 and used the following macro
    Range("Z1").Select
    Selection.Copy
    Range("G2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    But this obviously does not help with my sum function for the purchase order number.

    Thank you

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: copy cell to another cell, increment and copy back.

    Hi
    is this what you're after:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-16-2012
    Location
    adelaide
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: copy cell to another cell, increment and copy back.

    Thank you very much NickyC thats fits perfectly!

  4. #4
    Registered User
    Join Date
    04-16-2012
    Location
    adelaide
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: copy cell to another cell, increment and copy back.

    Is there an easy way to have this data in another workbook and access it from there?
    The trouble with the above is that I forgot to take into account that when the file is saved the original will not increment the order number. So it seems I will need to store the order number in a separate workbook and increment it to and from there.

    All code Ive seen for this appears to require fairly lengthy if and with statements.
    Is there a simple way to increment this cell in another workbook, then copy the result to this workbook?

    Thank you.

  5. #5
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: copy cell to another cell, increment and copy back.

    you can, but it is probably not the easiest way to do what you're trying to do.

    If you want to generate copies of the master for each purchase order, why not have a macro that increases the purchase order automatically in the master, saves it as the master, then creates a copy of the master and saves it with a different name as your purchase order.

    If that's not what you're trying to do, you could try posting a copy of your workbook.

  6. #6
    Registered User
    Join Date
    04-16-2012
    Location
    adelaide
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: copy cell to another cell, increment and copy back.

    Thank you NickyC, good idea.

    I really want to try to lock this down so that its hard to break. People here are not computer literate and this is why I wanted to have date and Purchase order as auto fill fields that they cannot screw up.

    I like your idea of incrementing the number and then saving the master, Im working on how to do this but figured I should post the workbook here in case there is stuff I am doing wrong.
    its been a long time since I have played with Excel formatting and VBA and its not like riding a bike

    Thanks
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    04-16-2012
    Location
    adelaide
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: copy cell to another cell, increment and copy back.

    Ok I think Ive got it nutted out.
    Does this look correct? Ive modified your code so that the macro prints the file, then saves the user copy, then clears user content and saves the master to preserve the order number....phew!
    (I may have some of it commented out, sorry if that makes things more difficult)


    Sub cmdGetSaveAsName_Click()
    ' print order then save file

    Dim save_as As Variant
    Dim file_name As String

    file_name = Sheets("Purchase Orders").Range("G2")

    ' print file
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
    IgnorePrintAreas:=False

    ' Get the file name.
    save_as = Application.GetSaveAsFilename([file_name], _
    FileFilter:="Excel Files,*.xlsm,All Files,*.*")
    ' See if the user canceled.
    If save_as = False Then Exit Sub

    ' Save the file with the new name.
    If LCase$(Right$(save_as, 4)) <> ".xlsm" Then
    file_name = save_as & ".xlsm"
    End If
    ActiveWorkbook.SaveAs Filename:=save_as

    ' clear contents of workbook
    Range("G2").Select
    Selection.ClearContents
    Range("G5").Select
    Selection.ClearContents
    Range("B9:B13").Select
    Selection.ClearContents
    Range("E9:G13").Select
    Selection.ClearContents
    Range("A16:G23").Select
    Selection.ClearContents
    Range("E25:F26").Select
    Selection.ClearContents

    ' save master file
    ActiveWorkbook.SaveAs "C:\Users\stuart\Desktop\ATR Purchase Orders.xlsm", FileFormat:=52
    End Sub
    Attached Files Attached Files
    Last edited by Tathagata; 04-18-2012 at 12:07 AM.

  8. #8
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: copy cell to another cell, increment and copy back.

    looks like you're on the right track
    the macros are fairly simple, you may need to think about sequencing - at what point do you want to change the order number, for example. Also, do you want to clear all the ext from the previous
    Let me know if you need some help with the macros

  9. #9
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: copy cell to another cell, increment and copy back.

    ah - should have refreshed before posting -I'll look at it now

  10. #10
    Registered User
    Join Date
    04-16-2012
    Location
    adelaide
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: copy cell to another cell, increment and copy back.

    ok sorry for the junk above. This is the correct, tested, working spreadsheet. I have protected it as it would be for general use (password is 'a')

    apologies for not uploading a good version before.
    Attached Files Attached Files
    Last edited by Tathagata; 04-18-2012 at 12:24 AM.

  11. #11
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: copy cell to another cell, increment and copy back.

    Seems fine. I didn’t test the save macro as the file paths are obviously set up for your directories.

    A couple of tips (not necessary, just simplifying):

    In Macroaaaaa:
    Rather than entering range addresses it can be helpful to name ranges in the sheet and then use these for your clear contents macros. This means that if you add or remove lines and columns etc the macro should still work.

    To do this, hold down the control key and use the mouse to select the ranges to be cleared (this allows non-adjacent cells to be selected), and enter a name like clear_range into the address box

    Then you can run a single line of code

    Please Login or Register  to view this content.
    In Call_Today:
    No need to hide the date in the sheet, a simple:

    Please Login or Register  to view this content.
    Will do

    Again, this reduces the risk of your macros not working if the sheet is altered

  12. #12
    Registered User
    Join Date
    04-16-2012
    Location
    adelaide
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: copy cell to another cell, increment and copy back.

    Thank you very much NickyC, that all works really well now. I have applied your suggestions (kinda, I couldnt work out your clear_range part, but instead just used the whole range to clearcontents which is much nicer than I had.
    I had thought the date() woudlnt work as I thought it would place the formula in the cell, but this isnt the case so it works great as you have proposed.

    Thanks again, I really appreciate your help. Cheers!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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