Metodo find attivazione foglio



  • Metodo find + attivazione foglio
    di Nicola (utente non iscritto) data: 19/08/2014 11:15:18

    Ciao a tutti ....

    Ho bisogno cortesemente di un aiuto da parte vostra....

    Sto cercando di utilizzare il metodo find per ricercare dei codici uguali e inserire i loro valori in un altra cartella....
    All'apertura del file da cui devo leggere i dati non riesco ad attivare l'unico foglio presente .....
    Di conseguenaza "Find non trova nulla"...

    Vi prego di darmi una mano...

    Vi allego i 2 file che utilizzo ...magari vi potrà essere di aiuto

    Grazieee millle

     
    Private Sub cmdaggiornalistino_Click()
    
    Dim ws1 As Workbook
    Dim Sh1 As Worksheet
    Dim ws2 As Workbook
    Dim Sh2 As Worksheet
    
    Dim Area As Range, RR As Object
    Dim Uriga1 As Long, Uriga2 As Long, R As Long, X As Long
    Dim ID As String, Percorso As String, nomeFile As String
    
    Set ws1 = Workbooks("1292 - SP SANLURI(Prova).xlsm")
    Set Sh1 = ws1.Worksheets("Proposta")
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    On Error Resume Next
    
    Uriga2 = Sh1.Range("A" & Rows.Count).End(xlUp).Row
    
    Percorso = "C:Users
    icola.spanuDesktoplist + giac 1292 - SP SANLURI_Pivot 1.xls"
    
    Workbooks.Open (Percorso)
    
    Set ws2 = Workbooks("list + giac 1292 - SP SANLURI_Pivot 1.xls")
    Set Sh2 = ws2.Worksheets("Corrispettivi 1")
    
    For Each Sh2 In ActiveWorkbook.Worksheets
    
    Sh2.Activate
    
    Uriga1 = Sh2.Range("A" & Rows.Count).End(xlUp).Row
    Set Area = Sh2.Range("A4:A" & Uriga1)
    
    For X = 4 To Uriga2
    
    ID = Cells(X, 1).Value
    
    Set RR = Area.Find(ID, LookIn:=xlValues, Lookat:=xlWhole)
    
    If R <> ID Then
    
    R = 0
    
    R = RR.Row
    
    Sh1.Cells(X, 5) = Sh2.Cells(R, 3)
    Sh1.Cells(X, 6) = Sh2.Cells(R, 4)
    
    End If
    
    Next X
    
    Next Sh2
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.DisplayAlerts = False
    ActiveWorkbook.Close SaveChanges:=True
    Application.DisplayAlerts = True
    
    MsgBox "Aggiornamento eseguito con successo"
    
    Set Sh1 = Nothing
    Set ws1 = Nothing
    Set Sh2 = Nothing
    Set ws2 = Nothing
    Set Area = Nothing
    
    End Sub



  • di Grograman data: 19/08/2014 12:10:30

    Come fai a capire dove sbagli lasciando "On error resume next"?

    Ho dato una sistemata al codice usando variabili con nomi più adatti (ws1 per un workbook!!).

    Non capisco se i fogli li devi cilcare tutti o devi agire solo sul foglio "Corrispettivi 1", nel caso specifico il codice sotto funziona peril singolo foglio.
     
    Option Explicit
    
    Private Sub cmdaggiornalistino_Click()
    
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    
    Dim Area As Range, RR As Range
    Dim Uriga1 As Long, Uriga2 As Long, R As Long, X As Long
    Dim ID As String, Percorso As String, nomeFile As String
    
    Set wb1 = ThisWorkbook
    Set ws1 = wb1.Worksheets("Proposta")
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    'On Error Resume Next
    
    Uriga2 = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row
    'Percorso = "C:Provelist + giac 1292 - SP SANLURI_Pivot 1.xls"
    Percorso = "C:Users
    icola.spanuDesktoplist + giac 1292 - SP SANLURI_Pivot 1.xls"
    
    Set wb2 = Application.Workbooks.Open(Percorso)
    Set ws2 = wb2.Worksheets("Corrispettivi 1")
    
    Uriga1 = ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row
    Set Area = ws2.Range("A4:A" & Uriga1)
    
    For X = 4 To Uriga2
    ID = ws1.Cells(X, 1).Value
      Set RR = Area.Find(ID, LookIn:=xlValues, Lookat:=xlWhole)
      If Not RR Is Nothing Then
        ws1.Cells(X, 5) = ws2.Cells(RR.Row, 3)
        ws1.Cells(X, 6) = ws2.Cells(RR.Row, 4)
        Set RR = Nothing
      End If
    Next X
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    'wb1.Close SaveChanges:=True
    Application.DisplayAlerts = True
    
    MsgBox "Aggiornamento eseguito con successo"
    
    Set ws1 = Nothing
    Set wb1 = Nothing
    Set ws2 = Nothing
    Set wb2 = Nothing
    Set Area = Nothing
    End Sub


  • Metodo find + attivazione foglio
    di Nicola (utente non iscritto) data: 19/08/2014 12:19:32

    Ciao Grograman...grazie per il tuo aiuto

    ho provato il codice è va alla grande ....

    Puoi spiegarmi gentilmente dove sbagliavo....te ne sarei grato per capirlo meglio...

    La mia intenzione era lanciare la macro dal foglio proposta e prendere i dati dal foglio corrispettivi.......

    perdonami ...ma non sono esperto ....però sto studiando tanto ...





  • di lepat (utente non iscritto) data: 19/08/2014 12:24:01

    che senso ha scrivere
    Set Sh2 = ws2.Worksheets("Corrispettivi 1")
    e subito dopo
    For Each Sh2 In ActiveWorkbook.Worksheets
    ?

    mi sembra che ci sia un solo foglio, inoltre non troverai mai gli errori con On Error Resume Next

     
    Option Explicit
    
    Private Sub cmdaggiornalistino_Click()
    
    Dim ws1 As Workbook
    Dim Sh1 As Worksheet
    Dim ws2 As Workbook
    Dim Sh2 As Worksheet
    
    Dim Area As Range, RR As Object
    Dim Uriga1 As Long, Uriga2 As Long, R As Long, X As Long
    Dim ID As String, Percorso As String, nomeFile As String
    
    Set ws1 = Workbooks("1292 - SP SANLURI(Prova).xlsm")
    Set Sh1 = ws1.Worksheets("Proposta")
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    On Error Resume Next
    
    Uriga2 = Sh1.Range("A" & Rows.Count).End(xlUp).Row
    
    Percorso = "C:UsersandreDesktoplist + giac 1292 - SP SANLURI_Pivot 1.xls"
    
    Workbooks.Open (Percorso)
    
    Set ws2 = Workbooks("list + giac 1292 - SP SANLURI_Pivot 1.xls")
    Set Sh2 = ws2.Worksheets("Corrispettivi 1")
    Sh2.Activate
    Uriga1 = Range("A" & Rows.Count).End(xlUp).Row
    Set Area = Range("A4:A" & Uriga1)
    For X = 4 To Uriga2
    ID = Cells(X, 1).Value
    Set RR = Area.Find(ID, LookIn:=xlValues, Lookat:=xlWhole)
    If R <> ID Then
    R = 0
    R = RR.Row
    Sh1.Cells(X, 5) = Sh2.Cells(R, 3)
    Sh1.Cells(X, 6) = Sh2.Cells(R, 4)
    End If
    Next X
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.DisplayAlerts = False
    ActiveWorkbook.Close SaveChanges:=True
    Application.DisplayAlerts = True
    
    MsgBox "Aggiornamento eseguito con successo"
    
    Set Sh1 = Nothing
    Set ws1 = Nothing
    Set Sh2 = Nothing
    Set ws2 = Nothing
    Set Area = Nothing
    
    End Sub
    
    
    
    
    Private Sub cmdaggiornapromo_Click()
    
    End Sub
    
    Private Sub cmdCancella_Click()
    Dim Uriga As Long
    
    Dim Ws As Worksheet: Set Ws = Sheets("Proposta")
    
    Uriga = Ws.Range("A" & Rows.Count).End(xlUp).Row
    
    
        Range("A4:J" & Uriga).Select
    
    Selection.ClearContents
    
    End Sub
    
    Private Sub cmdProposta_Click()
    Dim FinalRow As Long
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Range("J4:J" & FinalRow).FormulaR1C1 = "=(RC[-4]-RC[-5]-RC[-3])*-1"
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    
    End Sub
    
    



  • di Grograman data: 19/08/2014 12:29:12

    Direi che l'errore fondamentale era qui:

    Uriga1 = Sh2.Range("A" & Rows.Count).End(xlUp).Row

    Quel "Rows.count" senza paternità assume valore 1048576 e quindi tentando di attirbuire un range su un foglio con 65536 righe andava in errore.

    Pova nella ifnestra immediata a scrivere, appena prima della riga di codice suddetta:
    ?sh2.Rows.Count e poi ?Rows.Count per capire meglio la differenza.


  • Metodo find + attivazione foglio
    di Nicola (utente non iscritto) data: 19/08/2014 12:35:37

    Grazie mille Grograman...sei stato chiarissimo e velocissimo....

    complimenti





  • Metodo find + attivazione foglio
    di Nicola (utente non iscritto) data: 19/08/2014 12:37:38

    Grazie lepat per i tuoi consigli...