+ Reply to Thread
Results 1 to 13 of 13

write a macro to fill text only if a certain value exits in another cell

  1. #1
    Forum Contributor
    Join Date
    08-15-2015
    Location
    India
    MS-Off Ver
    Office 2016
    Posts
    227

    write a macro to fill text only if a certain value exits in another cell

    Hi,

    i am able to put in a specific text in a cell using a macro, but now i want to validate that macro such that it can enter data only if a particular data exists in another cell.

    explanation.

    i have the following code written

    [Code/]
    Sub inputaquablue()
    Dim cell As Range
    For Each cell In Selection
    If cell.Column = 2 And cell.Row >= 14 And cell.Row <= 78 Then
    cell.Value = "Aqua Blue"
    End If
    Next cell

    End Sub
    [Code]

    By using the above i am able to enter "Aqua Blue" in column B, rows 14 to 78, however now i want to allow entry of "Aqua Blue" only if the corresponding cell has the Text or Value "Fish" in it.

    Going by the above example i want to enter "Aqua Blue" in cells B20:B25, but it should input "Aqua Blue" only if cells A20:A25 has "Fish" written in them.

    If however if "fish is written in only cell A21 then "Aqua Blue" should come in only cell B21.

    Regards
    Last edited by Nirmall; 08-31-2015 at 09:28 AM.

  2. #2
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: write a macro to fill text only if a certain value exits in another cell

    Hello Nirmall
    Try something like:

    Please Login or Register  to view this content.
    This is not case sensitive to the word 'Fish', it will accept all cases.

    Hope this helps.
    DBY

  3. #3
    Forum Contributor
    Join Date
    08-15-2015
    Location
    India
    MS-Off Ver
    Office 2016
    Posts
    227

    Re: write a macro to fill text only if a certain value exits in another cell

    It says wrong number of arguments or invalid property assignment

    Is it because my sheet is named as Data???

    If that is the reason then what changes do i make in my code??

  4. #4
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: write a macro to fill text only if a certain value exits in another cell

    Hello there!
    What line is highlighted by the error ?
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  5. #5
    Forum Contributor
    Join Date
    08-15-2015
    Location
    India
    MS-Off Ver
    Office 2016
    Posts
    227

    Re: write a macro to fill text only if a certain value exits in another cell

    For Each c In Data.Range("A20:A25")

    I had changed the name from Sheet 1 to Data (Data is the name of my sheet)

  6. #6
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: write a macro to fill text only if a certain value exits in another cell

    Try this way then:
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: write a macro to fill text only if a certain value exits in another cell

    Maybe:

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    08-15-2015
    Location
    India
    MS-Off Ver
    Office 2016
    Posts
    227

    Re: write a macro to fill text only if a certain value exits in another cell

    Hi all, thanks for your replies, but am still not able to get the desired result, am putting my file as an attachment.

    Please let me know where the problem is??

    Regards

    Nirmall
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: write a macro to fill text only if a certain value exits in another cell

    Hello
    The following works for me on your sample file:

    Please Login or Register  to view this content.
    DBY

  10. #10
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: write a macro to fill text only if a certain value exits in another cell

    My suggestion works on your sample.

    Please Login or Register  to view this content.
    Add Option Compare Text above the Sub - Make a Selection.

  11. #11
    Forum Contributor
    Join Date
    08-15-2015
    Location
    India
    MS-Off Ver
    Office 2016
    Posts
    227

    Re: write a macro to fill text only if a certain value exits in another cell

    Hi All thanks Post 9 worked well.

    Thanks DBY,John Davis & bulina2k

  12. #12
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: write a macro to fill text only if a certain value exits in another cell

    Glad we could help. Thanks for the Rep mark.

    DBY

  13. #13
    Forum Contributor
    Join Date
    08-15-2015
    Location
    India
    MS-Off Ver
    Office 2016
    Posts
    227

    Re: write a macro to fill text only if a certain value exits in another cell

    Hi,sorry all,DBY if you can please help again,

    the code which you provided in this excel puts Aqua blue, whenever i run the macro and when there is Fish in the corresponding cells in column A.

    But there are times where i would want to assign a different color to "Fish". So instead of "Aqua Blue i would want to select White for a few cases.

    Kindly give a solution where the macro is run for cells which are selected by me.
    Meaning there might be Cells A20:A30 which might be having "Fish", but i would want to select "Aqua Blue" for cells B20:B25, and "White" for Cells B26:B30.
    Regards
    Attached Files Attached Files

+ 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] Macro to check if sheet name exits
    By robrobet in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2015, 01:00 PM
  2. Write a macro to import data from web to excel (need fill in column and redirect)
    By tatfish87 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-24-2014, 05:21 AM
  3. A macro to auto fill in text depending upon cell content
    By johngefc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-21-2014, 01:03 PM
  4. [SOLVED] UserForm in Excel- need to write code to auto fill a text field
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-09-2013, 10:58 AM
  5. [SOLVED] Macro fill cell cells with text from another column
    By forfiett in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-16-2012, 08:29 AM
  6. Write macro to fill body of the text automatically
    By Kotler in forum Word Programming / VBA / Macros
    Replies: 3
    Last Post: 04-10-2012, 12:17 AM
  7. Reprotect after macro exits.
    By D1TrueGod in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-20-2008, 12:34 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