公式通过从输入框中选择在循环内返回#NAME
formula returning #NAME inside loop via selection from input box
我有一个小宏,我希望它执行以下操作:第一个输入框要求用户 select 一个空范围,他希望在此范围的每个单元格中包含 debit/Credit… 第二个输入框要求用户 select 一个金额范围(该范围内每个单元格中的每个金额可以是负数或正数)。然后从这 2 select 离子中,我希望它 returns “D”(借方)或“C”(贷方)在第一个 select 范围内的每个单元格上输入框。不幸的是,我的宏无法正常工作,事实上,当我尝试执行它时,它 returns 我#NAME? (而不是“C”或“D”)在范围内的每个单元格中 selected 通过我的第一个输入框。例如在下图中,我通过我的第一个输入框 select 编辑范围 K2:K5(作为空范围)并通过我的第二个输入框编辑范围 C2:C5,在执行宏之后,它 returns 我在范围 K2:K5 的每个单元格中的#NAME。
非常感谢您的帮助。
哈维
请在下面找到我的 VBA 代码:
Sub ReturncorrectCreditdebitcolumnbasedonselectioninputbox()
Dim c As Range
Dim WorkRng2 As Range
Dim WorkRng As Range
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Please select the range where you want your Dedit/Credit", xTitleId, WorkRng.Address, Type:=8)
Set WorkRng2 = Application.Selection
Set WorkRng2 = Application.InputBox("Please select the range of amounts", xTitleId, WorkRng.Address, Type:=8)
For Each c In WorkRng
c.FormulaR1C1 = _
"=If(WorkRng2.Value>0, ""D"",""C"")"
Next c
End Sub
您无法一次比较多个单元格的值,因此它仅在 WorkRng2
是单个单元格时才有效。试试下面的方法,这是我对你正在尝试做的事情的最佳猜测。
Sub ReturncorrectCreditdebitcolumnbasedonselectioninputbox()
Dim c As Long
Dim WorkRng2 As Range
Dim WorkRng As Range
Set WorkRng = Application.InputBox("Please select the range where you want your Dedit/Credit", , , Type:=8)
Set WorkRng2 = Application.InputBox("Please select the range of amounts", , , Type:=8)
If WorkRng.Count <> WorkRng2.Count Then
MsgBox "Ranges must be same size"
Else
For c = 1 To WorkRng.Count
WorkRng.Cells(c).Formula = "=If(" & WorkRng2.Cells(c).Value & ">0, ""D"",""C"")"
Next c
End If
End Sub
我有一个小宏,我希望它执行以下操作:第一个输入框要求用户 select 一个空范围,他希望在此范围的每个单元格中包含 debit/Credit… 第二个输入框要求用户 select 一个金额范围(该范围内每个单元格中的每个金额可以是负数或正数)。然后从这 2 select 离子中,我希望它 returns “D”(借方)或“C”(贷方)在第一个 select 范围内的每个单元格上输入框。不幸的是,我的宏无法正常工作,事实上,当我尝试执行它时,它 returns 我#NAME? (而不是“C”或“D”)在范围内的每个单元格中 selected 通过我的第一个输入框。例如在下图中,我通过我的第一个输入框 select 编辑范围 K2:K5(作为空范围)并通过我的第二个输入框编辑范围 C2:C5,在执行宏之后,它 returns 我在范围 K2:K5 的每个单元格中的#NAME。 非常感谢您的帮助。 哈维 请在下面找到我的 VBA 代码:
Sub ReturncorrectCreditdebitcolumnbasedonselectioninputbox()
Dim c As Range
Dim WorkRng2 As Range
Dim WorkRng As Range
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Please select the range where you want your Dedit/Credit", xTitleId, WorkRng.Address, Type:=8)
Set WorkRng2 = Application.Selection
Set WorkRng2 = Application.InputBox("Please select the range of amounts", xTitleId, WorkRng.Address, Type:=8)
For Each c In WorkRng
c.FormulaR1C1 = _
"=If(WorkRng2.Value>0, ""D"",""C"")"
Next c
End Sub
您无法一次比较多个单元格的值,因此它仅在 WorkRng2
是单个单元格时才有效。试试下面的方法,这是我对你正在尝试做的事情的最佳猜测。
Sub ReturncorrectCreditdebitcolumnbasedonselectioninputbox()
Dim c As Long
Dim WorkRng2 As Range
Dim WorkRng As Range
Set WorkRng = Application.InputBox("Please select the range where you want your Dedit/Credit", , , Type:=8)
Set WorkRng2 = Application.InputBox("Please select the range of amounts", , , Type:=8)
If WorkRng.Count <> WorkRng2.Count Then
MsgBox "Ranges must be same size"
Else
For c = 1 To WorkRng.Count
WorkRng.Cells(c).Formula = "=If(" & WorkRng2.Cells(c).Value & ">0, ""D"",""C"")"
Next c
End If
End Sub