(Excel VBA)如何在找到单元格之前将这些列的多列和cell.value插入Excel工作表?

时间:2018-04-21 02:05:53

标签: excel vba excel-vba excel-formula

1。背景&目的
我有一张带有多列表格的Excel表格。 我想通过宏插入一些列(黄色):

  • "总费用"之前" PIC"公式为:Total Cost = Man-hour * C (其中C是常数)
  • " Review.Date"," Reviewer"," Year"之前"月"以及根据" Inspected.Date"确定财政年度("年")的公式 例如:Cell of Year = YEAR("Inspected.Date")+IF(MONTH("Inspected.Date")>=$D$1,1,0)

请参阅此图片了解更多详情。

Insert New Columns to Excel

2。问题&问题
我可以遍历所有列并检查Column.header =" PIC"或"月",然后插入新列。但我不知道如何插入"列标题"和"该列中所有单元格的值"使用公式因为插入新列后,某些列的位置将被更改。 那么,我怎么能用宏呢? 任何帮助将受到高度赞赏。 非常感谢你的关注。

第3。这是我目前的VBA

Sub InsertNewColumn()
   'Edit SPO Documents.
   'Consider worksheet"Sheet1"

   With Worksheets("Sheet1")

       '1. Find out last columns & last row in target sheet.
        LastCol = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
                  SearchDirection:=xlPrevious).Column
        LastRow = .Cells.Find(What:="*", SearchOrder:=xlByRows, _
                  SearchDirection:=xlPrevious).Row        
       '2.Loop through all columns and check column header. 
       '  If column header is "PIC" or "Month", then insert new column

        For i = LastCol To 1 Step -1            
            On Error Resume Next            
            CellValue = .Cells(1, i)   'Get Column header   

            'Insert column "Total Cost"
            If CellValue = "PIC" Then
                .Cells(1,i).EntireColumn.Insert Shift:=xlToLeft
                .Cells(1,i).Value2 = "Total Cost" 'Add column header            
            End If

            'Insert column "Year"
            If CellValue = "Month" Then
                .Cells(1,i).EntireColumn.Insert Shift:=xlToLeft
                .Cells(1,i).Value2 = "Year" 'Add column header

                'Add formula to year
                With Sheets("Sheet1").Columns(i)
                    For j = LastRow To 2 Step -1                       
                        'Sheets("Sheet1").Cells(j, i).Formula = "=YEAR("Inspected.Date")+IF(MONTH("Inspected.Date")>=$D$1,1,0)"
                        'I want to add formula to determine fiscal year for cell of Year based on "Inspected.Date"
                        'but I dont know how to do
                    Next j               
                End With
            End If  
        Next i   
    End With
    Application.ScreenUpdating = True   
End Sub

1 个答案:

答案 0 :(得分:0)

如果列尚不存在,那么您不需要For ... Next循环来定位和插入列的可靠方法。

dim c1 as variant, c2 as variant
with worksheets("sheet1")
    c1 = application.match("pic", .rows(1), 0)
    c2 = application.match("total cost", .rows(1), 0)
    if iserror(c2) and not iserror(c1) then
        .cells(1, c1).entirecolumn.insert
        .cells(1, c1) = "total cost"
    end if

    c1 = application.match("month", .rows(1), 0)
    c2 = application.match("reviewed.date", .rows(1), 0)
    if iserror(c2) and not iserror(c1) then
        c2 = application.match("inspected.date", .rows(1), 0)
        .cells(1, c1).resize(1, 3).entirecolumn.insert
        .cells(1, c1).resize(1, 3) = array("reviewed.date", "reviewer", "year")
        .range(.cells(2, c1+2), .cells(.rows.count, c2).end(xlup).offset(0, 4)).formula = _
            "=year(" & .cells(2, c2).address(0, 0) & ")+(month(" & .cells(2, c2).address(0, 0) & ")>=$d$1)"
    end if
end with

请注意,我已将公式修改为更简单的形式。

= YEAR("Inspected.Date")+(MONTH("Inspected.Date")>=$D$1)