检查不同列的必填字段

时间:2019-07-25 07:11:07

标签: excel vba

我必须在book1中创建一个宏。我有3个excel文件(book1,book2和book3)。

Book3是模板,显示5个元素(ID,名称,生日,地址,电话)以及这些元素是否是必需的。

宏应打开book2以检查元素是否具有值。如果book2中的所有必需元素都包含一个值,则Book1中的单元格B1将使用值“ pass”指示完整的数据。如果不是,则“ Fail”将显示在同一单元格中。我该如何实现?

enter image description here

Sub check()

Dim ExternalWb1 As Workbook
Dim msg As String
Dim rng As Range
Set ExternalWb1 = Workbooks.Open(Filename:="C:\Users\A9905681\Desktop\XY's\Wk 10\Test_Mandatory\Book2.xlsx")
Dim ExternalSheet1 As Worksheet
Set ExternalSheet1 = ExternalWb1.Worksheets("Sheet1")
Set rng = ExternalSheet1.Range("B2:B5")

For Each cell In rng
    If Not IsEmpty(cell) Then
        Sheet1.Range("B1") = "Pass"
    Else
        Sheet1.Range("B1") = "Fail"
    End If
Next cell

ExternalWb1.Close
End Sub

目前,我只能检查book2中的特定范围,例如范围(“ B2:B5”)。

1 个答案:

答案 0 :(得分:0)

这应该可以帮助您

SELECT DISTINCT
       COL1, COL2, COL3
FROM   (
  SELECT key,
         value,
         ROW_NUMBER() OVER ( PARTITION BY key ORDER BY value ) AS rn
  FROM   table_name
  UNPIVOT ( value FOR name IN ( COL1, COL2, COL3 ) ) u
)
PIVOT ( MAX( value ) FOR rn IN (
  1 AS COL1,
  2 AS COL2,
  3 AS COL3
) )

代码已注释,但是如果您需要帮助,请随时询问。