[英]Range.Value = “” type mismatch
I am trying to write some code to format a series of worksheets into a standardized format. 我正在尝试编写一些代码,以将一系列工作表格式化为标准化格式。 To do this, I want to remove blank rows, delete leading spaces, and remove ";*" that start a lot of lines. 为此,我要删除空白行,删除前导空格,并删除以很多行开头的“; *”。 Upon trying to see if aRow.Value = "", meaning there is no text in the row, a type mismatch error is always returned. 尝试查看aRow.Value =“”时,意味着该行中没有文本,则始终返回类型不匹配错误。
I have tried many different solutions from the internet, but none seem to work. 我尝试了许多来自Internet的解决方案,但似乎都没有用。
Sub Format()
Dim ws As Worksheet
Dim aRow As Range
Dim r As Integer
Dim cel As String
For Each ws In ThisWorkbook.Worksheets
For Each aRow In ws.UsedRange.Rows
If ws.Name = "Sheet1" Then
Exit For
End If
If aRow.Value = "" Then
aRow.Delete
End If
If aRow.Value = " " Then
aRow.Delete
End If
If Left(aRow.Value2, 2) = ";*" Then
aRow.Value = Right(aRow.Value2, Len(aRow.Value2) - 2)
Else
aRow.Delete
End If
cel = "A" & aRow.Row
aRow.Value = Application.WorksheetFunction.Trim(Range(cel).Value)
If aRow.Value = Empty Then
aRow.Delete
End If
Next
Next
End Sub
Row represents an entire row in Excel and not a cell. 行表示Excel中的整个行,而不是单元格。 You can not assigning a string value ("") to a row. 您不能将字符串值(“”)分配给行。
In your case, you want to check if the first cell equals to some value. 在您的情况下,您要检查第一个单元格是否等于某个值。 Instead of checking aRow.Value=
use aRow.Cells(1).Value
. 而不是检查aRow.Value=
使用aRow.Cells(1).Value
。
Once you detected the row to delete, use aRow.Clear()
to clear all row content. 一旦检测到要删除的行,请使用aRow.Clear()
清除所有行内容。
Welcome to SO. 欢迎来到SO。 I think you could replace line 我想你可以代替线
For Each aRow In ws.UsedRange.Rows
by this: 这样:
For Each aRow In ws.UsedRange.Columns(1).Cells
That way, you will loop trough each cell of first column. 这样,您将遍历第一列的每个单元格。
To delete an entire row, then you could use aRow.EntireRow.Delete
要删除整行,则可以使用aRow.EntireRow.Delete
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.