VBA:如何識別在單元格值的開頭或結尾處有空格的單元格

[英]VBA: How to identify cells that have spaces at the beginning or end of the cell value


I have a dataset that is 80 columns by 200,000 rows. This data contains cell values that have beginning or trailing spaces. I need to identify which cells have those spaces.

我有一個80列乘200,000行的數據集。此數據包含具有開始或尾隨空格的單元格值。我需要確定哪些單元格具有這些空格。

If the dataset were smaller I could easily write a couple of for loops to check each cell for spaces. However, considering the size of the data, checking each cell would slow excel down (if not crash it). I anticipate that auto filter would end the same way.

如果數據集較小,我可以輕松編寫幾個for循環來檢查每個單元格的空格。但是,考慮到數據的大小,檢查每個單元格會減慢速度(如果沒有崩潰)。我預計自動過濾器會以同樣的方式結束。

Is there any way to efficiently check the dataset for spaces at the beginning or end of a cell value, without going cell by cell via loop

有沒有辦法有效地檢查數據集中單元格值開頭或結尾的空格,而不是逐個單元格循環

3 个解决方案

#1


1  

Private Sub this()
    Dim i As Long, j As Long
    Dim sh As Worksheet
    Dim sheetArr As Variant
    Set sh = ThisWorkbook.Sheets("Sheet1")
    sheetArr = sh.UsedRange
    rowC = sh.UsedRange.Rows.Count
    colC = sh.UsedRange.Columns.Count
    For i = 1 To rowC
        For j = 1 To colC
            If Left(sheetArr(i, j), 1) = " " Then
                sh.Cells(i, j).Interior.ColorIndex = 37
            End If
            If Right(sheetArr(i, j), 1) = " " Then
                sh.Cells(i, j).Interior.ColorIndex = 37
            End If
        Next j
    Next i
End Sub

I tested this on a 1 million row sheet and was done within 5 seconds. This should be good enough.

我在100萬行表上測試了這個,並在5秒內完成。這應該足夠好了。

enter image description here

#2


2  

1) Select all cells you want to use

1)選擇要使用的所有單元格

2) Click on Home -> Condition formatting button -> New Rule

2)單擊主頁 - >條件格式按鈕 - >新規則

3) Scroll down to "Use a formula to determine which cells to format"

3)向下滾動到“使用公式確定要格式化的單元格”

4) Assuming your current cell is A1, type in the followiing

4)假設您當前的單元格為A1,請鍵入以下內容

=OR((RIGHT(A1,1)=" "),(LEFT(A1,1)=" "))

5) Press format button, click on "Fill" an choose a color for fill

5)按格式按鈕,點擊“填充”,選擇一種顏色進行填充

6) Press "ok", "ok".

6)按“確定”,“確定”。

The cells should be highlighted now

現在應該突出顯示單元格

#3


0  

A loop might be slow if you query the workbook cell by cell. Instead, a loop querying a variant array will be much much faster:

如果逐個單元查詢工作簿,則循環可能會很慢。相反,查詢變量數組的循環會快得多:

Dim i as long
Dim j as long
Dim a() as Variant
a = yourrange.value
for i = lbound(a,1) to ubound(a,1)
  for j = lbound(a,2) to ubound(a,2)
    if left(a(i,j),1) = " " or right(a(i,j),1) = " " then
     'beginning or ending with " "
    end if
  next j
next i

Another option would be to copy/paste the data to notepad++ for example and then use the extended search function with "\t " and " \t" as well as "\r\n " and " \r\n" in order to find " " next to tabs and breaks which separat columns and rows respectively

另一個選擇是將數據復制/粘貼到記事本++,然后使用擴展搜索功能“\ t”和“\ t”以及“\ r \ n”和“\ r \ n”以便在標簽和分隔符旁邊找到“”,分別分隔列和行


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/05/18/72f3a8172b7f8f3c72537164e5b5d5b7.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com