我在验证空白字段时遇到了一点困难。
当我使用此代码打开文件时,它会打开文件,检查该列中的应用程序编号(在我的应用程序编号位于第一列中)
我想要做的是,如果没有申请号,那么它应该写出以下错误“在以下行号找到空白申请号”
'Global Variables
Dim rErr As Integer
'
' Find the last used row in a Column: column A in this example
'
Function LastRowInOneColumn(ColNo As String) As Long
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, ColNo).End(xlUp).Row
End With
LastRowInOneColumn = LastRow
End Function
'
' Find the last used column in a Row: row 1 in this example
'
Function LastColumnInOneRow(RowNo As String)
Dim LastCol As Integer
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
LastColumnInOneRow = LastCol
'MsgBox LastCol
End Function
'
' To Check Application Number
'
Function Check_AppNo(appNo, pRow, Lrow) As Boolean
Check_AppNo = True
Dim MinAppNo, MaxAppNo As Single
MinAppNo = 0
MaxAppNo = 9999999999#
If (appNo < MinAppNo Or appNo > MaxAppNo) Then
Worksheets("Error_Results").Cells(rErr, 1) = "Application number out of range at Row " & i
rErr = rErr + 1
Check_AppNo = False
End If
For j = pRow + 1 To Lrow
If (appNo = Worksheets("Sheet1").Cells(j, 1)) Then
Worksheets("Error_Results").Cells(rErr, 1) = "Duplicate Application numbers at Rows " & pRow & " and " & j
rErr = rErr + 1
Check_AppNo = False
End If
Next j
End Function
Function OpenFile() As String
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Please select a file")
If NewFN = False Then
' They pressed Cancel
OpenFile = ""
'MsgBox "Stopping because you did not select a file"
Exit Function
Else
Workbooks.Open Filename:=NewFN
iPos = InStr(1, NewFN, "\") + 1
ipos1 = 0
Do
ipos1 = InStr(iPos, NewFN, "\") + 1
If (ipos1 <> 1) Then
iPos = ipos1
End If
Loop Until (ipos1 = 1)
OpenFile = Mid(NewFN, iPos, Len(NewFN) - iPos + 1)
End If
End Function
Sub AddWorkSheet(fName As String, sName As String)
Dim wSheet As Worksheet
Workbooks(fName).Activate
On Error Resume Next
Set wSheet = Worksheets(sName)
If wSheet Is Nothing Then
Worksheets.Add().Name = sName
Else
Worksheets(sName).Clear
End If
On Error GoTo 0
End Sub
Sub validate()
Dim fName As String
Dim aName As String
Dim flag As Variant
fName = OpenFile() ' Open the required data file
If (fName = "") Then
Exit Sub
End If
Call AddWorkSheet(fName, "Error_Results") ' Add Error Worksheet to the data Excel File
rErr = 1
Worksheets("Sheet1").Select
LastRow = LastRowInOneColumn("A") ' Get The Last Row in Column
For pRow = 2 To LastRow
rerr1 = rErr
appNo = Worksheets("Sheet1").Cells(pRow, 1)
flag = Check_AppNo(appNo, pRow, LastRow)
Next pRow 'Process the next Record in Error_Results WorkSheet
Workbooks(fName).Close (True) ' Closes an opened workbook on which the validation was done
End Sub
Sub Button1_Click()
Call validate
End Sub
请按照以下步骤运行代码:
- 第1步:首先制作一个名为“abc1”的excel文件
- 第 2 步:在该文件的第 1 列中,将其标题设为“申请号”
- 第 3 步:现在在其中输入申请编号(您想要的任何编号),中间留一个空白单元格
- 第 4 步:让另一个 excel 文件显示“验证器”
- 第 5 步:在那个地方的开发人员选项卡中的一个按钮
- 第 6 步:在开发人员选项卡中,单击 Visual Basic
- 第 7 步:您将看到一个 Visual Basic 编辑器
- 第8步:在左侧,您将看到一个项目浏览器窗口,在该窗口中右键单击粗体名称>选择插入>模块
- 第9步:然后按原样复制并粘贴上述代码
- 第 10 步:保存它并将 excel 文件另存为启用宏的文件
- 第 11 步:现在打开文件“验证器”,然后单击按钮运行代码
你会明白我想说什么,如果你看到代码,很容易理解
希望,任何人都可以帮助我