我很抱歉,如果这张贴在其他地方,但我已经搜索了很多,显然类型不匹配是一个常见的错误,与任何东西一起出现。 我正在使用Vba来清理Csv文件的副本。这是我的代码:
Option Explicit
Sub CleanCopy()
Dim rCell As Range
Dim rMerge As Range
Dim Rng As Range
Dim dCell As Range
Dim iRow As Long
Dim i As Long
Dim ws As Worksheet
Set ws = Worksheets("Reservas")
iRow = ws.Cells.find(What:="*", _
SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, _
LookIn:=xlValues).Row + 1
If ws.Range("A1") = "Status" Then
If rMerge Is Nothing Then
Set rMerge = ws.Range("A1").MergeArea.EntireColumn
End If
Else
MsgBox ("Copiou de forma errada, tente novamente")
Exit Sub
End If
rMerge.Delete
Set rCell = Nothing
Set rMerge = Nothing
If ws.Range("D1") = "Adults / " Then
ws.Range("D1") = Left(ws.Range("D1"), 6)
Else
MsgBox ("Copiou de forma errada, tente novamente")
Exit Sub
End If
If ws.Range("H1") <> "Total" Then
MsgBox ("Copiou de forma errada, tente novamente")
Exit Sub
End If
For Each rCell In ws.UsedRange
If rCell.Row > 3 Then
If rCell.MergeCells Then
If rMerge Is Nothing Then
Set rMerge = rCell.MergeArea.EntireRow
Else
Set rMerge = Union(rMerge, rCell.MergeArea.EntireRow)
End If
End If
End If
Next
rMerge.Delete
Set rCell = Nothing
Set rMerge = Nothing
Range("D2:D3").Select
Selection.ClearContents
Range("D1:D3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.merge
Set Rng = ws.Range("D4:D700")
Rng.Replace What:="~/", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
For Each dCell In Rng
dCell.Value = Left(dCell, 1)
Next
i = 4
With ws
While i < iRow
.Cells(i, "H") = .Cells(i, "H") - .Cells(i, "D")
i = i + 1
Wend
End With
End Sub
所以当我尝试重新计算H列时,我最终得到类型不匹配:
.Cells(i, "H") = .Cells(i, "H")) - (.Cells(i, "D")
我尝试将CDbl放在它们上面,当我执行“左”代码但这也不起作用。我知道我在忽略某些东西,所以如果你能告诉我它是什么我会很感激。提前谢谢!
答案 0 :(得分:0)
您正尝试从范围中减去范围。类型不匹配意味着您正在尝试使用错误类型的变量进行操作。
尝试类似:
.Cells(i, "H").Value = .Cells(i, "H").Value - .Cells(i, "D").Value
答案 1 :(得分:0)
列H和D中的值可能不是数字吗?你可能试图从数字中减去文字,这是没有意义的。
编辑:必须是D列中的数据不是数字。
Set Rng = ws.Range("D4:D700")
Rng.Replace What:="~/", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
For Each dCell In Rng
dCell.Value = Left(dCell, 1)
Next
这是您需要查看的代码部分。我怀疑它不会返回单元格D中的所有数字字符。
答案 2 :(得分:0)
所以伙计们,我要感谢你们所有人。不幸的是,我还没有能够提供“有用的评论”的事情,或者我会因为你们都非常有帮助。 对我来说最有帮助的是isnumeric位,它帮助我发现整个h列显示为文本,即使该列中只有数字。如果你想知道,最后这是我的代码看起来像什么,我放了一些额外的变量,以防万一,以减少出来的双倍:
With ws
While .Cells(i, "A") <> ""
TT = CDbl(.Cells(i, "D"))
'This is just in case there is any issues with decimal parts
sVal = Replace(.Cells(i, "H"), ".", ",")
If Not sVal = "" Then
If IsNumeric(sVal) = True Then
Total = CDbl(sVal)
End If
End If
.Cells(i, "H").Value = Total - TT
i = i + 1
Wend