Closed Thread
Results 1 to 8 of 8

Apply VBA code to multiple cells

Hybrid View

  1. #1
    Registered User
    Join Date
    11-23-2013
    Location
    Oregon
    MS-Off Ver
    Excel Starter 2010
    Posts
    15

    Apply VBA code to multiple cells

    Hi there,

    I'm trying to make a drop-down menu within which multiple answers can be selected. I found some code online that I copy-pasted into my file and it works great, except that it only applies to one cell. I would like to have this multiple-selection enabled dropdown menu for a whole column of cells on my sheet.

    Here is the code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim oldVal As String
        Dim newVal As String
        
        If Target.Address(0, 0) <> "I4" Then Exit Sub
    
        On Error GoTo ReEnable
        Application.EnableEvents = False
        newVal = Target.Value
        Application.Undo
        oldVal = Target.Value
        Target.Value = newVal
    
        If oldVal <> "" And newVal <> "" Then
            Target.Value = oldVal & ", " & newVal
        End If
    ReEnable:
        Application.EnableEvents = True
        
    End Sub
    ...where I4 is just one of the cells in which I would want this kind of drop-down menu to appear. I have never used VBA before, so the answer may be obvious to others, but my question is: how can I apply this code to cells other than I4?

    Thanks!
    Last edited by alansidman; 10-06-2014 at 02:49 PM. Reason: code tags added

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,454

    Re: Apply VBA code to multiple cells

    If Intersect(Target, Columns("I:I"))  Is Nothing Then Exit Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Apply VBA code to multiple cells

    Hi,

    Without going into the rest of the code, if you want to use the event procedure for the whole column "I", then try something like this (marked in red):

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim oldVal As String
        Dim newVal As String
    
        If Intersect(Columns("I"), Target) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
    
        On Error GoTo ReEnable
        Application.EnableEvents = False
        newVal = Target.Value
        Application.Undo
        oldVal = Target.Value
        Target.Value = newVal
    
        If oldVal <> "" And newVal <> "" Then
            Target.Value = oldVal & ", " & newVal
        End If
    ReEnable:
        Application.EnableEvents = True
    
    End Sub

  4. #4
    Registered User
    Join Date
    11-23-2013
    Location
    Oregon
    MS-Off Ver
    Excel Starter 2010
    Posts
    15

    Re: Apply VBA code to multiple cells

    Wow that works great! Thanks!!

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Apply VBA code to multiple cells

    @rosenina18

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,454

    Re: Apply VBA code to multiple cells

    You're welcome.




    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  7. #7
    Registered User
    Join Date
    11-14-2024
    Location
    karachi, pakistan
    MS-Off Ver
    2016
    Posts
    3

    Re: Apply VBA code to multiple cells

    I want to apply the below VB code on the whole column "I " kindly guided please


    Private Sub Worksheet_Change(ByVal Target As Range)
    
    ' Initializing variables
    Dim oldVal As String
    Dim newVal As String
    
    ' Checking for changes in the drop down lists
    
    Application.EnableEvents = True
    
    If Target.Address = "$I$4" Or Target.Address = "$G$3" Then
        
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            Exit Sub
    ' Adding the multiple selected items to the list
        Else
            If Target.Value = "" Then
                Exit Sub
    
            Else
                Application.EnableEvents = False
                newVal = Target.Value
                Application.Undo
                oldVal = Target.Value
                If oldVal = "" Then
                    Target.Value = newVal
                Else
                    If InStr(oldVal, newVal) = 0 Then
                        Target.Value = oldVal & ", " & newVal '& vbNewLine &
                    Else
                        Target.Value = oldVal
                    End If
                End If
            End If
        End If
    End If
    
    Application.EnableEvents = True
    
    End Sub
    Last edited by AliGW; 11-14-2024 at 08:07 AM. Reason: Code tags added - please review the forum guidelines.

  8. #8
    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. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,989

    Re: Apply VBA code to multiple cells

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar or even the same as this thread, we have a rule that you open your own thread on the issue and do not piggy back another member's thread.

    Please see Forum Rule #1 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    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.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Apply VBA code to multiple sheets
    By PaulusKabouter in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-26-2013, 11:37 AM
  2. Apply VBA code to multiple cells
    By jond291 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-31-2013, 07:33 AM
  3. [SOLVED] Can i modify this code to apply to multiple checkboxes and sheets?
    By stolen_83 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2013, 02:44 PM
  4. [SOLVED] Match code to apply multiple cells in 1 Sub
    By colvinb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-20-2012, 06:38 AM
  5. Need to apply VBA code to multiple Worksheets
    By parteegolfer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2006, 04:43 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