문제

I am trying to set the formula of a cell to an if formula that uses data from an already existing sheet within the workbook. However whenever trying to reference the sheet i get a file dialog box that opens looking for the path for the sheet. Below you will find my code which loops through all existing sheets in the workbook and compares it to a sheet name when it finds a match it stores the sheet code name for the current and previous weeks. Once it has these I have an if statement that tries to access these sheets and compare values. It is at this point that I get the file dialog box. Any thoughts or suggestions would be greatly appreciated. Thank you. Sorry for formatting issues I will correct as soon as possible.

sheetName = CStr(Year(Now)) & " eDR FW" & CStr(Format(Date, "ww")) & "_2"

lastWeekName = CStr(Year(Now)) & " eDR FW" & CStr(Format(Date, "ww") - 1) & "_2"

For Each ws In ActiveWorkbook.Worksheets

    If ws.Name = sheetName Then

        sheetName = ws.CodeName

    ElseIf ws.Name = lastWeekName Then

        lastWeekName = ws.CodeName

    End If

Next

rowRange = colLetter & "2:" & colLetter & CStr(sheetRowCount)

lineofBalance = "=IFERROR(IF(INDEX('" & lastWeekName & "'!$A$2:$W$10000,MATCH(A2,'" & sheetName & "'!$A$2:$A$10000,0),6)=F2,F2,CONCATENATE(""Updated: ""&F2)),CONCATENATE(""New: ""&F2))"
도움이 되었습니까?

해결책

All you need to do to make this work is change both ws.Codename to ws.Name.

The .Codename property only contains the first name of the sheet when it was originally created (which is most likely something like "Sheet32") and not the current name of the worksheet. It CAN be changed but most likely will remain the same unless you REALLY intend to change it. The formula with the erroneous worksheet name then opens the dialog box to try to find a reference for that particular worksheet.

Excel's formulas are designed to work with the current worksheet name and not it's codename. The only useful way I can see of using the .Codename, is if you wanna make sure to always use a particular worksheet (within VBA) even if the name of the worksheet was changed at some point (by yourself or a user).

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