+ Reply to Thread
Results 1 to 9 of 9

Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-name

Hybrid View

  1. #1
    Registered User
    Join Date
    08-13-2012
    Location
    NL
    MS-Off Ver
    Excel 2007
    Posts
    12

    Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-name

    Hello,

    I'm kind of new to VBA and I have a question that I can't seem to google my way out of... (Excel 2007)

    I have a kind of transporting material that I have to keep track of (exchanging them with either transporters or clients).
    For this, I weekly manually check our orderdata against the freight documents, which specify if the material is exchanged or if someone has a debt to us.
    At the moment I'm using a red font colour (-16776961) if material has not been exchanged and a green colour if it has.
    See attached document.

    Can someone (help me to) create a macro that:
    - finds each row with the red font colour (-16776961) in a workbook named "exchanged directly"
    - copies each red row to an existing other workbook named "administration", and places it on an existing sheet with sheetname = transporter-name (same name as the name in column G) in the first empty row (sheet has headers)
    - changes the font colour of the pasted row to the Automatic black colour
    - deletes the copied row from the "exchanged directly" workbook

    The greatest difficulty fo me is the second part of the above question. I'm hoping someone can help me out with this.
    Thanks!!
    Attached Files Attached Files
    Last edited by Marloes; 08-14-2012 at 04:37 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-

    Hello there,

    I believe the below code should work for you:
    *Please note: You will need to change the text I have made red in the code below to the filepath for the Administration workbook on your computer. If you are unsure of the filepath you can always open the workbook and in a blank cell type = CELL("filename") and that should give you the location.

    'declare variables
    Dim c As Range, LR As String, NLR As String
    Dim ExWB As Workbook, AdWB As Workbook, x As String
    Dim rng As Range
    
    Set ExWB = ThisWorkbook 'set exwb equal to the exchange directly workbook
        
        'open the Administration workbook,
        'change the below file path to your file path
        
        Application.Workbooks.Open Filename:="C:\Documents and Settings\rvasquez\My Documents\Example Spreadsheets\Test Folder2\Administration.xls"
             
            'set adWB equal to the Administration workbook
            Set AdWB = ActiveWorkbook
        
    With ExWB.Sheets(1) 'with the exchange workbook first worksheet in the workbook
        .Activate   'activate the workbook
        LR = .Range("A6555").End(xlUp).Row  'set the variable LR equal to teh last row that contains a value in column A
            For Each c In .Range("A1:A" & LR + 1).Cells 'loop through cells in column A from row 1 to the last row
                If c.Font.Color = RGB(255, 0, 0) Then   'if the font color in the current cell in the loop is red then
    
                    x = .Range("g" & c.Row).Value   'set x equal to value in the current cell in the loop's row in column G
                    .Range("A" & c.Row & ":J" & c.Row).Copy 'copy the cells from A to J of the current cell in the loop's row
                    
                    On Error Resume Next
                    
                    With AdWB.Sheets(x) 'with the worksheet in the Administrative workbook whose name is equal to x (defined above)
                        NLR = .Range("A6555").End(xlUp).Row + 1 'set NLR equal to the last row in column A the contains a value +1
                        .Range("A" & NLR).PasteSpecial xlPasteValues    'paste the copied values to the first empty cell
                        .Range("A" & NLR & ":J" & NLR).Font.Color = RGB(0, 0, 0)    'turn the font black
                    End With
                    
                    If rng Is Nothing Then  'if the variable rng is nothing then
                        Set rng = .Range(.Cells(c.Row, 1), .Cells(c.Row, 10))   'set rng equal to cells A through J of the current row
                    Else: Set rng = Union(rng, .Range(.Cells(c.Row, 1), .Cells(c.Row, 10))) 'if rng does exist then set the rng equal to the current rng and add the current cell in the loops row columns A through J
                    End If
                    
                End If
                    
                
            Next c  'move to next cell in the loop
    rng.Select  'select rng
    Selection.Delete shift:=xlUp    'delete the rng selected
    
    With AdWB
        .Close True 'close and save the Administrative workbook
    End With
    
    
    End With
    To use this code:
    1. Close the Administration workbook if it is open
    2. Open the Exchangeddirectly workbook
    3. Press Alt+F8
    4. Clear the macro name field
    5. In the macro name field type the text AddToAdmin
    6. Select the Create option
    7. In between the Sub AddToAdmin() and End Sub copy and paste the above code
    8. Anything that appears in green is a comment
    9. Don't forget to change the filepath to you Administration workbook's filepath
    10. Close out of Visual Basic
    11. Press Alt+F8
    12. Select the AddToAdmin macro
    13. Select the Run option

    Let me know if this works for you!

    Thanks!

    RVASQUEZ

  3. #3
    Registered User
    Join Date
    08-13-2012
    Location
    NL
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-

    Hi RVASQUEZ,

    Thank you so much for looking in to this!
    I ran the macro, but stumbled on the following error:

    Error 91: Object variable or With blockvariable not set, in line

    HTML Code: 
    Is it possible for you to alter it, so it will function as it's supposed to?
    Kind regards,
    Marloes

  4. #4
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-

    Hello there,

    Try placing

    .Activate
    above that line of code so that it looks like this

    .Activate
    rng.select

  5. #5
    Registered User
    Join Date
    08-13-2012
    Location
    NL
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-

    Hello Rvasquez,

    Sorry for the late reply. Unfortunately it still isn't working. I get the same error 91, on the same line as before.
    The Administration file is opened by the macro, but nothing is copied to it or deleted from the Exchanged-directly file.

    Here is the code at this time:
    Sub AddToAdmin()
    
    'declare variables
    Dim c As Range, LR As String, NLR As String
    Dim ExWB As Workbook, AdWB As Workbook, x As String
    Dim rng As Range
    
    Set ExWB = ThisWorkbook 'set exwb equal to the exchange directly workbook
        
        'open the Administration workbook,
        'change the below file path to your file path
        
        Application.Workbooks.Open Filename:="I:\Administration.xlsx"
             
            'set adWB equal to the Administration workbook
            Set AdWB = ActiveWorkbook
        
    With ExWB.Sheets(1) 'with the exchange workbook first worksheet in the workbook
        .Activate   'activate the workbook
        LR = .Range("A6555").End(xlUp).Row  'set the variable LR equal to the last row that contains a value in column A
            For Each c In .Range("A1:A" & LR + 1).Cells 'loop through cells in column A from row 1 to the last row
                If c.Font.Color = RGB(255, 0, 0) Then   'if the font color in the current cell in the loop is red then
    
                    x = .Range("g" & c.Row).Value   'set x equal to value in the current cell in the loop's row in column G
                    .Range("A" & c.Row & ":J" & c.Row).Copy 'copy the cells from A to J of the current cell in the loop's row
                    
                    On Error Resume Next
                    
                    With AdWB.Sheets(x) 'with the worksheet in the Administrative workbook whose name is equal to x (defined above)
                        NLR = .Range("A6555").End(xlUp).Row + 1 'set NLR equal to the last row in column A the contains a value +1
                        .Range("A" & NLR).PasteSpecial xlPasteValues    'paste the copied values to the first empty cell
                        .Range("A" & NLR & ":J" & NLR).Font.Color = RGB(0, 0, 0)    'turn the font black
                    End With
                    
                    If rng Is Nothing Then  'if the variable rng is nothing then
                        Set rng = .Range(.Cells(c.Row, 1), .Cells(c.Row, 10))   'set rng equal to cells A through J of the current row
                    Else: Set rng = Union(rng, .Range(.Cells(c.Row, 1), .Cells(c.Row, 10))) 'if rng does exist then set the rng equal to the current rng and add the current cell in the loops row columns A through J
                    End If
                    
                End If
                    
                
            Next c  'move to next cell in the loop
            
    .Activate
    rng.Select  'select rng <--- I get the error on this line
    Selection.Delete shift:=xlUp    'delete the rng selected
    
    With AdWB
        .Close True 'close and save the Administrative workbook
    End With
    
    
    End With
    End Sub
    Thanks so much for your effort!
    Marloes
    Last edited by Marloes; 08-20-2012 at 08:39 AM. Reason: changed path of file Administration to be anonymous

  6. #6
    Registered User
    Join Date
    08-13-2012
    Location
    NL
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-

    Hello,
    I've been looking at the macro for a while now .
    Could it be that variable c is not set?
    After running the macro, the only thing that happens is the Administration file is being opened.
    The active cells on both files (where the 'cursor' is) do not change.

    Thanks,
    Marloes

  7. #7
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-

    Hey there,

    No c is declared as a range and is set to the current cell in the loop. Try just for the moment deleting the following lines and let me know what happens.

    rng.Select  'select rng <--- I get the error on this line
    Selection.Delete shift:=xlUp    'delete the rng selected
    
    With AdWB
        .Close True 'close and save the Administrative workbook
    End With

  8. #8
    Registered User
    Join Date
    08-13-2012
    Location
    NL
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-

    Hello Rvasquez,

    I changed the macro according to your post. Nothing more happened than before.
    What did happen is that it ended up activating my Personal macro file (the macro was saved in that file, but i ran it from the Exchanged directly file).
    It got me thinking, and I saved the macro in the Exchanged directly file and ran it directly from there. It worked like a charm :-)
    Sorry for the trouble...!

    I have one more question: if the Administration file doesn't contain a sheet with the correct transporter name, the row gets deleted from the Exchanged directly file but it doesn't get copied to the Administration file. Would it be possible to skip the row in the Exchanged directly file if the transporter name (x) does not exist?

    I will go and try the macro on my original file as well.

    Thanks so much!

  9. #9
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Search for fontcolour, copy row to another existing workbook, to sheet with cellvalue-

    Hello there,

    Sorry about that I thought that was understood to place it in the exchangeddirectly worksheet by the line

    2.Open the Exchangeddirectly workbook
    Glad you could you get that sorted out!

    As for this:
    I have one more question: if the Administration file doesn't contain a sheet with the correct transporter name, the row gets deleted from the Exchanged directly file but it doesn't get copied to the Administration file. Would it be possible to skip the row in the Exchanged directly file if the transporter name (x) does not exist?
    Try the below code, replacing the existing code between the Sub AddToAdmin and End Sub with the below code. What this does is if the worksheet name is not found then it highlights the cell's row black.
    Let me know how it works!

    Thanks!


    'declare variables
    Dim c As Range, LR As String, NLR As String
    Dim ExWB As Workbook, AdWB As Workbook, x As String
    Dim rng As Range, y As Long
    
    Set ExWB = ThisWorkbook 'set exwb equal to the exchange directly workbook
        
        'open the Administration workbook,
        'change the below file path to your file path
        
        Application.Workbooks.Open Filename:="C:\Documents and Settings\rvasquez\My Documents\Example Spreadsheets\Test Folder2\Administration.xls"
            
            'set adWB equal to the Administration workbook
            Set AdWB = ActiveWorkbook
        
    With ExWB.Sheets(1) 'with the exchange workbook first worksheet in the workbook
        .Activate   'activate the workbook
        LR = .Range("A6555").End(xlUp).Row  'set the variable LR equal to teh last row that contains a value in column A
            For Each c In .Range("A1:A" & LR + 1).Cells 'loop through cells in column A from row 1 to the last row
                If c.Font.Color = RGB(255, 0, 0) Then   'if the font color in the current cell in the loop is red then
    
                    x = .Range("g" & c.Row).Value   'set x equal to value in the current cell in the loop's row in column G
                    .Range("A" & c.Row & ":J" & c.Row).Copy 'copy the cells from A to J of the current cell in the loop's row
                       
                    On Error Resume Next
                    With AdWB.Sheets(x) 'with the worksheet in the Administrative workbook whose name is equal to x (defined above)
                        
                        If Err.Number = 9 Then  'if the worksheet is not found then
                            c.EntireRow.Interior.Color = RGB(0, 0, 0)   'set the fill color of the row to black
                            y = 1   'set variable y = 1
                        Else
                            NLR = .Range("A6555").End(xlUp).Row + 1 'set NLR equal to the last row in column A the contains a value +1
                            .Range("A" & NLR).PasteSpecial xlPasteValues    'paste the copied values to the first empty cell
                            .Range("A" & NLR & ":J" & NLR).Font.Color = RGB(0, 0, 0)    'turn the font black
                            y = 0   'set variable y = 0
                        End If
                    End With
                    
                    If y = 0 Then   'if y = 0 then
                        If rng Is Nothing Then  'if the variable rng is nothing then
                            Set rng = .Range(.Cells(c.Row, 1), .Cells(c.Row, 10))   'set rng equal to cells A through J of the current row
                        Else: Set rng = Union(rng, .Range(.Cells(c.Row, 1), .Cells(c.Row, 10))) 'if rng does exist then set the rng equal to the current rng and add the current cell in the loops row columns A through J
                        End If
                    End If
                    
                    
                    
                End If
                    
                
            Next c  'move to next cell in the loop
    .Activate
    rng.Select  'select rng
    Selection.Delete shift:=xlUp    'delete the rng selected
    
    With AdWB
        .Close True 'close and save the Administrative workbook
    End With
    
    
    End With

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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