+ Reply to Thread
Results 1 to 5 of 5

Macro or VBA to add a new column with values based on existing data

  1. #1
    Registered User
    Join Date
    03-22-2021
    Location
    Durham, England
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (16.0.13801.20288) 64-bit
    Posts
    2

    Macro or VBA to add a new column with values based on existing data

    Hi All,

    I have a simple dataset, see columns A to D in attached file. This is sorted into Item No. and Date order ascending. I want to create a macro that adds a new column (E) called Forecast and then does the following...

    1. For the first row for each change in Item No. is just copies the Inventory value (D) from that row to the new column

    2. For each subsequent row for each Item No. it does the following calculation based on values from the previous row (column E - column C)

    In the example file the result of cell E3 is E2-C2 and result of E4 is E3-C3 and so on and so on.

    Any help would be very much appreciated.

    Thanks in advance
    Tim
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro or VBA to add a new column with values based on existing data

    Put this in E2 and copy it down the column
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    03-22-2021
    Location
    Durham, England
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (16.0.13801.20288) 64-bit
    Posts
    2

    Re: Macro or VBA to add a new column with values based on existing data

    Thanks so much, thats worked

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro or VBA to add a new column with values based on existing data

    You're welcome.

    The formula doesn't consider the last quantity for each item number e.g. C12

    I think you would want this formula to take into account the last quantity before a change in the Item No.

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-10-2021
    Location
    Germany
    MS-Off Ver
    Office 365
    Posts
    5

    Re: Macro or VBA to add a new column with values based on existing data

    Try that

    Sub Subsequent()
    Dim i As Long
    Range("E2") = Range("D2")
    For i = 2 To ActiveSheet.UsedRange.Rows.Count
    If Cells(i, 5) <> "" Then
    Cells(i + 1, 5) = Cells(i, 5) - Cells(i, 3)
    End If
    Next i
    End Sub

    if something wrong inform me please.

+ 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. Replies: 4
    Last Post: 12-09-2019, 07:30 PM
  2. Replies: 7
    Last Post: 05-20-2019, 10:34 PM
  3. Replies: 4
    Last Post: 03-19-2019, 10:30 AM
  4. Automating content insertion based upon existing data values
    By steve777888 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2013, 04:34 PM
  5. Macro to sum values in one column based on data in another colum while ignoring blanks
    By The Machinist in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-15-2012, 03:25 PM
  6. [SOLVED] Creating new field with conditional values based on existing data
    By DrNerj in forum Excel General
    Replies: 3
    Last Post: 07-30-2012, 02:54 PM
  7. Changing values based on existing data
    By Eddie Lives On in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 01-24-2011, 10:56 AM

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