我在Excel中有以下列:文档日期(所有单元格都有值)和初始处置日期(该列中有空白)。
每个文档日期单元格对应一个初始处理日期单元格。
对于任何空白初始处置日期单元格,我希望将其设置为距相应的文档日期 7天。 (严格空白的单元格)
例如:文件日期= 10/01/2018。所需的初始处置日期= 10/08/2018。
是否存在执行此操作的代码? (顺便说一下,我大约有55,000行和51列)。
非常感谢!任何建议或想法都将受到高度赞赏!
答案 0 :(得分:1)
如果您的“文档日期”在A列中,而“初始处置日期”在B列中,则以下内容将达到您想要的结果:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set the worksheet you are working with, amend as required
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 2 To Lastrow
'loop from row 2 to the last row with data
If ws.Cells(i, "B").Value = "" Then
'if there is no value in Column B then
ws.Cells(i, "B").Value = ws.Cells(i, "A").Value + 7
'add seven days to the date from Column A
End If
Next i
End Sub
答案 1 :(得分:1)
在这种情况下,遍历范围会更快一些。我假设您的数据在Sheet1
上,您的文档日期在Column A
上,并且您的初始沉积在Column B
。
最后,您需要确定是否希望这7天包含周末。我为你们两个都留下了解决方案。您将需要删除其中一个动作语句(在循环中间)
Option Explicit
Sub BetterCallSaul()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim LRow As Long, iRange As Range, iCell As Range
LRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set iRange = ws.Range("B2:B" & LRow)
Application.ScreenUpdating = False
For Each iCell In iRange
If iCell = "" Then
iCell = iCell.Offset(, -1) + 7 'Includes Weekends
iCell = WorksheetFunction.WorkDay(iCell.Offset(, -1), 7) 'Excludes Weekends
End If
Next iCell
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:1)
所有空白处的公式都可以避免延迟在工作表列中循环。
Sub ddPlus7()
Dim dd As Long, didd As Long
With Worksheets("sheet1")
'no error control on the next two lines so those header labels better be there
dd = Application.Match("Document Date", .Rows(1), 0)
didd = Application.Match("Desired Initial Disposition Date", .Rows(1), 0)
On Error Resume Next
With Intersect(.Columns(dd).SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow, _
.Columns(didd).SpecialCells(xlCellTypeBlanks).EntireRow, _
.Columns(didd))
.FormulaR1C1 = "=rc[" & dd - didd & "]+7"
End With
On Error GoTo 0
End With
End Sub