+ Reply to Thread
Results 1 to 3 of 3

Is there anything wrong with using 'Range.Formula = ' ?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-20-2018
    Location
    UK
    MS-Off Ver
    2018
    Posts
    7

    Question Is there anything wrong with using 'Range.Formula = ' ?

    Hi everyone,

    To preface this, my VBA knowledge is essentially 0, but otherwise I'm fairly confident with formulas in Excel -- so sorry if this is a bit of a weird question.

    I'm looking to automate cleaning some documents where users can easily submit phone numbers and other values in the wrong format. I can think of plenty of ways to do this with formulas like CLEAN, TRIM, SUBSTITUTE etc, but I have no idea how I'd go about doing it in VBA. Normally I'd just fill a formula down, but this cleaning exercise is required multiple times a day, hence why a macro makes sense.

    So let's say I just add something like this:

    Sub CleanMe()
    Dim LastRowColumnA As Long
    LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
    Range("B1:B" & LastRowColumnA).Formula = "=LEFT(A1,3)"
    End Sub
    Assume that cleaned all the data in A:A. Is there anything wrong with using this method? I'm not really asking for help on writing a text cleaning macro, I'm just asking whether there's any downside to using this method? I only ask because it seems a little strange to me to insert formulas inside a macro when I'm sure there must be some clever ways of doing it with VBA.

    Thank you!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,728

    Re: Is there anything wrong with using 'Range.Formula = ' ?

    No, nothing wrong with it.

    Sub CleanMe()
    Dim LastRowColumnA As Long
    LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
    With Range("B1:B" & LastRowColumnA)
        .Formula = "=LEFT(A1,3)"
        .Value = .Value     ' convert formula to values
    End With
    End Sub
    You can just post the formula for the top row/cell and it will auto-update. Note, if you have double quotes, you need to double them up. So, " becomes "".
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Is there anything wrong with using 'Range.Formula = ' ?

    Formulas are used all the time in macro code. I see it all the time and most commonly used inside of string variables that are on the right side of equal signs when assigning those variables to cell values. Furthermore, if you want to know everything about the property you are trying to use and whether it makes sense or has any risk associated with it, why not take a look at Microsoft's knowledge base article on it? It is here....

    https://docs.microsoft.com/en-us/off....range.formula

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Wrong formula-calculation of the bullet's range
    By chipxx in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-27-2020, 01:24 PM
  2. [SOLVED] Wrong formula or wrong function?
    By cpope in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-08-2017, 10:27 AM
  3. [SOLVED] Trying To Name A Range. What Am I Doing Wrong
    By rmccain in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 08-25-2016, 05:51 PM
  4. [SOLVED] List inserts auto formula with wrong range
    By ahartman in forum Excel General
    Replies: 4
    Last Post: 06-19-2012, 10:24 AM
  5. Code to select range and open a user form is opening the wrong range
    By rrbest in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-05-2010, 12:34 PM
  6. What is wrong with this range description
    By petca059 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2010, 04:40 AM
  7. [SOLVED] FORMULA at Source range (in Data->validation->List) gives wrong re
    By Eddy Stan in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-17-2006, 03:25 AM

Tags for this Thread

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