在VBA中更改VLOOKUP公式的工作表名称格式

时间:2016-05-22 13:32:21

标签: excel-vba vba excel

我想从每张纸上获取净关闭数量到一张新纸张,在此之前我一直使用01-12作为代表Jan-Dec的纸张名称。我想将工作表名称更改为Jan-Dec而不是01-12。如何更改格式?提前谢谢。

我的代码是:

Sub NCQ()
    Dim lastRow As Long
    Dim j As Byte

    j = 1

'// 3(space between columns) x 12(sheets) x 2(lookup columns in each sheet)
For i = 3 To (3 * 12) Step 2
    With Sheets("Net Close Qty")
        With .Cells(1, i).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row)
            .Formula = "=VLOOKUP($A1,'" & Format$(j, "00") & "'!$A$1:$T$155," & IIf(i Mod 6 = 3, 3, 3) & ",FALSE)"
            .Value = .Value
        End With
    End With
    j = j + 1
Next

'Rows(1).Insert shift:=xlShiftDown

End Sub

1 个答案:

答案 0 :(得分:2)

包括:

dts = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
ary = Split(dts, ",")

靠近顶部,然后使用:

ActiveCell.Formula = "=VLOOKUP($A1,'" & ary(j - 1) & "'!$A$1:$T$155," & IIf(i Mod 6 = 3, 3, 3) & ",FALSE)"

修改#1:

这尚未经过测试,但可以作为起点:

Sub NCQ()
    Dim lastRow As Long, dts As String
    Dim j As Long, i As Long

    dts = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
    ary = Split(dts, ",")
    j = 1
    For i = 3 To 36 Step 2
        With Sheets("Net Close Qty")
            With .Cells(1, i).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row)
                .Formula = "=VLOOKUP($A1,'" & ary(j - 1) & "'!$A$1:$T$155," & IIf(i Mod 6 = 3, 3, 3) & ",FALSE)"
                .Value = .Value
            End With
        End With
        j = j + 1
    Next
End Sub