1

vbscriptを使用してExcelのシートに設定操作を実装することは可能ですか?Aと言う-A'=A1

以下のように、私が行列Aを持っていると仮定します。

 PID  T1   T1Sdate  T1Assign  T2   T2Sdate  T2Assign   T3   T3Sdate  T3Assign   T4    T4Sdate  T4Assign

 10   A      10/11    Ram      B      2/5     Hari     X     03/03     Peter     L      04/09     Mikel

 20   A      10/11    Kajal    T      7/5     Lisa     X     03/03     Peter     L      04/07     Sila

 25   Y      10/11    Sila     T      7/5     Lisa     X     02/03     Peter     L      17/07     Mikel

上から私は以下の行列A'を得ました:

 PID  T1   T1Sdate  T1Assign  T2   T2Sdate  T2Assign   T3   T3Sdate  T3Assign   T4    T4Sdate  T4Assign

 10   A      10/11    Ram                              X     03/03     Peter     L      04/09     Mikel

 20   A      10/11    Kajal    T      7/5     Lisa    

 25                                                    X     02/03     Peter     L      17/07     Mikel

これで、A-A'を使用して出力行列を取得できますか?

 PID  T1   T1Sdate  T1Assign  T2   T2Sdate  T2Assign   T3   T3Sdate  T3Assign   T4    T4Sdate  T4Assign

 10                            B      2/5     Hari    

 20                                                   X     03/03     Peter     L      04/07        Sila

 25   Y      10/11    Sila     T      7/5     Lisa     
4

1 に答える 1

2

編集: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
于 2012-12-14T07:36:40.790 に答える