+ Reply to Thread
Results 1 to 15 of 15

Need to convert one formula into code and add it to the existing code

  1. #1
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Need to convert one formula into code and add it to the existing code

    Hello code experts
    I need your help to add at the end of this code a formula which is to be converted into a code to get the final result. When I run the code, a new sheet is created with the Name of "GSTIN Verified.". I want to include in the code, to add this formula to get the combined values of J K L M and N in column P with the same code.
    =TRIM(SUBSTITUTE(J2&" "&K2&" "&L2&" "&M2&" "&N2,"0",""))
    If you convert the formula and get it in the second row below the heading, then resize the column P with column B and fill down.
    To test the code for the second time, delete the GSTIN Verified sheet and then run the code.
    Expected result sheet attached.
    Attached Files Attached Files
    Last edited by RAJESH SHAH; 10-03-2022 at 01:49 AM. Reason: #Solved by rollis13

  2. #2
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: Need to convert one formula into code and add it to the existing code

    This would be the example to fill your second cell of column P; now all you have to do is adapt/add it to your project:
    Please Login or Register  to view this content.
    Last edited by rollis13; 10-02-2022 at 06:02 PM.

  3. #3
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Re: Need to convert one formula into code and add it to the existing code

    rollis13. The code hasn't entered the formula in column P2. It is blank like before Even after entering the code at the end of the previous code.
    HTML Code: 
    I entered the above and got the formula in P2. Will try to resize and fill down. If I am not able to resize then I will let you know.
    Thanks rollis13.
    Last edited by RAJESH SHAH; 10-02-2022 at 05:28 PM.

  4. #4
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Re: Need to convert one formula into code and add it to the existing code

    Referring the old codes for resizing the rows, they are connected to 2 sheets and resizing the whole row. I am unable to edit toresize it and filldown one row only.
    Will try again tomorrow. Time to hit the sack.

  5. #5
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Re: Need to convert one formula into code and add it to the existing code

    The resize of column P worked. I was able to fill down the rows from P2 to the last row. Thanks for the formula converted into code rollis13.
    code to resize is
    HTML Code: 

  6. #6
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: Need to convert one formula into code and add it to the existing code

    Glad having been of some help. It would be nice to see your resize code (it's missing in post #3 and #5).

  7. #7
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Re: Need to convert one formula into code and add it to the existing code

    Option Explicit

    Sub ResizeRows()
    Dim x As Long, y As Long
    Sheets("GSTIN Verified").Activate
    y = Sheets("GSTIN Verified").Range("P2").CurrentRegion.Columns.Count
    x = Sheets("GSTIN Verified").Range("A2:A" & Sheets("GSTIN Verified").Range("A" & Rows.Count).End(xlUp).Row).Rows.Count
    Range("P2").Resize(x, y).FillDown

    End Sub

  8. #8
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Re: Need to convert one formula into code and add it to the existing code

    I added your code of converting the formula at the end of the sheet.
    WrapUp:
    Debug.Print "Time elapsed = " & (MicroTimer - StartTime) & " seconds." ' Display Elapsed Time into Immediate Window (CTRL+G)
    MsgBox "GSTIN verification Completed." ' Notify user that the script has finished
    'code by rollis13
    Sheets("GSTIN Verified").Select
    '=TRIM(SUBSTITUTE(J2&" "&K2&" "&L2&" "&M2&" "&N2,"0",""))
    Range("P2").Formula = "=TRIM(SUBSTITUTE(RC[-6]&"" ""&RC[-5]&"" ""&RC[-4]&"" ""&RC[-3]&"" ""&RC[-2],""0"",""""))"

    Call ResizeRows.ResizeRows


    End Sub

  9. #9
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: Need to convert one formula into code and add it to the existing code

    Thanks for the feedback .
    Tip: to obtain a R1C1 version of a formula (well that's how I do it ). Go to menu File / Options / Formulas / section Working with formulas / and check "R1C1 reference style", copy your formula and paste it into your macro, then reverse Options.
    Once pasted in your code all you have to do is add double-quotation around every double-quotes found in the formula. Example: " " becomes "" "", "0" -> ""0"", "" -> """".
    Last edited by rollis13; 10-03-2022 at 04:59 AM.

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,492

    Re: Need to convert one formula into code and add it to the existing code

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  11. #11
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Re: Need to convert one formula into code and add it to the existing code

    I did as you said to check the box RICI but the remaining part I didn't understand. I hope it doesn't effect the formula.

  12. #12
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Re: Need to convert one formula into code and add it to the existing code

    I thought it was the HTML button tags around selected text. I was also wondering why was it not displaying. So, I should add just # and paste the code like this is it ?
    #Range("P2").Formula = "=TRIM(SUBSTITUTE(RC[-6]&"" ""&RC[-5]&"" ""&RC[-4]&"" ""&RC[-3]&"" ""&RC[-2],""0"",""""))"

  13. #13
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: Need to convert one formula into code and add it to the existing code

    Post #11:
    Let's say that you have a working formula in your cell and you would like to used it in your macro. Change your Options, go to the cell, copy the formula and paste it in your macro then remember to reverse your option. In the formula in your macro, as said, add double-quotes where needed.

    Post #12:
    You need to click the # you see in the Icon Bar and in the message-box will appear the tags [CODE ][/CODE ]. The code of your macro goes in between the tags.
    Last edited by rollis13; 10-03-2022 at 01:10 PM.

  14. #14
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,171

    Re: Need to convert one formula into code and add it to the existing code

    Thanks rolllis13. That was a simple way of explaining.
    I understood and got the reply for post #12.
    Post #11 , I will have to check and practice manually later. Right Now I have a lot of pending queries to solve.
    "Code to create a sheet and get selected data from the source sheet" is one of them. Can you help me with this ? I am not getting any replies here nor in the other forum where I posted.

  15. #15
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: Need to convert one formula into code and add it to the existing code

    [...] I am not getting any replies here nor in the other forum where I posted.
    That's probably because people don't like cross-posting, or, maybe you didn't explain well your request.
    Last edited by rollis13; 10-03-2022 at 04:30 PM.

+ 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] assistance to modify vba code existing code sheets name to sheet tab names in filtering
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2018, 03:58 AM
  2. [help with code] need to update existing code to copy and keep rows on input page
    By scott micklo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2016, 01:36 PM
  3. [SOLVED] VBA code in existing code to open when workbook encrypt with password.
    By deo_y in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-26-2014, 07:08 AM
  4. [SOLVED] VBA code to save current worksheet as temporary PDF file and then add to my existing code
    By brianfromla in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-20-2014, 08:35 AM
  5. Replies: 2
    Last Post: 12-17-2013, 11:51 AM
  6. Replies: 2
    Last Post: 03-17-2011, 08:55 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