要点
- 如何遍历所有列
colLong = Sheet1.UsedRange.Rows.Count
oriLong = 2
For i = oriLong To colLong
D = "D" & i
Next
- 如何将一整列用公式填充
很多excel的公式 加上WorksheetFunction.前缀,就可以使用
- 如何公式变值
For Each cell In Selection
cell.Value = cell.Value
Next
- VB中的Linux时间表达
DateDiff("s", "01/01/1970 00:00:00", Now())
- 多个if叠加
IF XXX THEN
ELSE
END IF
在套if的时候,里面的END IF也要写在里面
Sub buttom_Click()
Application.ScreenUpdating = False
Dim t
Dim rng As Range
Dim i As Integer
Dim D As String
Dim W As String
Dim N As String
Dim H As String
Dim F As String
Dim colI As String
Dim G As String
Dim L As String
Dim cell As Range
t = Timer
colLong = Sheet1.UsedRange.Rows.Count
oriLong = 2
Range("N2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
For i = oriLong To colLong
D = "D" & i
W = "W" & i
N = "N" & i
H = "H" & i
F = "F" & i
colI = "I" & i
G = "G" & i
L = "L" & i
' Range(W).Value = WorksheetFunction.CountIf(Range("D1:" & D), Range(D))
' If Range(W).Value = 1 Then
Min = Range(F).Value
Rank = WorksheetFunction.CountIf(Range("D1:" & D), Range(D))
If Min > 0 Then
If Rank = 1 Then
Demand = WorksheetFunction.Sum(Range(colI & ":I" & (i + Min - 1)))
week_Avg_request = Demand / Min
If Demand = 0 Then
Range(N).Value = Range(G).Value
Else
Range(N).Value = WorksheetFunction.RoundDown(Range(L).Value / week_Avg_request, 0)
End If
Else
Demand = ""
week_Avg_request = ""
End If
ElseIf Min <= 0 Then
If Rank = 1 Then
Demand = 0
week_Avg_request = 0
Range(N).Value = Range(G).Value
Else
Demand = ""
week_Avg_request = ""
End If
End If
' If Rank = 1 Then
' If Demand = 0 Then
' Range(N).Value = Range(G).Value
' Else
' Range(N).Value = WorksheetFunction.RoundDown(Range(L).Value / week_Avg_request, 0)
' End If
' End If
Next
Columns("N:N").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=IF(R[-1]C-1<0,0,R[-1]C-1)"
' For Each cell In Selection
' cell.Value = cell.Value
' Next
ActiveWorkbook.Save
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\InventoryForecast_ImportData" & DateDiff("s", "01/01/1970 00:00:00", Now()) & ".csv", FileFormat:=xlCSVUTF8
Application.ScreenUpdating = True
MsgBox "CSV已生成," & "总运行时间为" & Timer - t & "秒"
End Sub