VBA学习代码,找词,考虑下一栏,如果再
VBA learning Code, Look for word, consider the next column and if then
我正在努力自学 VBA,所以我正在做一些基本的事情来弄乱它。我现在要做的是在 A 列中查找单词 "Total" ,找到该单词后考虑下一列的值,然后使用 If then 语句判断列中的数字是否B > then 1 then type on C1 "yes" or B1 "No"
Sub try5()
Set cell = Columns("A:A").Select
Selection.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(columnOffset:=1).Activate.Select
If cell > 1 Then
Range("C1").Value = "yes"
Else
Range("C1").Value = "no"
End If
End Sub
代码的第一部分,如果我运行它自己工作但是VBA给我一个弹出窗口说:"object required"而如果我运行 IF THEN 语句表示 "Type mismatch"。
对不起,如果这是菜鸟问题。
尝试这样的事情。
- 设置您想要搜索值的范围
- 将搜索范围内的
Found
单元格设置为范围
- 在尝试分析之前确定是否找到匹配项(否则
Found
= Nothing
并且逻辑测试 Nothing > 1
没有意义。
- 使用
Offset(r, c)
导航与找到的单元格的 r
行和 c
列距离(这里我们想查看 1 列,所以我们使用 Offset(0,1)
= Offset(,1)
。然后,我们要查看 Col C(距 A 2 列),因此我们使用 Offset(,2)
编写 yes
/no
Sub try5()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim SearchRange As Range, Found As Range
Set SearchRange = ws.Range("A:A")
Set Found = SearchRange.Find(What:="Total", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Found Is Nothing Then 'If a match is NOT found
MsgBox "'Total' Not Found in Range " & SearchRange.Address(False, False)
Else 'If a match is found
If Found.Offset(, 1) > 1 Then
Found.Offset(, 2) = "yes"
Else
Found.Offset(, 2) = "no"
End If
End If
End Sub
我也不推荐使用.Select
。您应该始终尝试找到一种方法来明确说明要修改的 Workbook
、Sheet
、Range
或 Cell
。 None 这些对象必须是 Active
或 Selected
才能更改、删除、移动、复制等。
我正在努力自学 VBA,所以我正在做一些基本的事情来弄乱它。我现在要做的是在 A 列中查找单词 "Total" ,找到该单词后考虑下一列的值,然后使用 If then 语句判断列中的数字是否B > then 1 then type on C1 "yes" or B1 "No"
Sub try5()
Set cell = Columns("A:A").Select
Selection.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(columnOffset:=1).Activate.Select
If cell > 1 Then
Range("C1").Value = "yes"
Else
Range("C1").Value = "no"
End If
End Sub
代码的第一部分,如果我运行它自己工作但是VBA给我一个弹出窗口说:"object required"而如果我运行 IF THEN 语句表示 "Type mismatch"。
对不起,如果这是菜鸟问题。
尝试这样的事情。
- 设置您想要搜索值的范围
- 将搜索范围内的
Found
单元格设置为范围 - 在尝试分析之前确定是否找到匹配项(否则
Found
=Nothing
并且逻辑测试Nothing > 1
没有意义。 - 使用
Offset(r, c)
导航与找到的单元格的r
行和c
列距离(这里我们想查看 1 列,所以我们使用Offset(0,1)
=Offset(,1)
。然后,我们要查看 Col C(距 A 2 列),因此我们使用Offset(,2)
编写yes
/no
Sub try5()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim SearchRange As Range, Found As Range
Set SearchRange = ws.Range("A:A")
Set Found = SearchRange.Find(What:="Total", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Found Is Nothing Then 'If a match is NOT found
MsgBox "'Total' Not Found in Range " & SearchRange.Address(False, False)
Else 'If a match is found
If Found.Offset(, 1) > 1 Then
Found.Offset(, 2) = "yes"
Else
Found.Offset(, 2) = "no"
End If
End If
End Sub
我也不推荐使用.Select
。您应该始终尝试找到一种方法来明确说明要修改的 Workbook
、Sheet
、Range
或 Cell
。 None 这些对象必须是 Active
或 Selected
才能更改、删除、移动、复制等。