在arr中表示“是”

时间:2019-07-29 03:10:06

标签: excel vba

第一个问题:我想将arr(i, 2) = "Yes"更改为"*" & "Yes" & "*",这意味着该单元格包含“是”,而并不完全是“是”。我该如何实现?

第二个问题:如果未填写必填字段,这两个文件wb1.Closewb.Close无法关闭(这意味着结果=失败)。只有填写了所有必填字段,才能关闭两个文件。

Excel Files

Sub check2()

'First lets store the mandatory items
Dim Mandatory As New Scripting.Dictionary 'You need Microsoft Scripting Runtime library under tools-->references
Dim arr As Variant
Dim i As Long
Dim wb1 As Workbook
Set wb1 = Workbooks.Open(Filename:="C:\Users\A9905681\Desktop\XY's\Wk 10\Test_Mandatory\Book3.xlsx", ReadOnly:=True, UpdateLinks:=False)
arr = wb1.Sheets("Sheet1").UsedRange.Value 'store the sheet inside the array
For i = 2 To UBound(arr) 'loop through the data and store the mandatory items
    If arr(i, 1) = vbNullString Then Exit For 'force an exit if the row has an empty value on column A
    If arr(i, 2) = "Yes" Then 'if the item is mandatory, store it
        Mandatory.Add arr(i, 1), 1
    End If
Next i
'lets check every item in the book2
'For this one to work properly you MUST not have empty rows, the UsedRange must be the same as rows you have
'When having data and then clearing it, the usedrange remains the same, you must delete the empty rows.
Dim j As Long
Dim Header As String
Dim wb As Workbook
Set wb = Workbooks.Open(Filename:="C:\Users\A9905681\Desktop\XY's\Wk 10\Test_Mandatory\Book2.xlsx", ReadOnly:=True, UpdateLinks:=False)
arr = wb.Sheets("Sheet1").UsedRange.Value
'Loop through columns and rows
For j = 1 To UBound(arr, 2) 'loop through columns
    Header = arr(1, j)
    If Mandatory.Exists(Header) Then 'will loop only through mandatory items
        For i = 2 To UBound(arr) 'loop through rows
            If arr(i, j) = vbNullString Then 'once we find one item not filled
                ThisWorkbook.Sheets("Sheet1").Cells(1, 2) = "Fail" 'fill the cell with fail
                Exit Sub 'end the procedure
            End If
        Next i
    End If
Next j
wb1.Close
wb.Close
ThisWorkbook.Sheets("Sheet1").Cells(1, 2) = "Pass" 'if nothing happens above, we put Pass to the cell
End Sub

0 个答案:

没有答案