+ Reply to Thread
Results 1 to 10 of 10

For each cell... look at a range of cells not individually

Hybrid View

  1. #1
    Registered User
    Join Date
    08-16-2016
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    88

    For each cell... look at a range of cells not individually

    Hi,

    The below code allows a user to double click a cell and the range below that cell called (varrange) will turn grey if there is a number in the cell, and the value of each cell is then copied to another table.

    What I'm having trouble with is that if ALL the cells in the range (varrange) are already coloured grey (14013909) then I'd like to make the cell colour none and also remove the contents of the other table. The problem is that it looks at one cell at a time, I'm not sure how to look at all the cells in (varrange) before moving onto the next part.


        If Intersect(Target, Range("D10:I10")) Is Nothing And Intersect(Target, Range("L10:L10")) Is Nothing And Intersect(Target, Range("R10:R10")) Is Nothing Then
        Cancel = True
        Else
        Set varrange = Target.Resize(POIDTBLlr1 - 9, 1)
            For Each cell In varrange
                If Not IsNumeric(cell) Then
                    Cancel = True
                'IF ALL CELLS IN varrange ARE GREY THEN
       	    'cell.Interior.Color = 14013909 Then
       	    'cell.Interior.Color = xlNone
       	    'cell.Offset(0, 30).ClearContents
    	     ELSE
                    cell.Interior.Color = 14013909
                    cell.Offset(0, 30) = cell.Value
                End If
            Next
        End If

    Any ideas or input would be great, thanks

  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
    44,902

    Re: For each cell... look at a range of cells not individually

    Gut reaction is that you'd need to loop through the range and count the grey cells and compare that to the varrange.Cells.Count. If the counts are equal, you can clear the colour and range, etc.
    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
    Registered User
    Join Date
    08-16-2016
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    88

    Re: For each cell... look at a range of cells not individually

    Aah, that makes sense! Any ideas on how that would actually be coded? I'm still very new to VBA! Thanks again

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: For each cell... look at a range of cells not individually

    Perhaps this
        Dim done As Boolean
        If Intersect(Target, Range("D10:I10")) Is Nothing And Intersect(Target, Range("L10:L10")) Is Nothing And Intersect(Target, Range("R10:R10")) Is Nothing Then
            Cancel = True
        Else
            Set varrange = Target.Resize(POIDTBLlr1 - 9, 1)
            If Not IsNull(varrange.Interior.ColorIndex) Then
                    'IF ALL CELLS IN varrange ARE GREY THEN
                If varrange.Interior.Color = 14013909 Then
                    varrange.Interior.ColorIndex = xlColorIndexNone
                    varrange.Offset(, 30).ClearContents
                    done = True
                End If
            End If
            If Not done Then
                For Each cell In varrange
                    If Not IsNumeric(cell) Then
                        Cancel = True
    
                 Else
                        cell.Interior.Color = 14013909
                        cell.Offset(0, 30) = cell.Value
                    End If
                Next
            End If
        End If
    Last edited by xlnitwit; 11-30-2017 at 08:53 AM. Reason: Remove unnecessary lines
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    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
    44,902

    Re: For each cell... look at a range of cells not individually

    Something like:

    Sub sSample()
    
    Dim varrange As Range, cell As Range, lCount As Long
    
    Set varrange = Range("A2:A20")
    
    For Each cell In varrange
        If cell.Interior.Color = 14013909 Then
            lCount = lCount + 1
        End If
    Next 'cell
    
    If lCount = 0 Then
        MsgBox "no grey cells"
    ElseIf lCount = varrange.Cells.Count Then
        MsgBox "all grey cells"
    Else
        MsgBox "some grey cells"
    End If
    
    End Sub

  6. #6
    Registered User
    Join Date
    08-16-2016
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    88

    Re: For each cell... look at a range of cells not individually

    Hi both,

    I couldn't get the first option to work I'm afraid. The 2nd option looks more promising, but one issue...

    The No GREY cells part works fine. However even when all the cells are grey, I still get the "some grey cells", rather than "all grey". To explain further, please see the below image:
    Capture.PNG

    1st column : No cells - WORKS
    2nd column : All cells are grey (but says only some grey cells) - Not working
    3rd column : Some grey cells - WORKS

    I'm not sure if the reason why the 2nd column is saying only "some cells" is because the number of cells with a value doesn't match the size of the range? I'd only want to include the cells with a value in the comparison... hopefully that makes sense?

    the "varrange.Cells.Count" part counts ALL cells in the range, whereas I think it needs to count only cells with a numeric value.

    Thanks again
    Last edited by terratushi; 11-30-2017 at 09:01 AM.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: For each cell... look at a range of cells not individually

    A workbook would make it much easier to provide a solution, as would a precise explanation of what is needed. The is the first time I can see that you have mentioned only populated cells being relevant- which is probably why what I suggested doesn't work for you.

  8. #8
    Registered User
    Join Date
    08-16-2016
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    88

    Re: For each cell... look at a range of cells not individually

    Hi guys,

    Sorry about that, I had been working on it for a while so when I explained my problem I didn't realise I had overlooked another part of the code which would have explained the numeric cells.

    I've managed to get it to work by using:

        If IsNumeric(cell) Then
            cellcount = cellcount + 1
        End If
    Thanks again for all your help and ideas

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: For each cell... look at a range of cells not individually

    I'm pleased you got it working!

  10. #10
    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
    44,902

    Re: For each cell... look at a range of cells not individually

    You're welcome.

    I was counting grey cells, not numeric cells.
    ... is that if ALL the cells in the range (varrange) are already coloured grey (14013909) ...

+ 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] Code for multiple cells, but have the cell values change individually
    By neonhorizon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2017, 03:55 PM
  2. Replies: 0
    Last Post: 11-18-2014, 07:44 PM
  3. Replies: 6
    Last Post: 09-21-2013, 07:02 PM
  4. Copy hyperlink cell to range of cell individually
    By excelhelp9 in forum Excel General
    Replies: 5
    Last Post: 12-04-2012, 08:10 PM
  5. Unable to paste multiple cells, individually
    By Fos605 in forum Excel General
    Replies: 1
    Last Post: 04-19-2010, 11:19 AM
  6. macro to sum each range individually within a single column
    By jferguson in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-16-2010, 12:56 PM
  7. Replies: 4
    Last Post: 01-03-2005, 03:06 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