Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 根据格式对数据进行比较_Vba_Excel - Fatal编程技术网

Vba 根据格式对数据进行比较

Vba 根据格式对数据进行比较,vba,excel,Vba,Excel,我需要比较两个数据库的数据。因此,我将数据填充到excel表格中,并使用以下VBA代码进行比较 Sub Compare2WorkSheets(ws1 As Worksheet, ws2 As Worksheet) Dim ws1row As Long, ws2row As Long, ws1col As Integer, ws2col As Integer Dim maxrow As Long, maxcol As Integer, colval1 As String, colval

我需要比较两个数据库的数据。因此,我将数据填充到excel表格中,并使用以下VBA代码进行比较

Sub Compare2WorkSheets(ws1 As Worksheet, ws2 As Worksheet)

  Dim ws1row As Long, ws2row As Long, ws1col As Integer, ws2col As Integer 
  Dim maxrow As Long, maxcol As Integer, colval1 As String, colval2 As String 
  Dim report As Workbook, difference As Long 
  Dim row As Long, col As Integer 

  Set report = Workbooks.Add 

  With ws1.UsedRange 
    ws1row = .Rows.Count 
    ws1col = .Columns.Count 
  End With 

  With ws2.UsedRange 
    ws2row = .Rows.Count 
    ws2col = .Columns.Count
  End With

  maxrow = ws1row 
  maxcol = ws1col 
  If maxrow < ws2row Then maxrow = ws2row 
  If maxcol < ws2col Then maxcol = ws2col 

  difference = 0 

  For col = 1 To maxcol 
    For row = 1 To maxrow 
      colval1 = "" 
      colval2 = "" 
      colval1 = ws1.Cells(row, col).Formula 
      colval2 = ws2.Cells(row, col).Formula 
      If colval1 <> colval2 Then 
        difference = difference + 1 
        Cells(row, col).Formula = colval1 & "<> " & colval2 
        Cells(row, col).Interior.Color = 255 
        Cells(row, col).Font.ColorIndex = 2 
        Cells(row, col).Font.Bold = True 
      End If 
    Next row 
  Next col 

  Columns("A:B").ColumnWidth = 25 
  report.Saved = True 

  If difference = 0 Then 
    report.Close False 
  End If 
  Set report = Nothing 
  MsgBox difference & " cells contain different data! ", vbInformation, _
         "Comparing Two       Worksheets" 
End Sub
子比较2工作表(ws1作为工作表,ws2作为工作表)
将ws1row设置为Long,ws2row设置为Long,ws1col设置为Integer,ws2col设置为Integer
Dim maxrow为长,maxcol为整数,colval1为字符串,colval2为字符串
将报表设置为工作簿,差异设置为长
将行设置为长,列设置为整数
设置报告=工作簿。添加
使用ws1.UsedRange
ws1row=.Rows.Count
ws1col=.Columns.Count
以
使用ws2.UsedRange
ws2row=.Rows.Count
ws2col=.Columns.Count
以
maxrow=ws1row
maxcol=ws1col
如果maxrow

这里进行了单元级比较。现在,我需要根据数据类型进行比较。如果它是一个数字,那么我需要检查到两位小数。你能帮我合并这个要求吗

以下代码解决了我的问题@安迪,非常感谢。你的评论帮助我解决了问题

Sub Compare2WorkSheets(ws1 As Worksheet, ws2 As Worksheet)
Dim ws1row As Long, ws2row As Long, ws1col As Integer, ws2col As Integer
Dim maxrow As Long, maxcol As Integer, colval1 As String, colval2 As String
Dim report As Workbook, difference As Long
Dim row As Long, col As Integer
Dim lvalue As Boolean
Set report = Workbooks.Add
With ws1.UsedRange
ws1row = .Rows.Count
ws1col = .Columns.Count
End With
With ws2.UsedRange
ws2row = .Rows.Count
ws2col = .Columns.Count
End With
maxrow = ws1row
maxcol = ws1col
If maxrow < ws2row Then maxrow = ws2row
If maxcol < ws2col Then maxcol = ws2col
difference = 0
For col = 1 To maxcol
  For row = 1 To maxrow
  colval1 = ""
  colval2 = ""
  colval1 = ws1.Cells(row, col).Formula
  colval2 = ws2.Cells(row, col).Formula

  If IsNumeric(colval1) Or IsNumeric(colval2) Then

     If Round(Val(colval1), 2) <> Round(Val(colval2), 2) Then
        difference = difference + 1
        Cells(row, col).Formula = "DIFF " & colval1 & "<> " & colval2
        Cells(row, col).Interior.Color = 255
        Cells(row, col).Font.ColorIndex = 2
        Cells(row, col).Font.Bold = True
     End If

  Else
     If colval1 <> colval2 Then
        difference = difference + 1
        Cells(row, col).Formula = "DIFF " & colval1 & "<> " & colval2
        Cells(row, col).Interior.Color = 255
        Cells(row, col).Font.ColorIndex = 2
        Cells(row, col).Font.Bold = True
     End If
  End If
 Next row
 Next col
 Columns("A:B").ColumnWidth = 25
 report.Saved = True
 If difference = 0 Then
 report.Close False
 End If
 Set report = Nothing
 MsgBox difference & " cells contain different data! ", vbInformation, "Comparing Two    Worksheets"
 End Sub
子比较2工作表(ws1作为工作表,ws2作为工作表)
将ws1row设置为Long,ws2row设置为Long,ws1col设置为Integer,ws2col设置为Integer
Dim maxrow为长,maxcol为整数,colval1为字符串,colval2为字符串
将报表设置为工作簿,差异设置为长
将行设置为长,列设置为整数
作为布尔值的Dim左值
设置报告=工作簿。添加
使用ws1.UsedRange
ws1row=.Rows.Count
ws1col=.Columns.Count
以
使用ws2.UsedRange
ws2row=.Rows.Count
ws2col=.Columns.Count
以
maxrow=ws1row
maxcol=ws1col
如果maxrow
我不完全理解您的问题,但相信函数
VarType
可以满足您的需要。您可以尝试例如
IsNumeric(colval1)
并根据结果切换等价性测试。对于数字的2位小数精度,
Round(colval1,2)
可以完成这项工作。