下午好,
我是VBA世界的全新人物。我有一个用于记录风险的工作簿,我有一个名为RiskRegister的工作表,其中包含所记录的每个风险的所有数据。我还有一个摘要选项卡,它使用风险登记表中的ID,并使用vlookup公式将相关字段复制到摘要表。我需要更新宏做的是搜索工作表AP上的列AP以搜索" V高",如果找到匹配我需要将列A的值从该行复制到下一个可用的工作表CSR风险报告的A列上的行。我找到了下面的代码,但是当我运行它时,我得到运行时错误424对象需要。有人可以告诉我哪里出错,或者是否有更好的方法来完成这项任务?
Private Sub CommandButton1_Click()
Dim tValue As String
Dim dValue As String
'Get last row value of sheet RiskRegister
a = Worksheets("RiskRegister").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
'If Value = "V High" then copy the value in column A into coloumn 1 of "CSR Risk Report"
If Worksheets("RiskRegister").Cells(i, 42).Value = "V High" Then
tValue = Worksheets("RiskRegister").Cells(i, 1).Value.Copy
Worksheets("CSR Risk Report").Activate
'Get last row of "CSR Risk Report" and paste the value in last row of column A
b = Worksheets("CSR Risk Report").Cells(Rows.Count, 1).End(xlUp).Row
dValue = Worksheets("CSR Risk Report").Cells(b + 1, 1).Select
dValue = tValue
Worksheets("RiskRegister").Activate
End If
Next
' Not sure what this is
Application.CutCopyMode = False
End Sub
我还需要循环查看结果" High"然后" Medium",我打算简单地用新的搜索条件重复if循环。我希望这会将它粘贴到正确的优先级顺序,即V high - >高 - >介质
亲切的问候
亚当
答案 0 :(得分:2)
根据上述评论,试试这个
router.get('/:_categoryName', (req, res) => {
const { _categoryName } = req.params;
Malgezot.findOne({
item : { $in : [{ category: _categoryName }] }
}).then((data) => {
if (data) {
res.status(200).json(data)
}
}).catch((err) => {
res.status(500).json({
message: 'Internal server error'
});
});
});
答案 1 :(得分:1)
首先:声明" Option Explicit"
然后声明你的变量
不,我们可以看到您正在尝试分配方法" .copy"到一个字符串变量。 " .copy" method将值放入剪贴板,而不是变量。
然后你在下面做同样的事情,试图把方法"。选择"在一个字符串......
试试这个:
Option Explicit
Private Sub CommandButton1_Click()
Dim tValue As String
Dim dValue As Range
'Get last row value of sheet RiskRegister
Dim a As Long
a = Worksheets("RiskRegister").Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = 2 To a
'If Value = "V High" then copy the value in column A into coloumn 1 of "CSR Risk Report"
If Worksheets("RiskRegister").Cells(i, 42).Value = "V High" Then
tValue = Worksheets("RiskRegister").Cells(i, 1).Value
'Worksheets("CSR Risk Report").Activate
'Get last row of "CSR Risk Report" and paste the value in last row of column A
Dim b As Long
b = Worksheets("CSR Risk Report").Cells(Rows.Count, 1).End(xlUp).Row
Set dValue = Worksheets("CSR Risk Report").Cells(b + 1, 1)
dValue = tValue
'Worksheets("RiskRegister").Activate
End If
Next
End Sub
注意:" Application.CutCopyMode = False"清除剪贴板。如果您使用.copy和.paste方法,则只需要它。如果我们直接使用变量和值,那么我们就不需要了。
答案 2 :(得分:0)
您不需要复制单元格并更改工作表之间的激活 - 您每次更新时都不需要检查最后一行(变量b),只需添加1
说实话,听起来你只需要用过滤器安排一些东西但为了代码答案,试试这个:
Private Sub CommandButton1_Click()
Dim WSA As Worksheet
Dim WSB As Worksheet
Dim a As Long
Dim b As Long
Dim i As Long
Set WSA = ThisWorkbook.Worksheets("RiskRegister")
Set WSB = ThisWorkbook.Worksheets("CSR Risk Report")
'Get last rows
a = WSA.Cells(WSA.Rows.Count, 1).End(xlUp).Row
b = WSB.Cells(WSB.Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If WSA.Cells(i, 42).Value = "V High" Then
b = b + 1
WSB.Cells(b, 1).Value2 = WSA.Cells(i, 42).Value2
End If
Next
End Sub