+ Reply to Thread
Results 1 to 18 of 18

Replacing whole date with only "Day" using for next loop or other methods.

  1. #1
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Replacing whole date with only "Day" using for next loop or other methods.

    Hello Everyone!
    Hope you are safe and sound...

    In the column D of the sheet i attached, i have complete dates.
    I want to extract only the day and place that day in the same cell as of complete date, replacing the complete dates with day.
    and i want to do it for the whole column.
    I tried to solve this but type mismatch error arises. Please help...

    Please Login or Register  to view this content.
    Please do note that, before using above code i also performed some other functions on this worksheet i.e deleting blank rows through filtering,
    For that i wrote another code first, after removing those blank rows, i wanted to change the complete dates in "Posting Date" column with only "Day".
    After filtering the blank rows, i have 109 rows, data starting from Row 2.
    Attached Files Attached Files

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Try the below code …

    Please Login or Register  to view this content.
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,153

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Quote Originally Posted by Asad Mir View Post
    Replacing whole date with only "Day" using for next loop or other methods
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Thanks alot.....

  5. #5
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Thanks porucha vevrku!!!

    I have two questions...
    First:
    I couldn't understand this line of code.
    tbl is a variant and what does (i,1) means?
    Please Login or Register  to view this content.
    Second:
    After For Next Loop, you are changing number formatting to General and other things, My question
    here is... can you not do the same inside the loop?

    Clarification....
    Please Login or Register  to view this content.
    What i understand from the above mentioned line of code is that "rws" is last used row of the sheet,
    but, why are you adding 1 to last row?

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,560

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    No loop
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Quote Originally Posted by jindon View Post
    No loop
    Please Login or Register  to view this content.
    Thanks alot Jindon
    A brief explanation would be very helpful and appreciative.
    Actually, could not understand the following part of code.
    Please Login or Register  to view this content.
    .Value = .Parent.Evaluate("index(day(" & .Address & "),,)")
    Please Login or Register  to view this content.
    Last edited by Asad Mir; 06-11-2020 at 11:27 PM.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,560

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    One of the Evaluate method is to calculate the formula.
    e.g
    Please Login or Register  to view this content.
    However when calculation needs Worksheet Function, it need to prepare an Array for individual calculated data, otherwise it only shows the first calculated result you saw in Post #2 in this thread.
    Please Login or Register  to view this content.
    Read the link here which I posted first time and pgc01 explained nicely.

  9. #9
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Quote Originally Posted by jindon View Post
    otherwise it only shows the first calculated result you saw in Post #2 in this thread.
    Actually the code in post #2 works as expected but in Excel 365 as Excel creates the array for you. In normal spread sheet, if I type "=DAY(D2:D130)" in cell G2, it just creates an array of results from G2 until G130 … You can read more HERE if interested

    But thanks jindon for clarifying that the Excel version of the user is something I have to consider next time and maybe it's time to update your Excel version to explore & enjoy the new added features/behaviors

  10. #10
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Can't thank you enough...

  11. #11
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Quote Originally Posted by jindon View Post
    One of the Evaluate method is to calculate the formula.
    e.g
    Please Login or Register  to view this content.
    However when calculation needs Worksheet Function, it need to prepare an Array for individual calculated data, otherwise it only shows the first calculated result you saw in Post #2 in this thread.
    Please Login or Register  to view this content.
    Read the link here which I posted first time and pgc01 explained nicely.
    can't thank you enough...

  12. #12
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Thanks! All of You...

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,560

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    You are welcome and thanks for the rep.

    Please don't forget to mark the thread as "Solved" when your problem is solved.

  14. #14
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,153

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Quote Originally Posted by Asad Mir View Post
    ... tbl is a variant and what does (i,1) means?
    Please Login or Register  to view this content.
    Ad 1. "(i, 1)" means the value of the variable from the "i-th row" and "j-th column" (here j = 1) of the data table.
    When you load data from a worksheet to an array, the internal vbe mechanism creates a row-column layout as in a matrix.
    So when you get data from 1 column and many rows, the layout is created:

    1st row, 1st column
    2nd row, 1st column
    3rd row, 1st column
    ...
    n row, 1st column

    You refer to the individual elements of this layout through their indexes (measures of location).
    In an array variable ("tbl(i,j)"), this index will be a certain symbolic variable (here "i"), which will increase according to the rhythm of successive, repeated references to it (loops, cycles).

    Ad 2. Variables in vb (and in other "languages" ​​also) have the property that their current-original value can be modified (on the fly) and reassigned to the same variable.
    The entry:
    Please Login or Register  to view this content.
    means that from the data table you take/get an element from the "i-th row" and the "first column", then from this element you take/get some value (here "day") and this new value you reassign to this data array element

    Quote Originally Posted by Asad Mir View Post
    ... changing number formatting to General and other things ...
    ... can you not do the same inside the loop? ...
    In this code, the actions in the loop refer only to the data table taken (to the RAM memory) from the sheet and not to the sheet itself.
    You could do formatting inside the loop, but it would take longer because of referencing individual cells of the sheet.
    A one-time reference to the entire range of cells is shorter and faster.


    Quote Originally Posted by Asad Mir View Post
    ... why are you adding 1 to last row? ...
    ...
    Please Login or Register  to view this content.
    ...
    Because of the table header in the worksheet.
    First you had to reduce "rws" by 1 because only 129 rows were loaded to the table (no header lines), but then, you had to restore the "rws" variable to its original value again so that the data was entered in the correct range of cells in the worksheet ("D2:D130").

  15. #15
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Really, I am short of words to appreciate how porucha vevrku explained...
    It felt like i am learning in a real face to face environment...
    Can't Thank you enough....

    Best Wishes!!!
    Last edited by Asad Mir; 06-14-2020 at 11:06 PM.

  16. #16
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    One Last Question...

    What's the purpose of below mentioned code and how does it work?

    Please Login or Register  to view this content.
    tbl = Empty

  17. #17
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,153

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Quote Originally Posted by Asad Mir View Post
    ... What's the purpose ...
    'Empty' indicates an uninitialized variable value for 'Variant' sub-type - it's like "clearing" a variable.
    Since the 'Variant' type is a rather strange variable type, I aim/endevour to "reset" this variable after each use.
    (if a given variable of the 'Variant' type passes through the code all the time in one sub-type, this is not needed)

    Ps:
    Quote Originally Posted by Asad Mir View Post
    ... how... explained...
    "Oki" ... Asad Mir ... : ) ... thank you for the nice words, but "this" font size is not needed, if you can still edit your post, change it to normal size ... : ) ...

    greetings

  18. #18
    Registered User
    Join Date
    07-13-2019
    Location
    Mansehra, Pakistan
    MS-Off Ver
    2010
    Posts
    46

    Re: Replacing whole date with only "Day" using for next loop or other methods.

    Sure, Thanks!

+ 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. Ideas and methods in improving "automatic calendar"???
    By Kamal1234 in forum Excel General
    Replies: 1
    Last Post: 08-24-2017, 11:08 AM
  2. Replies: 1
    Last Post: 01-22-2016, 09:21 AM
  3. Replacing "Yes" with End of Month Date
    By JMathur in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-25-2013, 12:03 AM
  4. Date format of just "st" "nd" "rd" and "th" with text included
    By notrandom in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-10-2013, 05:45 PM
  5. [SOLVED] Removing ".", "?" or "!" from last word of sentence and replacing it later in vba.
    By staggers47 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-16-2013, 07:20 AM
  6. Return "green", "yellow" or "red" from date/age and priority ranking
    By Cantaloop in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-04-2013, 12:12 AM
  7. [SOLVED] replacing sheet("XX").Select with for Loop Worksheet Name
    By Jerseynjphillypa in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2012, 10:01 AM

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