Excel VBA范围变量和自动填充

时间:2017-09-12 00:19:08

标签: excel-vba variables range autofill vba

我在表格的末尾添加了列。首先是标题,然后是下面一列中的公式。然后我想自动填充整个列表的公式。我相信变量声明就是问题所在。我一直在底部的自动填充部分收到错误。

Dim LastCol As Long, lastrow As Long
Dim OOSCol As Long, LCell As Long
Dim LRange As Range
Dim ws As Worksheet
Set ws = Application.ActiveWorkbook.ActiveSheet    

With ws
    .Name = "Summary"
    LastCol = .Cells(51, .Columns.Count).End(xlToLeft).Column
    .Cells(51, LastCol + 1).Value = "OOS NO SOO"
    .Cells(52, LastCol + 1).FormulaR1C1 = "=IF((RC[-21]=4)*AND(RC[-20]=0)*AND(RC[-19]=0)*AND(RC[-3]=""Roster"")*AND(RC[-4]=""Listed""),1,"""")"
    .Cells(51, LastCol + 2).Value = "OOS SOO"
    .Cells(52, LastCol + 2).FormulaR1C1 = "=IF((RC[-22]=4)*AND(RC[-21]>0)*AND(RC[-20]>0)*AND(RC[-4]=""Roster"")*AND(RC[-5]=""Listed""),1,"""")"
    .Cells(51, LastCol + 3).Value = "SOH NO SOO"
    .Cells(52, LastCol + 3).FormulaR1C1 = "=IF((RC[-23]=4)*AND(RC[-22]>0)*AND(RC[-21]=0)*AND(RC[-5]=""Roster"")*AND(RC[-6]=""Listed""),1,"""")"
    .Cells(51, LastCol + 4).Value = "Negative Stock"
    .Cells(52, LastCol + 4).FormulaR1C1 = "=IF((RC[-24]=4)*AND(RC[-23]<0)*AND(RC[-6]=""Roster"")*AND(RC[-7]=""Listed""),1,"""")"
    .Cells(51, LastCol + 5).Value = "Overstock"
    .Cells(52, LastCol + 5).FormulaR1C1 = "=IF((RC[-25]=4)*AND(RC[-21]>14)*AND(RC[-7]=""Roster"")*AND(RC[-8]=""Listed""),1,"""")"
    .Cells(51, LastCol + 6).Value = "Dormant Stock"
    .Cells(52, LastCol + 6).FormulaR1C1 = "=IF((RC[-26]=4)*AND(RC[-25]>0)*AND(RC[-24]>10)*AND(RC[-8]=""Roster"")*AND(RC[-9]=""Listed""),1,"""")"
    .Cells(51, LastCol + 7).Value = "Outdated Stock Counts"
    .Cells(52, LastCol + 7).FormulaR1C1 = "=IF((RC[-27]=4)*AND(RC[-26]<>0)*AND(RC[-17]>30)*AND(RC[-9]=""Roster"")*AND(RC[-10]=""Listed""),1,"""")"
    .Cells(51, LastCol + 8).Value = "Total Issues"
    .Cells(52, LastCol + 8).FormulaR1C1 = "=SUM(RC[-7]:RC[-1])*AND(RC[-10]=""Roster"")*AND(RC[-11]=""Listed"")"
    .Range("A51", Cells(51, Columns.Count).End(xlToRight)).SpecialCells(xlCellTypeConstants).Interior.Color = RGB(0, 32, 96)
    .Range("A51", Cells(51, Columns.Count).End(xlToRight)).SpecialCells(xlCellTypeConstants).Font.Color = vbWhite

End With

With ws
    LastCol = .Cells(51, .Columns.Count).End(xlToLeft).Column
    OOSCol = .Cells(52, LastCol - 8)
    lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
    LCell = .Cells(lastrow, LastCol)
End With

LRange = Range(OOSCol, LCell).Select
Selection.AutoFill Destination:=Range(OOSCol, LCell)

1 个答案:

答案 0 :(得分:0)

我设法使用以下代码在其他网站的帮助下修复它。查看最后一行代码。这才有效。

Dim LastCol As Long, lastrow As Long
Dim ws As Worksheet
Set ws = Application.ActiveWorkbook.ActiveSheet

With ws
    .Name = "Summary"
    LastCol = .Cells(51, .Columns.Count).End(xlToLeft).Column
    .Cells(51, LastCol + 1).Value = "OOS NO SOO"
    .Cells(52, LastCol + 1).FormulaR1C1 = "=IF((RC[-21]=4)*AND(RC[-20]=0)*AND(RC[-19]=0)*AND(RC[-3]=""Roster"")*AND(RC[-4]=""Listed""),1,"""")"
    .Cells(51, LastCol + 2).Value = "OOS SOO"
    .Cells(52, LastCol + 2).FormulaR1C1 = "=IF((RC[-22]=4)*AND(RC[-21]>0)*AND(RC[-20]>0)*AND(RC[-4]=""Roster"")*AND(RC[-5]=""Listed""),1,"""")"
    .Cells(51, LastCol + 3).Value = "SOH NO SOO"
    .Cells(52, LastCol + 3).FormulaR1C1 = "=IF((RC[-23]=4)*AND(RC[-22]>0)*AND(RC[-21]=0)*AND(RC[-5]=""Roster"")*AND(RC[-6]=""Listed""),1,"""")"
    .Cells(51, LastCol + 4).Value = "Negative Stock"
    .Cells(52, LastCol + 4).FormulaR1C1 = "=IF((RC[-24]=4)*AND(RC[-23]<0)*AND(RC[-6]=""Roster"")*AND(RC[-7]=""Listed""),1,"""")"
    .Cells(51, LastCol + 5).Value = "Overstock"
    .Cells(52, LastCol + 5).FormulaR1C1 = "=IF((RC[-25]=4)*AND(RC[-21]>14)*AND(RC[-7]=""Roster"")*AND(RC[-8]=""Listed""),1,"""")"
    .Cells(51, LastCol + 6).Value = "Dormant Stock"
    .Cells(52, LastCol + 6).FormulaR1C1 = "=IF((RC[-26]=4)*AND(RC[-25]>0)*AND(RC[-24]>10)*AND(RC[-8]=""Roster"")*AND(RC[-9]=""Listed""),1,"""")"
    .Cells(51, LastCol + 7).Value = "Outdated Stock Counts"
    .Cells(52, LastCol + 7).FormulaR1C1 = "=IF((RC[-27]=4)*AND(RC[-26]<>0)*AND(RC[-17]>30)*AND(RC[-9]=""Roster"")*AND(RC[-10]=""Listed""),1,"""")"
    .Cells(51, LastCol + 8).Value = "Total Issues"
    .Cells(52, LastCol + 8).FormulaR1C1 = "=SUM(RC[-7]:RC[-1])*AND(RC[-10]=""Roster"")*AND(RC[-11]=""Listed"")"
    .Range("A51", Cells(51, Columns.Count).End(xlToRight)).SpecialCells(xlCellTypeConstants).Interior.Color = RGB(0, 32, 96)
    .Range("A51", Cells(51, Columns.Count).End(xlToRight)).SpecialCells(xlCellTypeConstants).Font.Color = vbWhite

End With

With ws
    lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
End With

Cells(52, LastCol + 1).Resize(1, 8).AutoFill Destination:=Range(Cells(52, LastCol + 1), Cells(lastrow, LastCol + 8))