+ Reply to Thread
Results 1 to 6 of 6

VBA Code for looping through the worksheets and active next cell then run the same command

Hybrid View

  1. #1
    Registered User
    Join Date
    01-16-2022
    Location
    Singapore, Singapore
    MS-Off Ver
    2013
    Posts
    3

    Question VBA Code for looping through the worksheets and active next cell then run the same command

    Hi, I need help to give a tweak of a code that a friend give (thank you very much for it).

        'declare sheets
        Dim DivSheet As Worksheet, MasterSheet As Worksheet
        Set DivSheet = Sheets("Form Capex NPD") 'the sheet with the data
        Set MasterSheet = Sheets("Sheet4") 'where you want to put the copied data
        
        'declare ranges
        Dim originalDestinationCell As Range, nextDestCell As Range
        Set originalDestinationCell = MasterSheet.Range("C6") 'the first cell the data will be copied to
        Set nextDestCell = originalDestinationCell.Offset(-1, 0)
        Dim firstGreyCell As Range: Set firstGreyCell = DivSheet.Range("C6") 'the cell with the grey fill-color
        Dim rangeToSearchIn As Range: Set rangeToSearchIn = DivSheet.Range("C2:C2000") 'the range that the data is in
        'note: a larger range will take more time
        
        'move cell values
        For Each c In rangeToSearchIn
            If IsEmpty(c) = False Then 'only copy if the cell is not blank
                If c.Interior.Color = firstGreyCell.Interior.Color Then 'if the interior color of cell 'c' is the same as 'firstGreyCell' then
                    Set nextDestCell = MasterSheet.Cells(nextDestCell.Row + 1, originalDestinationCell.Column) 'move the next cell down one column and back to the original column
                    nextDestCell.Value = c.Value 'copy the value to the recap sheet
                    nextDestCell.Interior.Color = c.Interior.Color 'copy the cell color too
                Else
                    'if the interior color is not the same as 'c'
                    Set nextDestCell = nextDestCell.Offset(0, 1) 'move the nextDestCell to the right by 1
                    nextDestCell.Value = c.Value 'copy its value
                End If
            End If
        Next c
    End Sub
    What I need to make adjustments are:
    after the code executed, move the selected cell to the end of the filled cells and run the code again through the worksheets inside the workbook (because the code only runs on the specified sheet only and I need to make it run through the entire worksheets inside the workbook).

    Thank you!

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,275

    Re: VBA Code for looping through the worksheets and active next cell then run the same com

    Possibly...
    Sub Not_Tested()
        Dim ws As Worksheet, MasterSheet As Worksheet
        Dim originalDestinationCell As Range, nextDestCell As Range
        Dim firstGreyCell As Range, rangeToSearchIn As Range, c As Range
    
        Set MasterSheet = Sheets("Sheet4")            'where you want to put the copied data
        Set originalDestinationCell = MasterSheet.Range("C6") 'the first cell the data will be copied to
        Set nextDestCell = originalDestinationCell.Offset(-1, 0)
        
        For Each ws In ThisWorkbook.Worksheets
            If Not ws.Name = MasterSheet.Name Then
                Set firstGreyCell = ws.Range("C6")
                Set rangeToSearchIn = ws.Range("C2:C2000") 'the range that the data is in
                
                For Each c In rangeToSearchIn
                    If IsEmpty(c) = False Then        'only copy if the cell is not blank
                        If c.Interior.Color = firstGreyCell.Interior.Color Then
                            'if the interior color of cell 'c' is the same as 'firstGreyCell' then
                            Set nextDestCell = MasterSheet.Cells(nextDestCell.Row + 1, originalDestinationCell.Column)
                            'move the next cell down one column and back to the original column
                            nextDestCell.Value = c.Value 'copy the value to the recap sheet
                            nextDestCell.Interior.Color = c.Interior.Color 'copy the cell color too
                        Else
                            'if the interior color is not the same as 'c'
                            Set nextDestCell = nextDestCell.Offset(0, 1) 'move the nextDestCell to the right by 1
                            nextDestCell.Value = c.Value 'copy its value
                        End If
                    End If
                Next c
            End If
        Next ws
    End Sub

  3. #3
    Registered User
    Join Date
    01-16-2022
    Location
    Singapore, Singapore
    MS-Off Ver
    2013
    Posts
    3

    Re: VBA Code for looping through the worksheets and active next cell then run the same com

    thank you for the reply and it works like what I expected. But the problem within the code is that its also copying the header of the table. is there any fix for this? thanks so much

  4. #4
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,275

    Re: VBA Code for looping through the worksheets and active next cell then run the same com

    Hard to tell without seeing the workbook. Suggest you attach a sample workbook.

  5. #5
    Registered User
    Join Date
    01-16-2022
    Location
    Singapore, Singapore
    MS-Off Ver
    2013
    Posts
    3

    Re: VBA Code for looping through the worksheets and active next cell then run the same com

    hope the screenshot can explain the problem

    Attachment 764927

  6. #6
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,275

    Re: VBA Code for looping through the worksheets and active next cell then run the same com

    Your attachment isn't viewable.

+ 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] vba command to go to specific cell in the active cell row question
    By lilsnoop in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-12-2020, 09:45 PM
  2. Deleting a range using active cell and cells command
    By beasley101 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-13-2015, 08:17 AM
  3. Can't type in cell because of active X command button. Any ideas?
    By dbrizor in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-16-2015, 10:42 AM
  4. [SOLVED] Code is not looping through all worksheets
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-10-2014, 12:26 AM
  5. Set Active Cell on Command Button Click?
    By cadnauseum in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-07-2012, 04:00 PM
  6. using a the active cell in a range command....
    By dr chuck in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-07-2006, 10:10 AM
  7. [SOLVED] Code to hide worksheets based on Active Directory login?
    By Joe in forum Excel General
    Replies: 0
    Last Post: 02-21-2006, 12:20 AM

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