VBA code crush when it's saved xlam or xlsb

56 Views Asked by At

I'm a beginner in VBA and I have met an error. I have a xlsx file stored on PC and a file stored on SharePoint. I open the local file (every time has another name/path) and I need to bring from SharePoint some data in specific cells (different range each time) related to a text. So I have an userform with a list of countries, user must select the country - then the code put the country in cell brings from SharePoint the contact persons, in cell B5 of the local file, the name of contact person in cell B4 and the email address in cell B6.

Then it search text "Total to pay by:" on the column A (the text could be in different lines on each file) then puts the country in the 2 cell from the cell where the text is ant at 2 cells distance a formula with some figures from SharePoint file.

The bug is on this line Cell.Offset(0, 2).Activate

I try to run the code before saving as xlsb or xlam and it's work perfect, but if I save it as add in or personal (to be available in any workbook) the code doesn't run any more. I need your help to know how to fix it, I think something change when the code is saved in one of this two extension. It's there any other way that this code could be stored in order to be available on any excel file?

msg error:

Runtime error 1004.Activate method of range class failed

The code :

 Sub CommandButton1_Click()
    Dim my_text As String
    Dim myrange As Range
    Dim strExcelFilePathTwo As String
    
    my_text = "Total to pay by:"
    Set myrange = Worksheets("Feuil1").Range("A1:A100")
    
    Range("B5").Select
    Range("B5") = ComboBox1
    
    strExcelFilePathTwo = "an sharepoint page not supported by dialog box/PROCENTAJE%20TARI.xlsx"
    Set wkBk = Workbooks.Open(strExcelFilePathTwo, UpdateLinks:=False, ReadOnly:=False)
    
    ThisWorkbook.Activate
    
    Range("B4").Select
    If Range("B5").Value = "Allemagne" Then
    Range("B4") = "=IF('[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R4C5=0,"""",'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R4C5)"
    End If
    If Range("B5").Value = "Allemagne" Then
    Range("B6") = "=IF('[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R4C6=0,"""",'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R4C6)"
    End If
    
    If Range("B5").Value = "Argentina" Then
    Range("B4") = "=IF('[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R35C5=0,"""",'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R35C5)"
    End If
    If Range("B5").Value = "Argentina" Then
    Range("B6") = "=IF('[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R35C6=0,"""",'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R35C6)"
    End If
    
    If Range("B5").Value = "UK" Then
    Range("B6") = "=IF('[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R7C6=0,"""",'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R7C6)"
    End If
    
    For Each Cell In myrange
        If InStr(LCase(Cell.Value), LCase(my_text)) <> 0 Then
            Cell.Offset(0, 2).Value = ComboBox1.Value
            'Cell.Offset(0, 2).Activate
            
        End If
    Next Cell
    
    If ActiveCell.Value = "Allemagne" Then
       ActiveCell.Offset(, 2).Formula = "=(R[-1]C*1.03)*'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R4C14"
    
    End If
    
    If ActiveCell.Value = "Argentina" Then
       ActiveCell.Offset(, 2).Formula = "=R[-1]C*1.03"
    
    End If
    
    If ActiveCell.Value = "Autriche" Then
       ActiveCell.Offset(, 2).Formula = "=(R[-1]C*1.03)*'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R12C14"
    
    End If
    
    If ActiveCell.Value = "Belgique" Then
       ActiveCell.Offset(, 2).Formula = "=(R[-1]C*1.03)*'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R8C14"
    
    End If
    
    If ActiveCell.Value = "Brésil" Then
       ActiveCell.Offset(, 2).Formula = "=R[-1]C*1.03"
    
    End If
    
    If ActiveCell.Value = "Bulgarie" Then
       ActiveCell.Offset(, 2).Formula = "=R[-1]C*1.03"
    
    End If
    
    
    If ActiveCell.Value = "Suede" Then
       ActiveCell.Offset(, 2).Formula = "=(R[-1]C*1.03)*'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R14C14"
    
    End If
    
    If ActiveCell.Value = "Suisse" Then
       ActiveCell.Offset(, 2).Formula = "=(R[-1]C*1.03)*'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R13C14"
    
    End If
    
    If ActiveCell.Value = "Turquie" Then
       ActiveCell.Offset(, 2).Formula = "=R[-1]C*1.03"
    
    End If
    
    If ActiveCell.Value = "UK" Then
       ActiveCell.Offset(, 2).Formula = "=(R[-1]C*1.03)*'[PROCENTAJE TARI.xlsx]Pourcentage 2019'!R7C14"
    
    End If
    
    Application.Goto Reference:="R6C3"
        With Selection.Font
            .Name = "Arial"
            .Size = 8
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
    wkBk.Close
    End Sub
0

There are 0 best solutions below