[英]Extract text from the left of “-” but ignore cells that do not contain “-”
I am trying to extract some values from a cell. 我正在尝试从单元格中提取一些值。
I have figured out how to extract the value of a cell to the left of "-" but I come across an error when the cell does not have a "-" at all. 我已经弄清楚了如何提取“-”左侧的单元格值,但是当该单元格根本没有“-”时遇到了一个错误。 How do I ignore these cells / error?. 如何忽略这些单元格/错误?
Any help would be much appreciated or ways to improve what I have made. 任何帮助将不胜感激或改善我所做的方法。
Also if it helps there are roughly 20000 rows on average. 同样,如果有帮助,平均大约有20000行。
Dim r As Long
Dim Location As Long
Dim m As Long
Dim ws As Worksheet
Set ws = Worksheets("NHBRSummary")
Set ws = Worksheets("Sheet1")
m = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
For r = 2 To m
Location = InStr(1, Cells(r, 1), "-")
Cells(r, 2).Value = Left(Cells(r, 1), Location - 1)
Next
End Sub
Set ws = Worksheets("Sheet1")
with ws
m = .Cells(.Rows.Count, "F").End(xlUp).Row
For r = 2 To m
if InStr(1, .Cells(r, 1), "-") > 0 then
.Cells(r, 2).Value = split(.Cells(r, 1).value)(0)
end if
Next
end with
You set ws to a sheet, then immediately change it, you should remove the first set if you aren't using it. 将ws设置为工作表,然后立即对其进行更改,如果不使用它,则应删除第一组。
You can still use left() and location if you would like, this is just another way of doing it. 如果愿意,您仍然可以使用left()和location,这只是另一种方式。 The important part is the if statement using instr() 重要的部分是使用instr()的if语句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.