ERRORE Method ‘Select’ of object range failed (1004)

Anonimo
2021-03-26T16:45:44+00:00

Buongiorno,

ho un problema con un excel VBA di questo tipo: in un workbook con alcuni sheets, il primo sheet contiene dei CommandButton che eseguono del codice VBA (allego il codice sotto con indicazione dell'istruzione che va in errore).

Il problema che mi si presenta è che, lanciando il codice VBA dal "bottone" ricevo l'errore Method ‘Select’ of object range failed (1004) mentre se lo eseguo dalla finestra VB la prima volta funziona ma se lo rilancio ottengo lo stesso errore.

Utilizzo Excel 2016 su MacBook Pro con mac OS BigSur 11.2.3

Grazie

Public Sub CreazioneClientiGlobal()

    Dim WB As Workbook

    Dim srcSH As Worksheet, destSH As Worksheet

    Dim rCell As Range, xRg As Range   

    Dim iRow As Long, LRow As Long, LCol As Long, cliRow As Long, ncliRow As Long, Ind As Long

    Dim strAnno As String, ColumnLetter As String, CodCli As String, GlCodCli As String, CodiceCliente As     

             String, xFirstAddress As String

    Const sWorkbook As String = "Clusters.xlsm"

    Const sFoglio_Anagrafica As String = "Anagrafica"

    Const sFoglio_Global As String = "Global"

    Const sFoglio_Clienti As String = "Clienti"

    Set WB = Workbooks(sWorkbook)

    With WB

        Set srcSH = .Sheets(sFoglio_Anagrafica)

        Set destSH = .Sheets(sFoglio_Global)

    End With

Application.ScreenUpdating = False

    With destSH

Intersect(.Columns("A:AC"), .UsedRange).Offset(1).ClearContents

    End With

    LRow = LastRow(srcSH, srcSH.Columns("A:F"))

    destSH.Range("F2:F" & LRow).NumberFormat = "General"

    For Each rCell In srcSH.Range("A2:A" & LRow).Cells

        If rCell.Value <> "" Then

           iRow = rCell.Row

destSH.Range("A" & iRow & ":C" & iRow).Value = srcSH.Range("A" & iRow & ":C" & iRow).Value

destSH.Range("D" & iRow & ":E" & iRow).Value = srcSH.Range("E" & iRow & ":F" & iRow).Value

           destSH.Range("I" & iRow).Value = "Anagrafica"

           strAnno = ""

           strAnno = Mid(srcSH.Range("F" & iRow), 7, 2)

           If strAnno <> "" Then

              If strAnno > 19 Then

strAnno = "19" & strAnno

destSH.Range("F" & iRow).Value = strAnno

              Else

strAnno = "20" & strAnno

                 destSH.Range("F" & iRow).Value = strAnno

End If

           End If

destSH.Range("G" & iRow & ":H" & iRow).Value = srcSH.Range("J" & iRow & ":K" & iRow).Value

        End If

    Next

    With WB

        Set srcSH = .Sheets(sFoglio_Clienti)

        Set destSH = .Sheets(sFoglio_Global)

    End With

    LRow = LastRow(srcSH)

    LCol = LastColumn(destSH)

    cliRow = 0

    GlCodCli = ""

    For Each rCell In srcSH.Range("A2:A" & LRow).Cells

           If rCell.Value <> "" Then

iRow = rCell.Row

CodCli = srcSH.Range("A" & iRow).Value

              If cliRow = 0 Or GlCodCli <> CodCli Then

cliRow = RigaCodiceCliente(destSH, CodCli)

If cliRow <> 0 Then

GlCodCli = CodCli

                    ColumnLetter = Split(Cells(1, LCol).Address, "$")(1)

ncliRow = cliRow + 1

destSH.Range("A" & ncliRow & ":" & ColumnLetter & ncliRow).Select

– **ERROR “**Method ‘Select’ of object range failed (1004) -

Selection.Insert Shift:=xlDown

destSH.Range("A" & ncliRow & ":H" & ncliRow).Value = destSH.Range("A" & cliRow & ":H" &   

cliRow).Value

destSH.Range("I" & ncliRow).Value = "Fondi"

 destSH.Range("J" & ncliRow & ":N" & ncliRow).Value = srcSH.Range("F" & iRow & ":J" &

iRow).Value

ncliRow = ncliRow + 1

End If

Else

destSH.Range("A" & ncliRow & ":" & ColumnLetter & ncliRow).Select

Selection.Insert Shift:=xlDown

destSH.Range("A" & ncliRow & ":H" & ncliRow).Value = destSH.Range("A" & cliRow & ":H" &

cliRow).Value

destSH.Range("I" & ncliRow).Value = "Fondi"

destSH.Range("J" & ncliRow & ":N" & ncliRow).Value = srcSH.Range("F" & iRow & ":J" &

iRow).Value

ncliRow = ncliRow + 1

End If

           End If

    Next

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

2 risposte

Ordina per: Più utili
  1. Anonimo
    2021-03-27T09:44:52+00:00

    Buongiorno Norman,

    ho risolto grazie mille.

    Buona giornata

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2021-03-26T17:08:30+00:00

    ho un problema con un excel VBA di questo tipo: in un workbook con alcuni sheets, il primo sheet contiene dei CommandButton che eseguono del codice VBA (allego il codice sotto con indicazione dell'istruzione che va in errore).

    Il problema che mi si presenta è che, lanciando il codice VBA dal "bottone" ricevo l'errore Method ‘Select’ of object range failed (1004) mentre se lo eseguo dalla finestra VB la prima volta funziona ma se lo rilancio ottengo lo stesso errore.

    Utilizzo Excel 2016 su MacBook Pro con mac OS BigSur 11.2.3

    Public Sub CreazioneClientiGlobal()

     

        Dim WB As Workbook

        Dim srcSH As Worksheet, destSH As Worksheet

        Dim rCell As Range, xRg As Range   

        Dim iRow As Long, LRow As Long, LCol As Long, cliRow As Long, ncliRow As Long, Ind As Long

        Dim strAnno As String, ColumnLetter As String, CodCli As String, GlCodCli As String, CodiceCliente As     

                                  String, xFirstAddress As String

     

        Const sWorkbook As String = "Clusters.xlsm"

        Const sFoglio_Anagrafica As String = "Anagrafica"

        Const sFoglio_Global As String = "Global"

        Const sFoglio_Clienti As String = "Clienti"

       

        Set WB = Workbooks(sWorkbook)

       

        With WB

            Set srcSH = .Sheets(sFoglio_Anagrafica)

            Set destSH = .Sheets(sFoglio_Global)

        End With

     

        Application.ScreenUpdating = False

        With destSH

            Intersect(.Columns("A:AC"), .UsedRange).Offset(1).ClearContents

        End With

        LRow = LastRow(srcSH, srcSH.Columns("A:F"))

        destSH.Range("F2:F" & LRow).NumberFormat = "General"

        For Each rCell In srcSH.Range("A2:A" & LRow).Cells

            If rCell.Value <> "" Then

               iRow = rCell.Row

               destSH.Range("A" & iRow & ":C" & iRow).Value = srcSH.Range("A" & iRow & ":C" & iRow).Value

               destSH.Range("D" & iRow & ":E" & iRow).Value = srcSH.Range("E" & iRow & ":F" & iRow).Value

               destSH.Range("I" & iRow).Value = "Anagrafica"

               strAnno = ""

               strAnno = Mid(srcSH.Range("F" & iRow), 7, 2)

               If strAnno <> "" Then

                  If strAnno > 19 Then

                     strAnno = "19" & strAnno

                     destSH.Range("F" & iRow).Value = strAnno

                  Else

                     strAnno = "20" & strAnno

                     destSH.Range("F" & iRow).Value = strAnno

                  End If

               End If

               destSH.Range("G" & iRow & ":H" & iRow).Value = srcSH.Range("J" & iRow & ":K" & iRow).Value

            End If

     

        Next

     

        With WB

            Set srcSH = .Sheets(sFoglio_Clienti)

            Set destSH = .Sheets(sFoglio_Global)

        End With

        LRow = LastRow(srcSH)

        LCol = LastColumn(destSH)

     

        cliRow = 0

        GlCodCli = ""

       

        For Each rCell In srcSH.Range("A2:A" & LRow).Cells

               If rCell.Value <> "" Then

                 

                  iRow = rCell.Row

                  CodCli = srcSH.Range("A" & iRow).Value

                  If cliRow = 0 Or GlCodCli <> CodCli Then

                     cliRow = RigaCodiceCliente(destSH, CodCli)

                     If cliRow <> 0 Then

                        GlCodCli = CodCli

                        ColumnLetter = Split(Cells(1, LCol).Address, "$")(1)

                        ncliRow = cliRow + 1

                        destSH.Range("A" & ncliRow & ":" & ColumnLetter & ncliRow).Select

        

                         – ERROR “Method ‘Select’ of object range failed (1004) -

     

                        Selection.Insert Shift:=xlDown

                        destSH.Range("A" & ncliRow & ":H" & ncliRow).Value = destSH.Range("A" & cliRow & ":H" &   

                        cliRow).Value

                        destSH.Range("I" & ncliRow).Value = "Fondi"

                        destSH.Range("J" & ncliRow & ":N" & ncliRow).Value = srcSH.Range("F" & iRow & ":J" &

                        iRow).Value

                        ncliRow = ncliRow + 1

                     End If

                  Else

                        destSH.Range("A" & ncliRow & ":" & ColumnLetter & ncliRow).Select

                        Selection.Insert Shift:=xlDown

                        destSH.Range("A" & ncliRow & ":H" & ncliRow).Value = destSH.Range("A" & cliRow & ":H" &

                        cliRow).Value

                        destSH.Range("I" & ncliRow).Value = "Fondi"

                        destSH.Range("J" & ncliRow & ":N" & ncliRow).Value = srcSH.Range("F" & iRow & ":J" &

                        iRow).Value

                        ncliRow = ncliRow + 1

                  End If

               End If

        Next

    Ciao DelMau,

    Ti chiederei gentilmente di caricare il file problematico, dopo averlo depurato dei dati sensibili, su un servizio di condivisione di file, ad esempio Microsoft OneDrive o DropBox, e postare un link al file in una risposta qui.

    Per caricare il file su Microsoft OneDrive, vedi:

       Condividere file e cartelle di OneDrive

    Per caricare il file su DropBox, vedi:

    Come faccio a condividere file e cartelle in Dropbox?

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento