vba - 循环遍历范围,如果单元格包含值,则复制到列中的下一个空单元格
问题描述
我很难找到任何有我疑问的东西。我可以找到我需要的不同部分,但无法将它们放在一起。
我需要做的是查看一个设定的范围,如果值在 0.001 和 0.26 之间,然后复制单元格并粘贴到列(“DA”)中的下一个空单元格中,还从找到值的同一行复制单元格但复制从列(“C”)并粘贴到列(“DB”)旁边。
我知道我必须使用 If 语句循环,并且当它找到与条件匹配时必须偏移单元格。但我不能把它放在一起。
我已经尝试了以下代码。
Sub COPYcell()
Dim Last As Long
Dim i As Long, unionRng As Range
Last = 61
Dim lastrow As Long
lastrow = Sheets("Sheet1").Range("DA100").End(xlUp).Row
For i = 5 To Last
If (.Cells(i, "J").Value) >= 0.01 And (.Cells(i, "J").Value) <= 0.26 Then
'Cells(i, "DA").Value = Cells(i, "J").Value
Range(i, "J").Copy = Range("DA" & lastrow)
Cells(i, "J").Offset(, -8) = Range("DB" & lastrow)
Range("DC" & lastrow) = "July"
End If
Next i
End Sub
解决方案
尝试以下操作:
Option Explicit
Public Sub COPYcell()
Dim last As Long, sht1 As Worksheet
Dim i As Long, unionRng As Range, lastrow As Long, nextRow
Application.ScreenUpdating = False
Set sht1 = Worksheets("Sheet1")
last = 61
With sht1
lastrow = .Cells(.Rows.Count, "DA").End(xlUp).Row
nextRow = IIf(lastrow = 1, 1, lastrow + 1)
For i = 5 To last
If .Cells(i, "J").Value >= 0.01 And .Cells(i, "J").Value <= 0.26 Then '1%=26%
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, .Cells(i, "J"))
Else
Set unionRng = .Cells(i, "J")
End If
End If
Next i
If Not unionRng Is Nothing Then
unionRng.Copy .Range("DA" & nextRow)
unionRng.Offset(0, -7).Copy .Range("DB" & nextRow)
End If
End With
Application.ScreenUpdating = False
End Sub
推荐阅读
- flutter - Flutter 找不到小部件属性:'child' 未定义
- android - 如何在flutter中从内部存储中打开PDF文件
- php - 添加一个活动类以显示活动 CPT 分类/类别
- laravel-livewire - Livewire Lifecycle Hooks 自我更新到上层
- c# - Ubuntu Rider 中的 Lambda 测试工具 3.1:找不到类型
- node.js - 为什么 vscode 推荐 node.js (typescript) 内置为“node:assert”而不是“asset”?
- winforms - 在 net5 windows 应用程序中引用 windows uwp sdk
- javascript - js中的打开和关闭按钮
- c# - 当我调用此用户控件时,为什么 xaml 一直说“字典中不存在给定的键“惯用语”?
- github - 如何在 GitHub Pages 网站上更改我的网站的标题文本?