+ Reply to Thread
Results 1 to 13 of 13

VBA: Chose placeholder based on conditions whilst copying from one to another worksheet

  1. #1
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Question VBA: Chose placeholder based on conditions whilst copying from one to another worksheet

    Hi all,

    I'm using VBA to copy/paste content from several worksheets to an overview worksheet based on the value selected (dropdown) in cell C3. The script searches for the value in C3 in the remaining worksheet (tab) names and then copies the content (C3:C) of any found worksheet to cell C14:C on the overview sheet. Working code (placed on overview worksheet) below.

    Please Login or Register  to view this content.
    So far so good Now, I have the issue that the content I'm copying over contains a placeholder ({1night}), which I would like to replace with another placeholder (one out of the 9 below) depending on the value I specify in cell E5 on the overview worksheet.

    {0nights}
    {1night}
    {2nights}
    {3nights}
    {4nights}
    {5nights}
    {6nights}
    {7nights}
    {8+nights}

    Logic:
    - If E5 = 0, replace {1night} with {0nights}
    - If E5 = 1, replace {1night} with {1night}
    - If E5 = 2, replace {1night} with {2nights}
    - If E5 = 3, replace {1night} with {3nights}
    - If E5 = 4, replace {1night} with {4nights}
    - If E5 = 5, replace {1night} with {5nights}
    - If E5 = 6, replace {1night} with {6nights}
    - If E5 = 7, replace {1night} with {7nights}
    - If E5 >= 8, replace {1night} with {8+nights}

    This logic should follow after the 'paste' to the overview sheet (so part of the script above).

    Is that feasible? Quite new to VBA so any help would be appreciated (cyber beer included)

    Many thanks!

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

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Where on the overview sheet would that placeholder be? What would the placeholder look like? Would it look like "{1night}" including the brackets?
    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
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Hi Kingo

    May I ask, are you making an application for a BB or hotel? Maybe I can help. I am not a great VBA coder but I have made applications for the hotels.

  4. #4
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213
    Quote Originally Posted by Mumps1 View Post
    Where on the overview sheet would that placeholder be? What would the placeholder look like? Would it look like "{1night}" including the brackets?
    Hi Mumps! Yes, that's correct!! The placeholder can appear anywhere in the cell (text) - sometimes even multiple times. Would this be an issue? Thanks so much for your help!! 😊

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

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    By "anywhere in the cell (text)" do you mean that the placeholder is part of a string in the cell, for example "The stay is for {1night} in June and {1night} in July." ? Also does it appear only in one cell on the sheet? If so which cell or can it appear in many cells in the sheet? If it appears in many cells, is it always in one particular row or column or can it appear anywhere on the sheet?

  6. #6
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Morning Mumps

    Oh yes, sorry should've mentioned that! Yes, the string is part of a text (e.g. "The stay is for {1night} in June and {1night} in July.") and it appears in all cells the upper script pastes to (basically each cell contains a translation of the text, but using the same strings), think that is C14 to C105 (so cells in c column). Hope this helps?

    Many thanks!
    Last edited by kingofcamden; 12-18-2018 at 07:19 AM.

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

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Try:
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Hi Mumps!

    Oh wow! Awesome - that works!! Thanks so much.

    Two remaining questions (hope that's ok!):

    1) Is there any way to change it to include the condition "If E5 >= 8, replace {1night} with {8+nights}"? At the moment, if I type in for example 10, it would replace {1night} with {10night}. However, the available strings are 1-8 only, so anything that is more than 8 should use {8night}.
    2) Is there a way to re-run this bit of the script, whenever the value in E5 is changed (e.g. from 2 to 3)?

    Please Login or Register  to view this content.
    Asking because I just noticed that once the copy is pasted to the overview page and the {1night} strings are replaced, the replaced string is then not updating when I change the value again in E5 (which makes sense as the script is controlled via C3). So if that replacement bit could also run retrospectively (after the paste) whenever the value in E5 is changed, that'd be perfect!

    Thanks so much for the help! Appreciate it

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

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Give this a try:
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Hi Mumps,

    Thanks so much for this! Tried it, but unfortunately no luck. It doesn't seem to do anything anymore :-(
    I've uploaded an example (using the code you had previously provided, which still works). Hope it's easier to work with that?

    Many thanks for your help!
    Attached Files Attached Files

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

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Try this version. Keep in mind that the file you attached in Post #10 does not contain any occurrence of "{1night}" so there is nothing to replace.
    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    Awesome!!! That did the trick! Yaaaay!

    Thanks so much for your help. May my day! I owe you a cyber beer *cheers*.

    Have a great Christmas!

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

    Re: VBA: Chose placeholder based on conditions whilst copying from one to another workshee

    My pleasure and the same to you.

+ 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. Replies: 1
    Last Post: 08-12-2014, 01:37 AM
  2. [SOLVED] Conditional formatting when copying a worksheet; order of conditions changes
    By jazzmatazz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-30-2014, 06:02 AM
  3. Need help with copying files based on conditions
    By cmill02s in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-09-2013, 02:27 PM
  4. Formula required to calculate prices whilst considering other conditions
    By NIVLAG in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-24-2013, 10:45 AM
  5. Copying Data from One Worksheet to Another One Based on Conditions VBA
    By esanchez in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2011, 04:36 PM
  6. Replies: 1
    Last Post: 01-14-2010, 03:31 AM
  7. Copying cells whilst retaining the formula
    By Ahoneysett in forum Excel General
    Replies: 1
    Last Post: 11-05-2009, 06:36 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