简体   繁体   English

从“-”左侧提取文本,但忽略不包含“-”的单元格

[英]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.

相关问题 InStr 提取“[”之前的所有文本,如果某些单元格包含“[”而某些单元格不包含 - InStr to extract all text before “[” if some cells contain “[” and some do not Excel VBA高亮显示范围内不包含单词列表中文本的单元格 - Excel VBA Highlight Cells in Range that Do Not Contain Text from a List of Words *EXCEL* 如果一系列单元格包含 *STRING*,则从正单元格中提取十进制数字的总和 - *EXCEL* If a range of cells contain *STRING* then extract the sum of decimal numbers from positive cells 如何计算包含某个列表中的文本的单元格 - How to count cells that contain text from a certain list Excel-查看一列中的单元格是否包含来自另一列的文本 - Excel - See if cells in one column contain text from another Excel VBA:如果单元格不包含表中的值,则删除行 - Excel VBA: delete rows if cells do not contain values from a table Excel,对单元格求和,但忽略文本。 从另一个工作表引用单元格并将其连接起来 - Excel, sum cells but ignore text. Cells are referenced from another sheet and concatenated 从Excel中的字符串模式后的单元格中提取文本 - extract text from cells after string pattern in Excel Excel:从单元格中提取由标记分隔的文本字符串的公式 - Excel: Formula to extract a string of text delimited by markers from cells 从包含逗号分隔文本的单元格中提取唯一字符 - Extract unique characters from cells containing comma separated text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM