Excel 2010 VBA - 使用 Like 运算符复制单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7470204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:03:13  来源:igfitidea点击:

Excel 2010 VBA - using the Like operator to copy cell values

excelvba

提问by Lindstrom

I want to copy the contents of a cell only if the cell contains times in the following format: 9:00-5:00pm. (The cell may contain abbreviations such as "N/A", "RDO", "Leave" etc. I tried this:

仅当单元格包含以下格式的时间时,我才想复制单元格的内容:9:00-5:00pm。(单元格可能包含缩写,如“N/A”、“RDO”、“Leave”等。我试过这个:

If Worksheets(2).Cells(R, c).Value Like "*-*" Then

Worksheets(26).Cells(x, c).Value = Worksheets(2).Cells(R, c).Value
end if

but it doesn't work properly. I'm experience with VBA in Excel, but I've never used the LIKE operator before.

但它不能正常工作。我在 Excel 中使用过 VBA,但我以前从未使用过 LIKE 运算符。

回答by Alex K.

To look for a -only;

寻找-唯一;

"*-*"

Alternatively you could be stricter;

或者你可以更严格;

if x like "#*:##-#*:##[ap]m"

回答by chris neilsen

the Likepart of your code is fine. to prove it, run this code fragment:

Like你的代码的一部分是好的。为了证明这一点,请运行以下代码片段:

If "9:00-5:00pm" Like "*-*" Then
    MsgBox "True"
End If

the problem must lie elsewhere. post some details of how it doesn't work for you...

问题一定出在别处。发布一些关于它如何不适合你的细节......