+ Reply to Thread
Results 1 to 9 of 9

VBA: Auto-fill cells based on user defined values and quantity

Hybrid View

  1. #1
    Registered User
    Join Date
    03-01-2018
    Location
    Washington
    MS-Off Ver
    2013
    Posts
    19

    VBA: Auto-fill cells based on user defined values and quantity

    Hello,

    I'm trying to figure out a way using VBA where a user can specify values in 2 adjacent cells with a quantity in a 3rd cell, hit a button and have it insert the values entered a number of times equal to the quantity specified.

    Sample Input:
    Product Price QTY
    Dog Socks $2.95 4
    Cat Mittens $1.99 4

    Sample Output:
    Product Price
    Dog Socks $2.95
    Dog Socks $2.95
    Dog Socks $2.95
    Dog Socks $2.95
    Cat Mittens $1.99
    Cat Mittens $1.99
    Cat Mittens $1.99
    Cat Mittens $1.99


    The actual product list has 50+ products and the prices can vary so using formulas isn't ideal. There can also be up to 30 different types of product per order. I figure the best approach is using VBA and having the output values specified by the user, then executing it based on the # entered into the quantity, and outputting the values into the "next empty cell" on the output sheet.

    Sample workbook attached.
    Attached Files Attached Files

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

    Re: VBA: Auto-fill cells based on user defined values and quantity

    Try:
    Private Sub Submit_Click()
        Application.ScreenUpdating = False
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim x As Long
        For x = 2 To LastRow
            Cells(x, 1).Resize(, 2).Copy Sheets("Output").Cells(Sheets("Output").Rows.Count, "C").End(xlUp).Offset(1, 0).Resize(Cells(x, 3))
        Next x
        Application.ScreenUpdating = True
    End Sub
    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
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,086

    Re: VBA: Auto-fill cells based on user defined values and quantity

    See next code
    Option Explicit
    Private Sub Submit_Click()
    Dim WsIn As Worksheet
    Dim Wsout As Worksheet
    Dim LR  As Integer, I  As Integer
    Const FR  As Integer = 2
    Dim Rg  As Range
        Set WsIn = Sheets("Input")
        Set Wsout = Sheets("Output")
        With WsIn
            LR = .Cells(Rows.Count, "C").End(3).Row
            For Each Rg In Range(.Cells(FR, "C"), .Cells(LR, "C"))
                Rg(1, -1).Resize(1, 2).Copy
                   Wsout.Cells(Rows.Count, "C").End(3)(2, 1).Resize(Rg, 1) _
                       .PasteSpecial Paste:=xlPasteValues
            Next Rg
        End With
        MsgBox (" Job Done")
    End Sub
    Last edited by PCI; 03-23-2018 at 02:54 PM. Reason: Macro assigned to button
    - Battle without fear gives no glory - Just try

  4. #4
    Registered User
    Join Date
    03-01-2018
    Location
    Washington
    MS-Off Ver
    2013
    Posts
    19

    Re: VBA: Auto-fill cells based on user defined values and quantity

    Mumps1
    That's awesome! Thank you.

    I'm impressed with how few lines of code it ended up being.

    How hard would it be to have it only paste as values and not carry over the formatting?
    Last edited by Dracius; 03-23-2018 at 03:48 PM.

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

    Re: VBA: Auto-fill cells based on user defined values and quantity

    Try:
    Private Sub Submit_Click()
        Application.ScreenUpdating = False
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim x As Long
        For x = 2 To LastRow
            Cells(x, 1).Resize(, 2).Copy
            Sheets("Output").Cells(Sheets("Output").Rows.Count, "C").End(xlUp).Offset(1, 0).Resize(Cells(x, 3)).PasteSpecial xlPasteValues
        Next x
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,086

    Re: VBA: Auto-fill cells based on user defined values and quantity

    "only paste as values and not carry over the formatting?"
    The code sent, only copies values ...just try it !

  7. #7
    Registered User
    Join Date
    03-01-2018
    Location
    Washington
    MS-Off Ver
    2013
    Posts
    19

    Re: VBA: Auto-fill cells based on user defined values and quantity

    Quote Originally Posted by PCI View Post
    "only paste as values and not carry over the formatting?"
    The code sent, only copies values ...just try it !
    The reply you're quoting was directed at Mumps1, not you, since his initial code didn't paste as values.

    I've tried yours and it works too. Thank you both!

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: VBA: Auto-fill cells based on user defined values and quantity

    Glad to help.

  9. #9
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: VBA: Auto-fill cells based on user defined values and quantity

    Just for reference was also cross-posted here
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

+ 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. VBA error with auto fill of user defined formula
    By dquigley in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2018, 12:20 PM
  2. [SOLVED] Auto-Row Fill based on Quantity of components
    By Blackhawks in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-23-2016, 04:48 PM
  3. [SOLVED] Averaging a user input defined range based on values in columns
    By smit.etha in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-14-2014, 02:21 AM
  4. VBA to select cells based on user defined dates
    By rajkumarmp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2013, 07:09 AM
  5. Need to auto sort tables or adapt formulas to fill cells based on values.
    By dropanddrive03 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-20-2013, 12:21 PM
  6. [SOLVED] Auto fill a cell with a value based on pre-defined range.
    By kraft_mk in forum Excel General
    Replies: 2
    Last Post: 03-19-2013, 03:50 PM
  7. excel automation: auto fill up cell value out of defined matched values on sheet 2
    By randybondoc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-18-2012, 05:01 AM

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