excel - 将 If 语句从单个单元格复制到列中的所有单元格
问题描述
我已经能够使用以下代码使用 If/Else 语句将“打印文档”或“电子交付”输入到单元格 K2 中。我希望尝试为所有 K 列扩展相同的语句(即 K3 搜索 O3 并返回正确的值)。任何帮助将非常感激。非常感谢,迈克尔
Sub InsertType()
Dim score As Integer, result As String
score = Range("O2").Value
If score > 0 Then
result = "Print Documents"
Else
result = "E-Delivery"
End If
Range("K2").Value = result
End Sub
解决方案
您可以循环您的 VBA 代码:)
代码:
Sub InsertType()
Dim ws As Worksheet
Dim score As Integer, result As String
Dim i As Long
Dim lRow As Long
Set ws = ActiveWorkbook.Worksheets("Sheet1") 'Name of worksheet
lRow = ws.Cells(Rows.Count, "O").End(xlUp).Row 'Find the last row in column O
For i = 1 To lRow 'Loop from row 1 to last row
score = ws.Range(ws.Cells(i, "O"), ws.Cells(i, "O")).Value 'Take value from row i and column O
If score > 0 Then
result = "Print Documents"
Else
result = "E-Delivery"
End If
ws.Range(ws.Cells(i, "K"), ws.Cells(i, "K")).Value = result 'Print Result in Row i and column K
Next i
End Sub
结果:
推荐阅读
- javascript - 使用 React Redux Store 的最佳方式是什么?
- python - Atom 脚本环境中打印功能的 SyntaxError
- javascript - 每当消息中提到成员时,如何自动增加值
- qt - 带有自定义插槽功能的连接功能在 Qt 上不起作用
- arrays - 在 mongodb 中返回包含满足条件的数组的完整文档
- java - 通知生成器未收到完整上下文
- javascript - 如何使用 Chrome 扩展安装本机依赖项?
- angular - MEAN 堆栈身份验证问题 RxJS 不起作用?
- c++ - 在访问该类的函数时,如何在我的类的二维向量上使用点运算符?
- angular - Angular中从父组件到子组件的数据共享