vba - Copy from cell if two other cells meet criteria?
问题描述
any help with this would be appreciated.
I am trying to copy a cell from column (G) into another worksheet, if cells from the same row in column (R) = "Y" and column (B) = "Month"
I had it working for just "Y" criteria but as soon as I added the "Month" this button has stopped working?
this is the code I have;
Private Sub CommandButton1_Click()
Dim LR As Long, i As Long
With Sheets("Savings Q4")
LR = .Range("R" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With .Range("R" & i)
If .Value = "Y" Then
With .Range("B" & i)
If .Value = "January" Then
Sheets("Savings Q4").Range("G:G").Copy Destination:=Sheets("Cifas Loadings").Range("A:A")
End If
End With
Next i
End With
End Sub
Any help would be appreciated.
Thanks
解决方案
At least, you forgot one End With and one End If. But anyway it's not correct with logical part.
Dim LR As Long, i As Long
With Sheets("Savings Q4")
LR = .Range("R" & Rows.count).End(xlUp).Row
For i = 1 To LR
With .Range("R" & i)
If .Value = "Y" Then
With .Range("B" & i)
If .Value = "January" Then
Sheets("Savings Q4").Range("G:G").Copy Destination:=Sheets("Cifas Loadings").Range("A:A")
End If
End With
End If
End With
Next i
End With
It's easier to make it w/o With, like
if Range("R"& i).value="Y" and Range("B" & i).value = "Month"
COPE CELLS
end if
推荐阅读
- javascript - Access function within key:value array
- delphi - 确定哪个项目在运行时(Delphi Seattle)
- mysql - 如何从数据库 X 中的表中删除行,其中 ID 存在于数据库 Y 中
- java - 方面定义“方面定义”上发生类依赖错误“org/springframework/context/event/EventListenerFactory”
- vb.net - 一个动画循环,用两个椭圆写的,只用一个椭圆就可以解决吗?
- java - 使用 java 连接到 mySQL 数据库时出错
- c++ - 使用 C++ 在 OpenCV 中按深度维度排序
- c++ - 有没有一种惯用的方法来查找数组变换的最小值/最大值,而无需存储变换或不必要地重新计算?
- android - Android AES/GCM/NoPadding Bug 在一定长度的输入字节上?
- c# - C#通用算法将参数限制为数字