+ Reply to Thread
Results 1 to 3 of 3

Using Varying Range Size in Macro

  1. #1
    Charles Allen
    Guest

    Using Varying Range Size in Macro

    I'm trying to create a macro that does the following steps.

    1. Inserts a column in front of an existing column of data.
    2. Enters a constant value in each cell of the new column for every
    filled cell of the existing column.

    I can do this the first time. However, I don't know how to make the
    macro dynamic to take into account varying lengths of the existing
    column. It may be 10 rows one time and 20 rows the second time.

    The data might look like this:
    Column A
    Row 1 $100
    Row 2 $200
    Row 3 $300

    After the macro, the data would look like this:
    Column A Column B
    Row 1 1234 $100
    Row 2 1234 $200
    Row 3 1234 $300

    The next time, there will be more or less rows in Column A.

    Thank you for your help.


  2. #2
    Bob Phillips
    Guest

    Re: Using Varying Range Size in Macro

    Try something like

    With ActiveCell
    Columns(.Column).Insert
    Cells(1, .Column - 1).Value = 1234
    iLastRow = Cells(Rows.Count, .Column).End(xlUp).Row
    Cells(1, .Column - 1).AutoFill Range(Cells(1, .Column - 1),
    Cells(iLastRow, .Column - 1))
    End With


    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Charles Allen" <[email protected]> wrote in message
    news:[email protected]...
    > I'm trying to create a macro that does the following steps.
    >
    > 1. Inserts a column in front of an existing column of data.
    > 2. Enters a constant value in each cell of the new column for every
    > filled cell of the existing column.
    >
    > I can do this the first time. However, I don't know how to make the
    > macro dynamic to take into account varying lengths of the existing
    > column. It may be 10 rows one time and 20 rows the second time.
    >
    > The data might look like this:
    > Column A
    > Row 1 $100
    > Row 2 $200
    > Row 3 $300
    >
    > After the macro, the data would look like this:
    > Column A Column B
    > Row 1 1234 $100
    > Row 2 1234 $200
    > Row 3 1234 $300
    >
    > The next time, there will be more or less rows in Column A.
    >
    > Thank you for your help.
    >




  3. #3
    Valued Forum Contributor
    Join Date
    07-11-2004
    Posts
    851
    you can figure out your range by:

    you know your first row (I assume) - let's say it is 1

    your last row for column a is - lets' call this lastrow

    lastrow =cells(1,1).end(xldown).row

    so now

    range(cells(1,2),cells(lastrow,2)).entirecolumn.insert

    will insert a column where column B was for this range of rows

    for your constant, lets say you place it in cell e1 - better yet in acell with a named range "entry" - this way after the insert you still know where it is

    so, fill via the following

    for i =1 to lastrow
    cells(2,i).value = range("entry").value
    next i

    Good Luck!
    not a professional, just trying to assist.....

+ 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