1

是否可以使用 vbscript 在 Excel 表单上实现 Set 操作?说 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 回答