+ Reply to Thread
Results 1 to 7 of 7

changing range value in run time

Hybrid View

  1. #1
    Registered User
    Join Date
    08-29-2010
    Location
    eindhoven
    MS-Off Ver
    Excel 2010
    Posts
    62

    changing range value in run time

    Dear All,

    I have a simple macro below :

    Sub Macro2()

    ActiveCell.Offset(-5, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "TASK"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-3],""-"",RC[-2])"
    Selection.AutoFill Destination:=ActiveCell.Range("A1:A5")

    End Sub

    Above code does following :

    - go to col-d , types TASK
    - perform concat on col-a, col-b text and returns value
    - it works fine as long as i have 5 records, where is there increase in no of records it gives error.

    How to give the cell range based on no records present in data

    regards,
    jsimha

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: changing range value in run time

    Hi jsimha,

    the code is strangely written. for example the first statement will only work if the cursor is in row 6 (i.e. ActiveCell), because you are telling the system to go up 5 rows from the current position and one to the right. So for you to put the value "TASK" in column D the cursor must be in column C and at least row 6, and the row determined where the value TASK will be written.

    ActiveCell.Offset(-5, 1).Range("A1").Select
    Would you be able to describe in words what you are trying to achieve here?
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    08-29-2010
    Location
    eindhoven
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: changing range value in run time

    Hi Olaf,

    I am trying to concatenation in the excel via macro

    See the excel workbook attached, in the column-d from row 42 till the down i see the formula concatenate exist.
    Which i do not want. I want concatenate should perform only till the data exist.

    I don't want range to be hard coded in macro. is there anyway it calculated automatically?

    Regards,
    Jay
    Attached Files Attached Files

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: changing range value in run time

    or write the formula assignment line as if you find that more readable

    .Cells(lngRowNumber, "D").Formula = Replace("=CONCATENATE(A$1,B$1)", "$1", lngRowNumber)

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: changing range value in run time

    Try (after clearing all old entries)

    '####################################################################################################################################
    '# introduce concatenation formula for populated rows
    '####################################################################################################################################
         Public Sub IntroduceFormula()
              '# declare
                   Dim lngRowNumber As Long
              '# process all rows from row 2 till the last row entry in column A
                   With ThisWorkbook.Worksheets("Sheet1")
                        For lngRowNumber = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
                             .Cells(lngRowNumber, "D").Formula = "=CONCATENATE(A" & lngRowNumber & ",B" & lngRowNumber & ")"
                        Next lngRowNumber
                   End With
         End Sub

  6. #6
    Registered User
    Join Date
    08-29-2010
    Location
    eindhoven
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: changing range value in run time

    Thanks Olaf, working fine.
    have a nice day.

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: changing range value in run time

    jsimha,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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