문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top