使用日期函数删除列

时间:2015-05-15 10:30:18

标签: excel vba excel-vba

如果我错误地发布了这个问题,我很抱歉,我仍然习惯了StackExchange网络。

我创建了一个用户表单来复制模板工作簿,填写所选月份和年份的日期,然后删除额外的列,因为模板工作簿有31个预先格式化的列。

我遇到的问题是当我点击"输入"在用户表单中的按钮,工作簿重复并保存,并将日期放在顶部,但是,它没有完全执行"删除" "复制表"下的行。对于工作簿中的每个工作表(每日销售,总库存,交货,损益表,利润),当它放入顶部的日期时,应该删除大于"天"的额外列。并且在该月的第一个日期之后不到31列。

Option Explicit
Private Sub UserForm_Initialize()
InitializeMonthsCombo
InitializeYearsCombo
End Sub
Private Sub InitializeMonthsCombo()
Dim months() As String
months =    Split("January,February,March,April,May,June,July,August,_
September,October,November,December", ",")

Dim i As Integer
For i = LBound(months) To UBound(months)
    Me.CmboMonth.AddItem months(i)
Next
End Sub
Private Sub InitializeYearsCombo()

Const startYear As Integer = 2015
Const endYear As Integer = 2035

Dim i As Integer
For i = startYear To endYear
    Me.CmboYear.AddItem i
Next
End Sub
Private Sub CmdEnter_Click()
Dim Days As Integer
Dim StartDate As Date

    StartDate = CDate("1-" & CmboMonth.Value & "-" & CmboYear.Value)
    Days = (DateDiff("d", StartDate, DateAdd("m", 1, StartDate))) - 1

'Duplicate Sheet
Sheets(Array("Daily Sales", "Total Inventory", "Deliveries", "Income   
Statement", "Profits")).Copy


        'Daily Sales
        Dim DailySales As Worksheet
        Set DailySales = Worksheets("Daily Sales")

        Populate DailySales, DailySales.Range("B6"),   
        DailySales.Range(DailySales.Cells(6, 2), DailySales.Cells(6, 2 + 
            Days))
        Range(Cells(1, 3 + Days), Cells(1, 32)).EntireColumn.Delete

        'Total Inventory
        Dim TotalInventory As Worksheet
        Set TotalInventory = Worksheets("Total Inventory")

        Populate TotalInventory, TotalInventory.Range("C5"), 
         TotalInventory.Range(TotalInventory.Cells(5, 3), 
         TotalInventory.Cells(5, 3 + Days))
        Populate TotalInventory, TotalInventory.Range("C5"), 
         TotalInventory.Range(TotalInventory.Cells(5, 3), 
         TotalInventory.Cells(5, 2))
        Range(Cells(1, 4 + Days), Cells(1, 33)).EntireColumn.Delete

        'Deliveries
        Dim Deliveries As Worksheet
        Set Deliveries = Worksheets("Deliveries")

        Populate Deliveries, Deliveries.Range("B6"), 
        Deliveries.Range(Deliveries.Cells(6, 2), Deliveries.Cells(6, 2 + 
              Days))
        Range(Cells(1, 3 + Days), Cells(1, 32)).EntireColumn.Delete

        'Income Statement
        Dim IncomeStatement As Worksheet
        Set IncomeStatement = Worksheets("Income Statement")

        Populate IncomeStatement, IncomeStatement.Range("C4"), 
            IncomeStatement.Range(IncomeStatement.Cells(4, 3), 
            IncomeStatement.Cells(4, 3 + Days))
        Range(Cells(1, 4 + Days), Cells(1, 33)).EntireColumn.Delete

        'Profits
        Dim Profits As Worksheet
        Set Profits = Worksheets("Profits")

        Populate Profits, Profits.Range("E4"), 
           Profits.Range(Profits.Cells(4, 5), Profits.Cells(4, 5 + Days))
        Range(Cells(1, 6 + Days), Cells(1, 35)).EntireColumn.Delete
    End Sub
Public Sub Populate(DestSheet As Worksheet, first As Range, dest As  
Range)
    Dim StartDate As Date
    StartDate = CDate("1-" & CmboMonth.Value & "-" & CmboYear.Value)

    first.Value = StartDate
    first.AutoFill Destination:=dest, Type:=xlFillValues

    DestSheet.Cells.EntireColumn.AutoFit

'Close Userform
Unload Me

End Sub

Private Sub CmdCancel_Click()
'Close Userform
Unload Me
End Sub

1 个答案:

答案 0 :(得分:0)

我解决了这个问题!问题是当我去删除每张纸上的列时,我没有更改对该纸张的引用,因此它删除了活动纸张上的列,这是第一张“每日销售”,五次,而不是改变床单。我需要澄清范围表。

Range(Cells(1, 3 + Days), Cells(1, 32)).EntireColumn.Delete

变为:

Range(Deliveries.Cells(1,3+Days), Deliveries.Cells(1,32))_
.EntireColumn.Delete

我认为R3uK试图告诉我什么,但我不明白。谢谢R3uK!