简体   繁体   中英

Worksheet loop - How to retrieve name of the worksheet and put it into formula

For each column in the worksheet, I was trying to grab information from another worksheet based on the header of the column (where the first row in the column would have the same name as the worksheet).

Here is my code:

Dim i As Integer
For i = 1 To 60
    wsmain.Cells(2, 11 + i).FormulaArray = "=IF(priceFlag, '[SP60-Sub-Index.xlsm]wsName'!F2, '[SP60-Sub-Index.xlsm]wsName'!E2)"

    strName = wsmain.Cells(2, 11 + i).Value
    With wsmain.Cells(2, 11 + i)
        .Replace "wsName", strName, xlPart
    End With
Next i

I have defined strName as a string, but it does not work.

Any ideas?

This looks in row 2 for the sheet name and puts the formula in row 3.

There is no need for the replace, just concatenate in place.

FormulaArray will make the formula an array formula you want just Formula.

The code you were using is to get around FormulaArrays limit of 255 characters. It is not needed for simple formulas.

Dim i As Long
For i = 1 To 60
    strName = wsmain.Cells(2, 11 + i).Value
    wsmain.Cells(3, 11 + i).Formula = "=IF(priceFlag, '[SP60-Sub-Index.xlsm]" & strName & "'!F2, '[SP60-Sub-Index.xlsm]" & strName & "'!E2)"    
Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM