我今天在这里是因为我作为实习生(ahum excel支持/咖啡制作者)有一项任务,以找到一种方法来放置4种不同的公式并应用它们
目前,我已经尝试提出一个公式,但是我不知道我是否朝着正确的方向前进:
Sub test()
Dim ws As Worksheet: Set ws = Sheets("Detail")
Dim lr As Long
lr = ws.Cells(Rows.Count, "F").End(xlUp).Row
For Each cell In ws.Range(ws.Cells(2, "F"), ws.Cells(lr, "F"))
cell.Value="IF(H2="NB","",AY2)"
Next cell
End Sub
但是我基本上要把它应用于4列:
"MFR CUSTLINE# PRICE (DYP) DELIVERY
=IF(H2="NB","",AY2) =A2 =IF(P2="","NB",P2) =IF(BR2>(D2+AM2),"STOCK",IF(AR2="0 Weeks","",SUBSTITUTE(AR2," Weeks"," WKS")))
"
4 MFR CUSTLINE PRICE和DELIVERY只是该列的间接费用,因此第1行,然后我必须将公式应用于该列的底部,因为我可以有5行或15000
预先感谢您的帮助和理解,因为vba或编程中的知识是最基本的。
答案 0 :(得分:2)
不需要循环
Sub test()
Dim ws As Worksheet
Set ws = Sheets("Detail")
Dim lr As Long
lr = ws.Cells(Rows.Count, "F").End(xlUp).Row
Dim arr
arr = Array("MFR", "CUSTLINE#", "PRICE (DYP)", "DELIVERY")
ws.Range("F1:I1").Value = arr
arr = Array("=IF(H2=""NB"","""",AY2)", "=A2", "=IF(P2="""",""NB"",P2)", "=IF(BR2>(D2+AM2),""STOCK"",IF(AR2=""0 Weeks"","""",SUBSTITUTE(AR2,"" Weeks"","" WKS"")))")
ws.Range("F2:I" & lr).Formula = arr
End Sub
答案 1 :(得分:1)
一种解决方法是:
Sub test()
Dim ws As Worksheet: Set ws = Sheets("Detail")
Dim myRow As Long
For myRow = 2 to ws.Cells(Rows.Count, "F").End(xlUp).Row
Range("XA" & myRow).Formula = "=IF(H" & myRow & "=""NB"","""",AY" & myRow & ")"
Range("XB" & myRow).Formula = "=A" & myRow
Range("XC" & myRow).Formula = "=IF(P" & myRow & "="""",""NB"",P" & myRow & ")"
Range("XD" & myRow).Formula = "=IF(BR" & myRow & ">(D" & myRow & "+AM" & myRow & "),""STOCK"",IF(AR" & myRow & "=""0 Weeks"","""",SUBSTITUTE(AR" & myRow & ","" Weeks"","" WKS"")))"
Next myRow
End Sub
注意:这假定公式位于XA
,XB
,XC
和XD
列中
编辑:修正了错误的报价。