+ Reply to Thread
Results 1 to 12 of 12

Problem with Range.Formula in VBA

  1. #1
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Göteborg
    MS-Off Ver
    2010
    Posts
    130

    Problem with Range.Formula in VBA

    Below is my code that is a part of a bigger code. The problem here is that in column O there is a date in the following format yyyyww, for example 201645.
    In my formula I would like to take the previous week so 201645 will be 201644 which is working as intended. The problem is that when a date is for example 201701 then I want the formula to give me 201652 but right now it gives me 201700 because IF(Right(O2,2)=01 is not true since it reads 01 as only 1. How can I fix this? Its giving me some headache.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Problem with Range.Formula in VBA

    Try

    Please Login or Register  to view this content.
    Kind regards
    Leo

  3. #3
    Registered User
    Join Date
    06-17-2012
    Location
    Australia
    MS-Off Ver
    MS Office 365
    Posts
    40

    Re: Problem with Range.Formula in VBA

    Hi Gandreso,

    Small change to your formula using quotations will give you the desired outcome.

    Please Login or Register  to view this content.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Problem with Range.Formula in VBA

    Personally I think you'd be better advised to use proper dates in your workbook rather than hybrid dates which appear to refer to a year and a week number. Can't you use say week ending dates, or week commencing dates and use a formula that deducted 7 days from the previous date? That is going to be far more flexible.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Göteborg
    MS-Off Ver
    2010
    Posts
    130

    Re: Problem with Range.Formula in VBA

    First of all thanks for the responses.

    @bradl822 - I was thinking about the same but the formula row gives me a syntax error. I think it has to be that you cant use quotations inside a quotations.
    .Formula = "=IF(Right(O2,2)="01",CONCATENATE(LEFT(O2,4)-1,52),O2-1)"

  6. #6
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Göteborg
    MS-Off Ver
    2010
    Posts
    130

    Re: Problem with Range.Formula in VBA

    @Richard Buttrey - I totally agree with you Richard. The problem is that the data comes from an datasystem that has just that dateformat. But I agree, proper dates would be much more flexible.

  7. #7
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Problem with Range.Formula in VBA

    Other 1

    Please Login or Register  to view this content.
    Cheers
    Leo

  8. #8
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Göteborg
    MS-Off Ver
    2010
    Posts
    130

    Re: Problem with Range.Formula in VBA

    @LeoTaxi - Your code is working as I hoped for. The thing is that my datafile is about 300000 rows atleast, which meant that I wanted to avoid looping through the data if possible to avoid slow code. But I have run the code and your loop is not as slow as I thought it would be. Im gonna do some more tests, I come back later. Anyway thank you all.

  9. #9
    Registered User
    Join Date
    06-17-2012
    Location
    Australia
    MS-Off Ver
    MS Office 365
    Posts
    40

    Re: Problem with Range.Formula in VBA

    Sorry, my bad. Untested.

    Try with double quotations, I believe I have done this before.
    .Formula = "=IF(Right(O2,2)=""01" ",CONCATENATE(LEFT(O2,4)-1,52),O2-1)"

  10. #10
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Problem with Range.Formula in VBA

    See #7


    Cheers
    Leo

  11. #11
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Göteborg
    MS-Off Ver
    2010
    Posts
    130

    Re: Problem with Range.Formula in VBA

    Oh I forgot about that I have to use double quotations. Both of the below is working so thanks! But which of the two should be run the fastest?
    Please Login or Register  to view this content.

  12. #12
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Problem with Range.Formula in VBA

    Quote Originally Posted by gandreso View Post
    @Richard Buttrey - I totally agree with you Richard. The problem is that the data comes from an datasystem that has just that dateformat. But I agree, proper dates would be much more flexible.
    Hi,

    OK, understood, but notwithstanding I'd be inclined to have a pre-processing macro that creates a proper Excel date number from the datasystem 'dates in another column, and then use the proper date numbers in your macro so that you future proof and can use the dates for other analysis stuff should that ever be necessary.

    So for instance with the system 'date' 201645 in A1 and assuming week 1 2016 is Monday 4 Jan

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    will return the date November 14 2016. Adjust the number 4 in the formula as appropriate for a different January 2016 w/c date.

+ 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. [SOLVED] Challenging Problem - IndexMatch Formula with Dynamic Range
    By nobodyukno in forum Excel General
    Replies: 2
    Last Post: 05-07-2015, 08:40 AM
  2. [SOLVED] Copying Formula / Range Adjustment Problem
    By dramadog in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-15-2013, 02:20 PM
  3. IF formula working with a range... Problem!
    By dwilkinson12 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-25-2013, 09:43 AM
  4. Problem with incorporating data range in formula
    By ahmad_fou in forum Excel General
    Replies: 1
    Last Post: 08-13-2011, 02:54 PM
  5. Problem with incorporating data range in formula,
    By ahmad_fou in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2011, 12:10 PM
  6. range formula problem
    By Bassist in forum Excel General
    Replies: 1
    Last Post: 06-18-2008, 12:28 PM
  7. cell vs range problem with formula
    By Martin Skrenek in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-14-2005, 12:06 PM

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