Advertisement

06.12.2008 at 05:45AM PDT, ID: 23479191
[x]
Attachment Details

Excel Macro - Range, multiple columns

Asked by Runski in Microsoft Excel Spreadsheet Software, VB Script

Tags: VB

Hi all experts.

I have a macro that is designed to copy cells based on a cell value. I need help to select the range of what needs to be copied. Now it only copies content from column "F". I need to be able to select multiple columns - "F" & "K" & "I".

Here is the macro as of today.

Sub CopyToColumnB()

    Dim LRow As Integer
    Dim LQty As Integer
    Dim LProduct As String
    Dim LColCPosition As Integer
    Dim j As Integer
    Dim LStart As Integer
    Dim LEnd As Integer
   
    'Search for values in column B starting at row 2
    LRow = 1

    'Copy values to column C starting at row 2
    LColCPosition = 1

    'Search through values in column B until a blank cell is encountered
    While Len(Range("A" & CStr(LRow)).Value) > 0

        'Retrieve quantity and product name
        LQty = Range("L" & CStr(LRow)).Value
        LProduct = Range("F" & CStr(LRow)).Value

        'Set start and end position for copy to column C
        LStart = LColCPosition
        LEnd = LColCPosition + LQty

        'Copy product name the number of times that is given by the quantity
        For j = LStart To LEnd - 1
            Range("B" & CStr(j)).Value = LProduct
        Next

        'Update column C position
        LColCPosition = LEnd

        LRow = LRow + 1

    Wend

    MsgBox "Column B has been populated."

End Sub
Start Free Trial
[+][-]06.12.2008 at 08:02AM PDT, ID: 21770220

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Excel Spreadsheet Software, VB Script
Tags: VB
Sign Up Now!
Solution Provided By: nike_golf
Participating Experts: 1
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628