Results 1 to 2 of 2

Thread: VBA Excel Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    1

    VBA Excel Help

    Hi Folks,

    Hopefully someone here might be able to help me.

    Currently i have a spreadsheet with a number of columns. For example the columns include 'Description', 'Product', 'Quantity' and 'Price'. The quantity column, for instance, can sometimes have a value of over one (two, three, eight or even ten or twenty). And herein lies the problem!

    Ideally I would like each quantity to have its unique row in the spreadsheet with the quantity being "1" right the way through the sheet.

    I wrote a short script to handle this, however, my big problem is dividing up the 'Price' column, as it represents Total Cost for a quantity. Currently my script copies and pastes the entire row depending on the quantity. For instance if the quantity were 3, the row would be copied twice and display 3 seperate rows. However the 'Price' would still remain as a total cost for each unit.

    Would anyone know how I would split the Total Cost cell, in accordance with the quantity? For example if the quantity was 5 and the total cost was 500. How would i split the single row into 5 separate rows while also having a Price of 100 for each?

    Does this make sense to anyone? I really hope I explained it correctly as I've been immersed in this for a few days now and its driving me insane!



    VBA:
    Code:
    Function Match_Data_Master() 
        Worksheets("Sheet3").Select 
        Sheets("Sheet3").Range("d1").Select 
        I = 1 
        ref = ActiveCell.Value 
        Dim REFBACK As String 
        Dim PRODUCT As Integer 
        While ActiveCell <> "" Or IsNull(ActiveCell) 
            If ActiveCell.Value = 0 Then 
                ActiveCell.Offset(0, 5).Value = 0 
            Else 
                ActiveCell.Offset(0, 5).Value = 1 
                PRODUCT = PRODUCT + 1 
            End If 
            ref = ActiveCell.Value 
            REFBACK = ActiveCell.Value 
            If ref > 1 Then 
                Dim j As Variant 
                j = ref 
                While j <> 1 
                    Rows(ActiveCell.Row).Select 
                    Selection.Copy 
                    Selection.Insert Shift:=xlDown 
                    ActiveCell.Offset(1, 0).Select 
                    j = j - 1 
                    PRODUCT = PRODUCT + 1 
                Wend 
                ActiveCell.Offset(0, 3).Select 
            End If 
            ActiveCell.Offset(1, 0).Select 
            I = I + 1 
        Wend 
         'Range("J1").Select
         'Application.CutCopyMode = False
         'ActiveCell.FormulaR1C1 = "=SUM(J[-1])"
         'Range("J1").Select
        Dim Lr As Long 
        Lr = Cells(Rows.Count, "i").End(xlUp).Row + 1 
        Cells(Lr, "i").Formula = "=SUM(I1:J" & Lr - 1 & ")" 
         'Columns("C:C").Select
         'Selection.Delete Shift:=xlToLeft
        MsgBox "Number of Products:  " & PRODUCT 
    End Function
    Thanks for anyone who read the post, its pretty long but if anyone has any tips or pointers about where to go i'd be very very grateful!

    Cheers
    Last edited by si_the_geek; Oct 6th, 2010 at 03:46 AM. Reason: added Code tags

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Excel Help

    totalcost / quantity?? for each row
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width