我无法在我的代码中实现以下if语句:
IF(O3<0,"Y",N")
有人可以帮助改变我的代码以包含此if语句吗?这是我尝试过的:
If Range("O3:O4183") < 0 Then Range("P3").Value = "Y"
Worksheets("Sample File").Range("P3:P4183").FillDown
ElseIf Range("O3:O4183") > 0 Then Range("P3").Value = "N"
Worksheets("Sample File").Range("P3:P4183").FillDown
End If
完整代码:
Sub stackoverflow()
Dim rng As Range
''sample file creation
''values
Range("A3").Value = "CSH" ''hardcode
Worksheets("Sample File").Range("A3:A4183").FillDown
Set rng = Worksheets("File").Range("C2:C4182")
Worksheets("Sample File").Range("B3").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
Set rng = Worksheets("File").Range("D2:D4182")
Worksheets("Sample File").Range("C3").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
Range("D3").Value = "1" ''hardcode
Worksheets("Sample File").Range("D3:D4183").FillDown
Set rng = Worksheets("File").Range("E2:E4182")
Worksheets("Sample File").Range("E3").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
Set rng = Worksheets("File").Range("E2:E4182")
Worksheets("Sample File").Range("F3").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
Range("G3").Value = "USD" ''hardcode
Worksheets("Sample File").Range("G3:G4183").FillDown
Set rng = Worksheets("File").Range("K2:K4182")
Worksheets("Sample File").Range("H3").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
Set rng = Worksheets("File").Range("F2:F4182")
Worksheets("Sample File").Range("I3").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
Range("J3").Value = "DEALT" ''hardcode
Worksheets("Sample File").Range("J3:J4183").FillDown
Set rng = Worksheets("File").Range("H2:H4182")
Worksheets("Sample File").Range("M3").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
Range("N3").Value = "0" ''hardcode
Worksheets("Sample File").Range("N3:N4183").FillDown
Set rng = Worksheets("File").Range("J2:J4182")
Worksheets("Sample File").Range("O3").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
''If statement: IF(O3<0,"Y",N")
End Sub
答案 0 :(得分:5)
VBA中{bool-expression}
语句有两种有效的语法。
If
这就是你在这里所拥有的:
If {bool-expression} Then
{statements}
[ElseIf {bool-expression} Then]
{statements}
[Else]
{statements}
End If
缩进是误导性的 - 第2行将无条件执行,无论Else
语句中ElseIf
的结果如何。
<script type="text/javascript" src="jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="dependsOn-1.0.0.min.js"></script>
<script>
$(document).ready(function) {
$('#basicTest .subject').dependsOn({
'#basicTest input[type="checkbox"]': {
checked: true
}
});
});
</script
当您需要<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form id="basicTest">
<label>
<input type="checkbox"> Check me
</label>
<input type="text" class="subject">
</form>
</body>
</html>
/ q = {result :{ 'count_select': count_select}}
逻辑时,或者如果要有条件地执行多于1个语句*时,请使用该语法。
* 是使用语句语法有条件地执行多个语句的方法,但它们对可读性有害;远离容易的错误,更喜欢块语法。
答案 1 :(得分:1)
能够想出这个,
Set range1 = Worksheets("Sample File").Range("P3:P4183")
range1.Cells.Value = "=IF(RC[-1]<0,""Y"",""N"")"
Worksheets("Sample File").Range("P3:P4183").FillDown
似乎工作。
答案 2 :(得分:1)
尝试一下
Option Explicit
'
Sub stackoverflow()
Dim src As Range
Set src = Worksheets("File").Range("2:4182")
Dim dst As Range
Set dst = Worksheets("Sample File").Range("3:4183")
' sample file creation
' values
dst.Columns("A") = "CSH" ' hardcode
dst.Columns("D") = "1"
dst.Columns("G") = "USD"
dst.Columns("J") = "DEALT"
dst.Columns("N") = "0"
dst.Columns("B") = src.Columns("C").Value
dst.Columns("C") = src.Columns("D").Value
dst.Columns("E") = src.Columns("E").Value
dst.Columns("F") = src.Columns("E").Value
dst.Columns("H") = src.Columns("K").Value
dst.Columns("I") = src.Columns("F").Value
dst.Columns("M") = src.Columns("H").Value
dst.Columns("O") = src.Columns("J").Value
dst.Columns("P") = "=IF(RC[-1]<0,""Y"",""N"")"
End Sub