編集:VBAソリューションをVBSソリューションに変換
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
xlApp.EnableEvents = False
xlApp.ScreenUpdating = False
set xlBook = xlApp.Workbooks.Open("C:\Users\wangCL\Desktop\sample.xlsm")
Dim rangeA
Dim rangeB
Dim rangeC
Set rangeA = xlBook.Worksheets("Sheet1").Range("A1:E1")
Set rangeB = xlBook.Worksheets("Sheet2").Range("A1:E1")
Set rangeC = xlBook.Worksheets("Sheet3").Range("A1:E1")
Dim valueTypeA
Dim valueTypeB
If rangeA.Rows.Count <> rangeB.Rows.Count Or _
rangeA.Columns.Count <> rangeB.Columns.Count Or _
rangeA.Rows.Count <> rangeC.Rows.Count Or _
rangeA.Columns.Count <> rangeC.Columns.Count Then
MsgBox "Please input 3 ranges with identical size"
End If
rangeC.clearContents
For i = 1 To rangeA.Rows.Count
For j = 1 To rangeA.Columns.Count
valueTypeA = TypeName(rangeA.Cells(i, j).Value)
valueTypeB = TypeName(rangeB.Cells(i, j).Value)
If valueTypeA = valueTypeB Then
If valueTypeA = "Integer" Or valueTypeA = "Double" Or valueTypeA = "Long" Or valueTypeA = "Single" Then
rangeC.Cells(i, j).Value = rangeA.Cells(i, j).Value - rangeB.Cells(i, j).Value ' just numeric minus
ElseIf valueTypeA = "String" Then
' implement your own String comparsion function here
' I provide a simple one here
' matching the identical string
If rangeA.Cells(i, j).Value = rangeB.Cells(i, j).Value Then
rangeC.Cells(i, j).Value = ""
Else
'handle if the 2 string not match case here
rangeC.Cells(i, j).Value = rangeA.Cells(i, j).Value
End If
ElseIf valueTypeA = "Date" Then
' implement your own Date comparsion function here
' I provide a simple one here
' matching the identical date
If rangeA.Cells(i, j).Value = rangeB.Cells(i, j).Value Then
rangeC.Cells(i, j).Value = ""
Else
'handle if the 2 string not match case here
rangeC.Cells(i, j).Value = rangeA.Cells(i, j).Value
End If
End If
Else
' handle if the 2 types are different
End If
Next
Next
xlBook.save
xlApp.Quit
set xlBook = nothing
set xlApp = nothing
msgbox "Done"
VBAソリューション
' Assume the range are M*N matrix
Sub MinusOperation()
Dim rangeA As Range
Dim rangeB As Range
Dim rangeC As Range ' output range
Set rangeA = ThisWorkbook.Worksheets("Sheet1").Range("A1:E1")
Set rangeB = ThisWorkbook.Worksheets("Sheet2").Range("A1:E1")
Set rangeC = ThisWorkbook.Worksheets("Sheet3").Range("A1:E1")
Dim valueTypeA As String
Dim valueTypeB As String
If rangeA.Rows.Count <> rangeB.Rows.Count Or _
rangeA.Columns.Count <> rangeB.Columns.Count Or _
rangeA.Rows.Count <> rangeC.Rows.Count Or _
rangeA.Columns.Count <> rangeC.Columns.Count Then
MsgBox "Please input 3 ranges with identical size"
exit sub
End If
For i = 1 To rangeA.Rows.Count
For j = 1 To rangeA.Columns.Count
valueTypeA = TypeName(rangeA.Cells(i, j).Value)
valueTypeB = TypeName(rangeB.Cells(i, j).Value)
If valueTypeA = valueTypeB Then
If valueTypeA = "Integer" Or valueTypeA = "Double" Or valueTypeA = "Long" Or valueTypeA = "Single" Then
rangeC.Cells(i, j).Value = rangeA.Cells(i, j).Value - rangeB.Cells(i, j).Value ' just numeric minus
ElseIf valueTypeA = "String" Then
' implement your own String comparsion function here
' I provide a simple one here
' matching the identical string
If rangeA.Cells(i, j).Value = rangeB.Cells(i, j).Value Then
rangeC.Cells(i, j).Value = ""
Else
'handle if the 2 string not match case here
rangeC.Cells(i, j).Value = rangeA.Cells(i, j).Value
End If
ElseIf valueTypeA = "Date" Then
' implement your own Date comparsion function here
' I provide a simple one here
' matching the identical date
If rangeA.Cells(i, j).Value = rangeB.Cells(i, j).Value Then
rangeC.Cells(i, j).Value = ""
Else
'handle if the 2 string not match case here
rangeC.Cells(i, j).Value = rangeA.Cells(i, j).Value
End If
End If
Else
' handle if the 2 types are different
End If
Next j
Next i
End Sub