我发现问题和答案与我的问题非常相似,但没有解决我遇到的具体问题。用我的宏我想说:
如果列“M”,分析工作表上的第6行不为空且列“B”第6行和第“C”行第6行和第“第6行”第6行(所有在分析工作表上)都为空且列“ C“固定成本测试数据工作表上的第6行不为空且固定成本测试数据上的列”C“第6行的值>>今天的日期,然后我想要列”M“的值,第6行上的分析工作表计算如下:
Sheets("Analysis Worksheet").Range("M" & i).Value = (Sheets("Analysis Worksheet").Range("M" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) - ((Sheets("Analysis Worksheet").Range("M" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) * (Sheets("Analysis Worksheet").Range("E" & i).Value * 0.01))
我想要i = j。 i和j是相同的数字/同一行。
我需要在代码中更改什么,以便在分析工作表上对行执行计算时,它在固定成本测试数据工作表上使用相同行的值,并且当它们向下移动时行保持“同步”工作表在一起?
这是我对宏的代码:
Sub ReduceCost_Percentage()
With Worksheets("Analysis Worksheet")
Dim i As Long
Dim LastRow As Long
LastRow = Range("X" & Rows.Count).End(xlUp).Row
Dim j As Long
j = i
Dim LRow As Long
LRow = Worksheets("Fixed Cost Test Data").Range("C" & Rows.Count).End(xlUp).Row
For i = 5 To LastRow
For j = i To i
If Sheets("Analysis Worksheet").Range("E" & i).Value > 0 And IsEmpty(Sheets("Analysis Worksheet").Range("B" & i).Value) _
And IsEmpty(Sheets("Analysis Worksheet").Range("C" & i).Value) And IsEmpty(Sheets("Analysis Worksheet").Range("D" & i).Value) _
And Not IsEmpty(Sheets("Analysis Worksheet").Range("M" & i).Value) And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("C" & j).Value) And (Date >= Worksheets("Fixed Cost Test Data").Range("C" & j).Value) Then
Sheets("Analysis Worksheet").Range("M" & i).Value = (Sheets("Analysis Worksheet").Range("M" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) - ((Sheets("Analysis Worksheet").Range("M" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) * (Sheets("Analysis Worksheet").Range("E" & i).Value * 0.01))
ElseIf Sheets("Analysis Worksheet").Range("E" & i).Value > 0 And IsEmpty(Sheets("Analysis Worksheet").Range("B" & i).Value) _
And IsEmpty(Sheets("Analysis Worksheet").Range("C" & i).Value) And IsEmpty(Sheets("Analysis Worksheet").Range("D" & i).Value) _
And Not IsEmpty(Sheets("Analysis Worksheet").Range("M" & i).Value) And (IsEmpty(Worksheets("Fixed Cost Test Data").Range("C" & j).Value) Or Worksheets("Fixed Cost Test Data").Range("C" & j).Value > Date) Then
Sheets("Analysis Worksheet").Range("M" & i).Value = Sheets("Analysis Worksheet").Range("M" & i).Value - (Sheets("Analysis Worksheet").Range("M" & i).Value * (Sheets("Analysis Worksheet").Range("E" & i).Value * 0.01))
End If
Next j
Next i
For i = 5 To LastRow
For j = i To i
If Sheets("Analysis Worksheet").Range("E" & i).Value > 0 And IsEmpty(Sheets("Analysis Worksheet").Range("B" & i).Value) _
And IsEmpty(Sheets("Analysis Worksheet").Range("C" & i).Value) And IsEmpty(Sheets("Analysis Worksheet").Range("D" & i).Value) _
And Not IsEmpty(Sheets("Analysis Worksheet").Range("N" & i).Value) And Not IsEmpty(Sheets("Fixed Cost Test Data").Range("C" & j).Value) And (Date >= Sheets("Fixed Cost Test Data").Range("C" & j).Value) Then
Sheets("Analysis Worksheet").Range("N" & i).Value = (Sheets("Analysis Worksheet").Range("N" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) - ((Sheets("Analysis Worksheet").Range("N" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) * (Sheets("Analysis Worksheet").Range("E" & i).Value * 0.01))
ElseIf Sheets("Analysis Worksheet").Range("E" & i).Value > 0 And IsEmpty(Sheets("Analysis Worksheet").Range("B" & i).Value) _
And IsEmpty(Sheets("Analysis Worksheet").Range("C" & i).Value) And IsEmpty(Sheets("Analysis Worksheet").Range("D" & i).Value) _
And Not IsEmpty(Sheets("Analysis Worksheet").Range("N" & i).Value) And (IsEmpty(Sheets("Fixed Cost Test Data").Range("C" & j).Value) Or Sheets("Fixed Cost Test Data").Range("C" & j).Value > Date) Then
Sheets("Analysis Worksheet").Range("N" & i).Value = Sheets("Analysis Worksheet").Range("N" & i).Value - (Sheets("Analysis Worksheet").Range("N" & i).Value * (Sheets("Analysis Worksheet").Range("E" & i).Value * 0.01))
End If
Next j
Next i
'....Continues until Cell "X"
End With
End Sub
答案 0 :(得分:0)
有两件事:当你使用“with”块(例如For j = i To i
)时,可以从该块中的几乎所有代码中省略它。此外,Sub ReduceCost_Percentage()
With Worksheets("Analysis Worksheet")
Dim i As Long
Dim LastRow As Long
LastRow = Range("X" & Rows.Count).End(xlUp).Row
Dim j As Long
Dim LRow As Long
LRow = Worksheets("Fixed Cost Test Data").Range("C" & Rows.Count).End(xlUp).Row
For i = 5 To LastRow
For j = i To LRow
If .Range("E" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) _
And IsEmpty(.Range("C" & i).Value) And IsEmpty(.Range("D" & i).Value) _
And Not IsEmpty(.Range("M" & i).Value) And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("C" & j).Value) And (Date >= Worksheets("Fixed Cost Test Data").Range("C" & j).Value) Then
.Range("M" & i).Value = (.Range("M" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) - ((.Range("M" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) * (.Range("E" & i).Value * 0.01))
ElseIf .Range("E" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) _
And IsEmpty(.Range("C" & i).Value) And IsEmpty(.Range("D" & i).Value) _
And Not IsEmpty(.Range("M" & i).Value) And (IsEmpty(Worksheets("Fixed Cost Test Data").Range("C" & j).Value) Or Worksheets("Fixed Cost Test Data").Range("C" & j).Value > Date) Then
.Range("M" & i).Value = .Range("M" & i).Value - (.Range("M" & i).Value * (.Range("E" & i).Value * 0.01))
End If
Next j
Next i
For i = 5 To LastRow
For j = i To LRow
If .Range("E" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) _
And IsEmpty(.Range("C" & i).Value) And IsEmpty(.Range("D" & i).Value) _
And Not IsEmpty(.Range("N" & i).Value) And Not IsEmpty(Sheets("Fixed Cost Test Data").Range("C" & j).Value) And (Date >= Sheets("Fixed Cost Test Data").Range("C" & j).Value) Then
.Range("N" & i).Value = (.Range("N" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) - ((.Range("N" & i).Value - Sheets("Fixed Cost Test Data").Range("B" & j).Value) * (.Range("E" & i).Value * 0.01))
ElseIf .Range("E" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) _
And IsEmpty(.Range("C" & i).Value) And IsEmpty(.Range("D" & i).Value) _
And Not IsEmpty(.Range("N" & i).Value) And (IsEmpty(Sheets("Fixed Cost Test Data").Range("C" & j).Value) Or Sheets("Fixed Cost Test Data").Range("C" & j).Value > Date) Then
.Range("N" & i).Value = .Range("N" & i).Value - (.Range("N" & i).Value * (.Range("E" & i).Value * 0.01))
End If
Next j
Next i
'....Continues until Cell "X"
End With
End Sub
将无所作为,它在启动时完成。你可能想用Lrow。
也许这会更好用:
{{1}}
答案 1 :(得分:0)
注意:我将此作为答案发布,因为它太长而无法放入评论框。
谢谢你!我发现我的代码的问题是Date函数,我在第二个工作表上的行号并不需要单独的变量(j)。在我有Date >= Sheets("Fixed Cost Test Data").Range("C" & j).Value Then
的地方,我应该有TodayDate >= Worksheets("Fixed Cost Test Data").Range("C" & i).Value Then
。我的新工作代码是:
Sub ReduceCost_Percentage()
With Worksheets("Analysis Worksheet")
Dim i As Long
Dim LastRow As Long
LastRow = Range("X" & Rows.Count).End(xlUp).Row
Dim TodayDate As Date
TodayDate = Format(Date, "DD/MM/YYYY")
For i = 5 To LastRow
If .Range("E" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) _
And IsEmpty(.Range("C" & i).Value) And IsEmpty(.Range("D" & i).Value) _
And Not IsEmpty(.Range("M" & i).Value) And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("C" & i).Value) _
And TodayDate >= Worksheets("Fixed Cost Test Data").Range("C" & i).Value Then
.Range("M" & i).Value = (.Range("M" & i).Value - Worksheets("Fixed Cost Test Data").Range("B" & i).Value) - ((.Range("M" & i).Value - Worksheets("Fixed Cost Test Data").Range("B" & i).Value) * (.Range("E" & i).Value * 0.01))
ElseIf .Range("E" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) _
And IsEmpty(.Range("C" & i).Value) And IsEmpty(.Range("D" & i).Value) _
And Not IsEmpty(.Range("M" & i).Value) And (IsEmpty(Worksheets("Fixed Cost Test Data").Range("C" & i).Value) Or Worksheets("Fixed Cost Test Data").Range("C" & i).Value > Date) Then
.Range("M" & i).Value = .Range("M" & i).Value - (.Range("M" & i).Value * (.Range("E" & i).Value * 0.01))
End If
Next I
谢谢!