Pregunta

I'm trying to make a simple macro in VBA to change the name the formula of a same cell in all worksheets in a workbook. However, for some reason it doesnt work. Can someone explain to me what is wrong? Why does it work to change the formula to lookupvalue but not to formulavalue?

Sub RenameTest()

For Each aSheet In ActiveWorkbook.Worksheets
    aSheet.Activate
    If aSheet.Name <> "Test" Then
        Dim lookupvalue As String
        Dim formulavalue As String
        lookupvalue = Cells(2, 3).Value
        formulavalue = "=VLOOKUP(" & lookupvalue & "'Test'!A1:B122;2;FALSE)"
        Cells(2, 11).Formula = formulavalue
    End If


Next aSheet

End Sub

¿Fue útil?

Solución

When using .Formula property you should always use comma , as separator (even if your local settings required semicolon as separator). Change ; to , :

formulavalue = "=VLOOKUP(" & lookupvalue & ",'Test'!A1:B122,2,FALSE)"
Cells(2, 11).Formula = formulavalue

Alternatively, you could use .FormulaLocal (with your local separator, i.e. semicolon ;):

formulavalue = "=VLOOKUP(" & lookupvalue & ";'Test'!A1:B122;2;FALSE)"
Cells(2, 11).FormulaLocal = formulavalue

another issue - you've missed comma (semicolon) before 'Test'!A1:B122.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top