[英]EXCEL VBA replacing cell's content
I'd like to replace a cell's text in case a condition is fullfilled, but the data is in another file. 我想在满足条件的情况下替换单元格的文本,但数据在另一个文件中。 How can I do it?
我该怎么做? I used something like this:
我使用过这样的东西:
Sub test()
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
If targetSheet.Range("Q19", "BL23").Text = "OK" Then
sourceSheet.Range("GB38", "GH38").Text = "Agreed"
End If
End Sub
You need to: 你需要:
EDIT#1: 编辑#1:
Here is the reason I suggest testing the cells separately. 这就是我建议分别测试细胞的原因。 Consider:
考虑:
Sub qwerty()
MsgBox Range("C3,A1") = "X"
End Sub
On an empty worksheet, we get False 在空工作表上,我们得到False
If we set C3 to an X we get True 如果我们将C3设置为X,我们就会得到True
If we clear C3 and set A1 to an X we get False ! 如果我们清除C3并将A1设置为X,我们会得到假 !
It turns out that for a dis-joint range, only the first element is examined........the others are ignored! 事实证明,对于一个脱离范围,只检查第一个元素........其他元素被忽略了!
here an example how you can achieve replacement by condition 这里举例说明如何通过条件实现替换
'''''
If targetSheet.[Q19, BL23].text= "ok" Then
sourceSheet.Cells.Replace what:="Search string", replacement:="Replacement String"
End If
'''''
the cell's content doesn't change to "Agreed"
单元格的内容不会更改为“已约定”
here your updated code, but this is not replacement as written in title 这里是您更新的代码,但这不是标题中所写的替代品
''''''
If targetSheet.[Q19, BL23].text = "OK" Then
sourceSheet.[GB38, GH38].Value = "Agreed"
End If
'''''
for verification of the multiple range content use .text
, but to insert value in multiple range you need to use .value
要验证多范围内容使用
.text
,但要在多个范围内插入值,您需要使用.value
test in screenshots below 测试下面的截图
wrong way of the multiple range verification using .value
使用
.value
多范围验证的错误方法
wrong way of the multiple range verification without specified range property 没有指定范围属性的多范围验证的错误方式
right way of the multiple range verification using .text
使用
.text
多范围验证的正确方法
Try adding 尝试添加
customerWorkbook.Close customerWorkbook.Saved = True
before your End Sub
statement. 在你的
End Sub
声明之前。 That should fix it. 那应该解决它。
Got it! 得到它了! Thanks for the support
感谢您的支持
Sub test()
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
Dim x As String
If sourceSheet.Range("Q19").Text = "OK" Then
x = "Agreed"
End If
targetSheet.Range("GB38", "GH38") = x
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.