简体   繁体   中英

Look for a specific cell from a workbook to another workbook

I need you all to help me with this problem.

The context:

I'm trying (struggling) to create a counter of every proposition the enterprise make to know every date of signature of each proposition. So I create a Worksheet that will serve as a base for everyone, every time they create one they just need to type a number to identify the proposition. I've already created a macro to write the number and the amount of a new proposition in the last free row of the counter Worksheet.

Note: The 2 Worksheet are in different Workbooks.

Now the difficulty is that on the counter sheet there are multiple signatures. Signature A, B, C, D and they never happened on the same day.

Note: I will create a different macro for each signature based on the eventual answer we will find. ( A macro for A, a macro for B, etc…)

Exemple:

ChronoView

Charles creates a Proposition, number: 101 and only do the signature A.

John creates a Proposition, number: 102 and only do the signature B.

I need to be sure that it will enter the date of signature A in the row of Proposition 101 and also enter the date of signature B in the row of Proposition 102

My actual position:

I've made 2 Workbooks:

—Contract (This workbook contains a sheet named “Proposition”)

—Chrono (This workbook contains a sheet named “Counter”)

PropositionView

So if I want my date of signature A for the Proposition 101 in the right row, I need to find the text/value of G6 that is in “Proposition”, and look for it in “Counter” A column. If it functions correctly, it must find the A9 cell then write the formula: TODAY() in H9

What I did (don't laugh, I started macro 3 weeks ago haha):

Sub DateSignatureA()
Dim Chrono As Workbook
Dim Contract As Workbook
Dim refCell As Range
Dim refRow As Range


Set Contract = ActiveWorkbook
Set refCell = Range("G6")
Workbooks.Open Filename:="C:\Users\a.leboedec\Documents\S-T_Experimental\Chrono_Experimental"
Set refRow = Range("A:A").Find(G6, [A8], xlValues, , , xlNext)
    refRow(0,7).Interior.Color = RGB(200, 200, 500)
    refRow(0,7).Formula = "TODAY()"


End Sub

It tells me now “Run-time Error 438”

Note: * * Text * * is where there is the error

Thanks to the one that read it all, and I wait for you all answers.

When you do .Find , put the larger search area on the outside like SearchArea.Find( "SearchForThisValue", ... )

When working with multiple workbooks, you should write the whole object path when using Range objects. like

Application.Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1")
'Or
Dim WB As Workbook
Set WB = Application.Workbooks("Book1.xlsx")
Dim WS As Worksheet
Set WS = WB.Worksheets(1)
Dim Target As Range
Set Target = WS.Range("A1")

Doing this is clearer to you and whoever else needs to read the code. This also avoids grabbing values from the wrong sheet or book.

Here is your code, rewritten with the full object paths and the fixed Find statement.

Sub DateSignatureA()
    Dim Chrono As Workbook
    Dim ChronoWS As Worksheet
    Dim Contract As Workbook
    Dim ContractWS As Worksheet
    Dim refCell As Range
    Dim refRow As Range
    
    
    Set Contract = ActiveWorkbook
    Set ContractWS = Contract.ActiveSheet
    Set refCell = ContractWS.Range("G6")
    
    Set Chrono = Workbooks.Open(Filename:="C:\Users\a.leboedec\Documents\S-T_Experimental\Chrono_Experimental")
    Set ChronoWS = Chrono.ActiveSheet
    Set refRow = ChronoWS.Range("A:A").Find( _
                                        refCell.Value, _
                                        After:=ChronoWS.Range("A8"), _
                                        SearchDirection:=xlNext _
                                        )
    
    If Not refRow Is Nothing Then
        refRow.Cells(1, 7).Interior.Color = RGB(200, 200, 500)
        refRow.Cells(1, 7).Value = Date
    End If

End Sub

Also, when working with Find , there is always the case that nothing is found. It is good to test for that case to avoid errors. Not refRow Is Nothing tests to make sure that something was returned from Find before trying to access its properties and methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM