Consulting

Results 1 to 10 of 10

Thread: Solved: Help: Adding function to line of code

  1. #1

    Solved: Help: Adding function to line of code

    Current code:
    [VBA]
    ActiveCell.FormulaR1C1 = "=TRIM(RC[-1])"
    [/VBA]

    How to add PROPER to same line of code to perform in one step?


    YLP

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]ActiveCell.FormulaR1C1 = "=PROPER(TRIM(RC[-1]))" [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    MD,
    Instead of using ActiveCell.Formula, how about starting at E2 and down to last cell in E that has
    data?

    YLP

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Which column is the formula to go in?

    Try this.
    [vba]
    LastRow = Range("E" & Rows.Count).End(xlUp).Row

    Range("F2:F" & LastRow).FormulaR1C1="=PROPER(TRIM(RC[-1]))" [/vba]

  5. #5
    Hi Norie,
    The previous line of code edits column (D) to remove unwanted characters, this would be the same column, (D), to run Proper and Trim on. But it should start in D2 and .End(xlUp)

    Code for reference purposes:
    [VBA]Columns("D").Select
    Selection.Replace What:="~~P ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Selection.Replace What:="~~C ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Selection.Replace What:="~* ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Selection.Replace What:="~~", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False[/VBA]

    YLP

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Sorry but I still can't see which column you want the formula in.

  7. #7
    Norie,
    Sorry. I may be thinking of this incorrectly.
    Column D is where the formula and the results would take place. Can the formula be run and results occur in the same cell? Will this work?

  8. #8
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Sorry, now you've got me really confused.

    The code I posted should place the formula in F for every row where there is data in column E.

    Is that what you want or is it something else?

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi YLP,
    Can you post a spreadsheet indicating your original data and the result you want to see.
    regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    MD,
    I got this resolved. I finally see how the code was working. Thanks to you and Norie.
    I am posting a new topic, but a follow up question regurarding this topic.

    YLP

Posting Permissions

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