+ Reply to Thread
Results 1 to 32 of 32

Copy value from one cell to another if defined conditions are satisfied

  1. #1
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Smile Copy value from one cell to another if defined conditions are satisfied

    Hi,

    Please help me to create a excel vba code:

    i am using example for explanation purpose:
    1. Consider an Excel workbook with multiple sheets. In the "VBASheet" sheet, we have data like this:
    VBASheet:
    | A | B | C | D | E | F | G |
    |-------|-------|-------------|--------|-------|-------|-------|
    | 1 | KW 15 | A1 SB-27 | A1 | | | |
    | 2 | KW15 | A3 SB-38 | A3 SB | | | |
    | 3 | KW15 | A3 SE-38 | A3 Lim | | | |
    | | | | | | | |

    In "VBASheet" sheet, go through Column D. If a sheet name is found in Column D, we'll retrieve values from Columns B and C in that row. (if "A1" Sheet is found in workbook then retrieve values from Column B and C in that row, here value in Cell B="KW 15" and value in Cell C="A1 SB-27")

    Before running VBA Code:

    Sheet "A1":
    | A | B | C | D | E | F I
    |-------|-------|-------|-------|-------|
    | Data1 | A1 SB-27 | KW15| KW16 | Jan | KW17 |
    | Data4 | Value2 | Sheet1| Data5 | Dat6 | 11 I
    | Data7 | Value3 | Sheet2| Data8 | Dat9 | 12 I
    | Data1 | Value1 | Sheet3| Data2 | Dat3 | 13 I
    | Data4 | Value2 | Sheet4| Da5 | Daa6 | 14 I
    | Data7 | Value3 | Sheet1| Data8 | Dat9 | 15 I


    Next, we'll search for the value in Cell C in the corresponding sheet (so we look for "A1 SB-27" in Sheet "A1"). If "A1 SB-27" is found in Cell B2 in Sheet "A1" then in the same row look for "KW15" which was cell value in Column B in "VBASheet" Sheet. When both condition is fulfilled then offset by (1,0) -> to give cell D2 in this example and check if this cell includes "KW" as value in it. Here when we offset by (1,0) to "KW15" in "A1" Sheet than cell value is "KW16", which includes "KW" in it. then offset by (1,0) to "KW16" (CEll D2) and copy value of this cell D2 ("Sheet1") in cell which is located (3,0) to it. So copy "Sheet1" and replace "Sheet4" by "Sheet1", continue this loop by moving (1,0) to cell D2 now which is Cell E2 ("Jan") and check if cell E2 includes "KW" if yes then below above copy operation and if not then again move (1,0) to cell E2...

    After running VBA Code:

    Sheet "A1":
    | A | B | C | D | E | F I
    |-------|-------|-------|-------|-------|
    | Data1 | A1 SB-27 | KW15 | KW16 I Jan | KW17 |
    | Data4 | Value2 | Sheet1 | Data5 | Dat6 | 11 I
    | Data7 | Value3 | Sheet2 | Data8 | Dat9 | 12 I
    | Data1 | Value1 | Sheet3 | Data2 | Dat3 | 13 I
    | Data4 | Value2 | Sheet4 | Data5 | Daa6 | 11 I
    | Data7 | Value3 | Sheet1 | Data8 | Dat9 | 15 I


    In my "VBASheet" Sheet, there are more than 50 sheets listed in column D as i have this 50 worksheet also in my workbook and this similar logic must work for all the sheets

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Mappe1.xlsx

    I have attached here sample file to get good idea and in Shhet "A1" and Sheet "A2", I have tried to show before and after situation after using the VBA Code.
    In my original solution, I want to copy value if "KW" is mentioned in cell and i do not wish to create duplicate new table as shown in this file after execution of vba code.
    Thank you once again!

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    This macro assumes that your VBASheet sheet has headers in row 1 and your data starts in row 2.
    Please Login or Register  to view this content.
    Last edited by Mumps1; 03-25-2024 at 02:46 PM.

  5. #5
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Hi Mumps1, I have adjusted the code due to runtime error. This code is working to look "KW" in cell adjacent to cell in which column value B is found and then after it stops and don't move forward to check in other cells:
    Emaple: I have sheet "A1" mentioned in column D in "VBASheet". We look for Sheet "A1" in "VBASheet" and look for corresponding value in column B("KW15") and C("A1 SB-27")
    Actual scenario on running this code:
    A1 SB-27 KW15 KW16 KW17 Jan KW18 A1 SB-27 KW15 KW16 KW17 Jan KW18
    10 20 30 40 50 10 20 30 40 50
    -- -- -- -- -- ----> -- -- -- -- --
    -- -- -- -- -- -- -- -- -- --
    -- -- -- -- -- -- -- -- -- --

    Desired scenario:

    A1 SB-27 KW15 KW16 KW17 Jan KW18 A1 SB-27 KW15 KW16 KW17 Jan KW18
    10 20 30 40 50 10 20 30 40 50
    -- -- -- -- -- ----> -- -- -- -- --
    -- -- -- -- -- -- -- -- -- --
    -- -- -- -- -- -- 20 30 -- 50

    Actual vs desired solution.png20


    Here is the new code:

    Please Login or Register  to view this content.
    Run time error on using code mentioned by you:
    Run-time error 91.png
    Last edited by AliGW; 03-26-2024 at 03:52 AM. Reason: Code tags added - please review the forum guidelines.

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    Is it working properly for you?

  7. #7
    Banned User!
    Join Date
    03-11-2024
    Location
    usa
    MS-Off Ver
    CURRENT
    Posts
    23

    Re: Copy value from one cell to another if defined conditions are satisfied

    To complete the task, you will need to write a VBA macro. I am attaching the code below
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Thank you for the code but its not working at all. Would you please help to create a code which gives desired outcome.
    Thank you in advance!

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,850

    Re: Copy value from one cell to another if defined conditions are satisfied

    You will need to say in what way it isn't working.

    Provide a sample workbook containing the source data, your code and a manually mocked-up demonstration of the results you require.
    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.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  10. #10
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    I have already attached sample file with desired result after VBA MAcro. Would you please let me know if it not visible anymore?

    Thank you!
    Last edited by AliGW; 03-27-2024 at 03:54 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

  11. #11
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Quote Originally Posted by Mumps1 View Post
    Is it working properly for you?
    Unfortunately its not working!

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    I tested the code using the file you posted and it worked as you requested. Are you using the macro on a different file? If so, attach a copy of that file.

  13. #13
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Here is the file, I am working on this file:
    Attached Files Attached Files
    Last edited by AliGW; 03-27-2024 at 03:47 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    You have not posted the link to your file.

  15. #15
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    You are right, I posted above. Here my excel language is in german.
    Last edited by AliGW; 03-27-2024 at 03:47 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

  16. #16
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    Is there any reason why the data in the Q3 Indien sheet begins in column B instead of column A like all the other sheets? Also, there are some cells highlighted in blue. Is that of any importance?

  17. #17
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Blue color does not hold any importance here. Q3 Indien Sheet has Indien written in ColumnA just to differntiate it from other sheet, if there's problem than its text can be deleted so that format stays the same
    Last edited by AliGW; 03-27-2024 at 03:47 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

  18. #18
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    Delete column A in the Q3 Indien sheet and try this macro:
    Please Login or Register  to view this content.
    Last edited by Mumps1; 03-26-2024 at 11:28 AM.

  19. #19
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Thank you for the support
    I deleted Column ! in Q3 Indien Sheet and tried this macro, but there is run time error 13:Attachment 864212
    Would you please here?
    Last edited by AliGW; 03-27-2024 at 03:46 AM.

  20. #20
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    Your link doesn't work. See the yellow banner at the top of this page for instructions to attach a file.

  21. #21
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    here is attachment: Attachment 864256
    This code is highlighted on debugging: If Evaluate("isref('" & sh.Value & "'!A1)") Then
    Last edited by AliGW; 03-27-2024 at 03:44 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

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

    Re: Copy value from one cell to another if defined conditions are satisfied

    The attachment is invalid. Please read the yellow banner at the top of the page and follow its instructions to attach your workbook.

  23. #23
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Good Morning, I can open and see above attachment and I was also mentioned which code is highlighted on debugging. I am bit puzzled here, what is the problem?
    Last edited by AliGW; 03-27-2024 at 03:44 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

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

    Re: Copy value from one cell to another if defined conditions are satisfied

    The problem is that you are not attaching it in the correct way. That attachment is NOT visible to all. Please do as I have asked.

    From the yellow banner:

    ... scroll down to GO ADVANCED, click, and then scroll down to MANAGE ATTACHMENTS and click again. Now follow the instructions at the top of that screen.
    Last edited by AliGW; 03-27-2024 at 03:46 AM.

  25. #25
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,850

    Re: Copy value from one cell to another if defined conditions are satisfied

    Further:

    Post #13 has been attached in the correct way.

    Posts #19 and #21 have NOT been attached in the correct way.

    Follow the instructions in my previous post.

  26. #26
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    I have again attached the error run time message. I hope its visible now...
    Thank you!
    Attached Images Attached Images

  27. #27
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,850

    Re: Copy value from one cell to another if defined conditions are satisfied

    You were asked to attach the LATEST copy of your WORKBOOK (not a screenshot). Please help us to help you. Workbooks are particularly important when members are working with a different locale (language settings).

  28. #28
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Okay got it, I am attaching here with the same code which was proposed by @Mumps1

    Unfortunately due to file size constraint, i have to delete some sheets which are not required before uploading!
    Attached Files Attached Files
    Last edited by vk1995; 03-27-2024 at 04:35 AM.

  29. #29
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,850

    Re: Copy value from one cell to another if defined conditions are satisfied

    That's fine - thank you.

  30. #30
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    Try:
    Please Login or Register  to view this content.

  31. #31
    Registered User
    Join Date
    03-22-2024
    Location
    Germany
    MS-Off Ver
    365
    Posts
    15

    Re: Copy value from one cell to another if defined conditions are satisfied

    Quote Originally Posted by Mumps1 View Post
    Try:
    Please Login or Register  to view this content.
    Thank you @Mumps1 for the continuous support. Finally it worked!

    But when I tried it today, it encountered same run time error. I feel that macro is not reliable tool to perform the defined operation everytime or what should I take care in specific to run this code successfully?
    Let me if there is something to keep in mind before running this code.

    Thank you once again!

  32. #32
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Copy value from one cell to another if defined conditions are satisfied

    Are you using the macro in the same file as you posted? Which line of code is highlighted when you click "Debug" and what is the exact error message?

+ 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] copy full row to another sheet if conditions are satisfied
    By kv.singh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-12-2021, 08:52 AM
  2. Extract Values If Conditions are Satisfied
    By bjnockle in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-13-2021, 10:05 AM
  3. [SOLVED] Return an answer if 4 conditions are satisfied
    By allen003 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-03-2020, 11:14 AM
  4. [SOLVED] compute an average if certain conditions are satisfied
    By anita2017 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-07-2017, 10:29 AM
  5. Replies: 5
    Last Post: 08-18-2016, 09:13 AM
  6. Linked Lookup IF 2 conditions are satisfied
    By reghu in forum Excel General
    Replies: 0
    Last Post: 09-01-2010, 07:13 AM
  7. Replies: 10
    Last Post: 08-28-2008, 06:45 PM

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