Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'MsgBox "开始检测数据..."
'数据不合法就不允许保存
Dim isCancel As Boolean
'定义已填写的数据行数
Dim length As Integer
'存放各列行数,选择最大的值
Dim cellArray As Variant
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Dim g As Integer
Dim h As Integer
Dim i As Integer
Dim j As Integer
a = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row
b = ThisWorkbook.Sheets(1).Range("b65536").End(xlUp).Row
c = ThisWorkbook.Sheets(1).Range("c65536").End(xlUp).Row
d = ThisWorkbook.Sheets(1).Range("d65536").End(xlUp).Row
e = ThisWorkbook.Sheets(1).Range("e65536").End(xlUp).Row
f = ThisWorkbook.Sheets(1).Range("f65536").End(xlUp).Row
g = ThisWorkbook.Sheets(1).Range("g65536").End(xlUp).Row
h = ThisWorkbook.Sheets(1).Range("h65536").End(xlUp).Row
i = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
cellArray = Array(a, b, c, d, e, f, g, h, i)
length = Excel.Application.WorksheetFunction.Max(cellArray)

'校验每行数据是否合法
For i = 2 To length
'前5列不能为空
If Worksheets("Sheet1").Range("A" & i).Value = "" Then
Worksheets("Sheet1").Range("A" & i).Select
MsgBox "第" & i & "行【网点全称】列:内容不允许为空"
isCancel = True
Exit For
End If

If Worksheets("Sheet1").Range("B" & i).Value = "" Then
Worksheets("Sheet1").Range("B" & i).Select
MsgBox "第" & i & "行【网点简称】列:内容不允许为空"
isCancel = True
Exit For
End If

If Worksheets("Sheet1").Range("C" & i).Value = "" Then
Worksheets("Sheet1").Range("C" & i).Select
MsgBox "第" & i & "行【网点类型】列:内容不允许为空"
isCancel = True
Exit For
End If

If Worksheets("Sheet1").Range("D" & i).Value = "" Then
Worksheets("Sheet1").Range("D" & i).Select
MsgBox "第" & i & "行【上级机构代码】列:内容不允许为空"
isCancel = True
Exit For
End If

'检测上级机构类型长度是否为15位
If Len(Worksheets("Sheet1").Range("D" & i).Value) <> 15 Then
Worksheets("Sheet1").Range("D" & i).Select
MsgBox "第" & i & "行【上级机构代码】列:长度必须为15"
isCancel = True
Exit For
End If

If Worksheets("Sheet1").Range("E" & i).Value = "" Then
Worksheets("Sheet1").Range("E" & i).Select
MsgBox "第" & i & "行【网点状态】列:内容不允许为空"
isCancel = True
Exit For
End If

Next

'数据不合法就不允许保存
If isCancel Then
Cancel = True
Else
Cancel = False
End If

End Sub

--------------------------

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'MsgBox "开始检测数据..."
'数据不合法就不允许保存
Dim isCancel As Boolean
'定义已填写的数据行数
Dim length As Integer
'存放各列行数,选择最大的值
Dim cellArray As Variant
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Dim g As Integer
Dim h As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim m As Integer
Dim n As Integer
Dim o As Integer
Dim p As Integer
Dim q As Integer
Dim r As Integer
Dim s As Integer
Dim t As Integer

a = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row
b = ThisWorkbook.Sheets(1).Range("b65536").End(xlUp).Row
c = ThisWorkbook.Sheets(1).Range("c65536").End(xlUp).Row
d = ThisWorkbook.Sheets(1).Range("d65536").End(xlUp).Row
e = ThisWorkbook.Sheets(1).Range("e65536").End(xlUp).Row
f = ThisWorkbook.Sheets(1).Range("f65536").End(xlUp).Row
g = ThisWorkbook.Sheets(1).Range("g65536").End(xlUp).Row
h = ThisWorkbook.Sheets(1).Range("h65536").End(xlUp).Row
i = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
j = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
k = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
l = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
m = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
n = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
o = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
p = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
q = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
r = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
s = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row
t = ThisWorkbook.Sheets(1).Range("i65536").End(xlUp).Row

cellArray = Array(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)
length = Excel.Application.WorksheetFunction.Max(cellArray)

'校验每行数据是否合法
For i = 2 To length
'ABCDG项不能为空
If Worksheets("Sheet1").Range("A" & i).Value = "" Then
Worksheets("Sheet1").Range("A" & i).Select
MsgBox "第" & i & "行【商户代码】列:内容不允许为空"
isCancel = True
Exit For
End If

'商户代码必须是1-99的数字
If Not bTest(Worksheets("Sheet1").Range("A" & i).Value, "^[0-9]*[1-9][0-9]*$") Then
Worksheets("Sheet1").Range("A" & i).Select
MsgBox "第" & i & "行【商户代码】列:必须是1-99的数字"
isCancel = True
Exit For
Else
If Worksheets("Sheet1").Range("A" & i).Value < 1 Or Worksheets("Sheet1").Range("A" & i).Value > 99 Then
Worksheets("Sheet1").Range("A" & i).Select
MsgBox "第" & i & "行【商户代码】列:必须是1-99的数字"
isCancel = True
Exit For
End If
End If

If Worksheets("Sheet1").Range("B" & i).Value = "" Then
Worksheets("Sheet1").Range("B" & i).Select
MsgBox "第" & i & "行【商户全称】列:内容不允许为空"
isCancel = True
Exit For
End If

If Worksheets("Sheet1").Range("C" & i).Value = "" Then
Worksheets("Sheet1").Range("C" & i).Select
MsgBox "第" & i & "行【商户简称】列:内容不允许为空"
isCancel = True
Exit For
End If

If Worksheets("Sheet1").Range("D" & i).Value = "" Then
Worksheets("Sheet1").Range("D" & i).Select
MsgBox "第" & i & "行【商户类型】列:内容不允许为空"
isCancel = True
Exit For
End If

If Worksheets("Sheet1").Range("G" & i).Value = "" Then
Worksheets("Sheet1").Range("G" & i).Select
MsgBox "第" & i & "行【商户状态】列:内容不允许为空"
isCancel = True
Exit For
End If

'EF两项不能同时为空
If Worksheets("Sheet1").Range("E" & i).Value = "" And Worksheets("Sheet1").Range("F" & i).Value = "" Then
Worksheets("Sheet1").Range("E" & i).Select
MsgBox "第" & i & "行【单用途卡上级单位】列和【多用途卡上级单位】:不能同时为空"
isCancel = True
Exit For
End If

'最大退货次数只能为0或正整数
If Worksheets("Sheet1").Range("H" & i).Value <> "" And (Not bTest(Worksheets("Sheet1").Range("H" & i).Value, "^(0|[1-9]\d*)$")) Then
Worksheets("Sheet1").Range("H" & i).Select
MsgBox "第" & i & "行【最大退货次数】列:只能为0或正整数"
isCancel = True
Exit For
End If

'校验邮箱
If Worksheets("Sheet1").Range("T" & i).Value <> "" And (Not bTest(Worksheets("Sheet1").Range("T" & i).Value, "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")) Then
Worksheets("Sheet1").Range("T" & i).Select
MsgBox "第" & i & "行【财务联系人Email】列:邮箱不合法"
isCancel = True
Exit For
End If

Next

'数据不合法就不允许保存
If isCancel Then
Cancel = True
Else
Cancel = False
End If

End Sub

'正则表达式
Function bTest(ByVal s As String, ByVal p As String) As Boolean
Dim re As RegExp
Set re = New RegExp
re.IgnoreCase = False '设置是否匹配大小写
re.Pattern = p
bTest = re.Test(s)
End Function

EXCEL 保存之前校验的更多相关文章

  1. excel保存为制表符分隔的文本文件 js无法完整读取

    excel保存为制表符分隔的文本文件 js无法完整读取 excel另存为文本有两个选项,一个是制表符分隔的文本文件,一个是unicode文本.生成的文件Unicode更大一些.但是这里需要注意的是[制 ...

  2. EXCEL保存提示“隐私问题警告:此文档中包含宏……”解决办法

    先点击“禁止宏运行”的那个按钮.打开文件后,按alt + F11 进入宏编辑器,在“工程”里查看是什么宏.如果是你需要的,就留着.否则右击这个宏名称,选择“移除”. 另外,如果是你需要的,还需要在 工 ...

  3. excel保存时出现“请注意,您的文档的部分内容可能包含了文档检查器无法删除的个人信息”

    这个问题的原因是由于工作簿包含宏.ActiveX控件等内容, 而Excel被设置为在保存文件时自动删除文件属性中的个人信息,因而出现该对话框.如果要避免出现这个提示,可进行如下设置: Excel 20 ...

  4. .Net core 使用NPOI 直接导入Excel到数据库(即不先将Excel保存到服务器再读取文件到数据库)

    /// <summary> /// 导入信息 /// </summary> /// <param name="file"></param& ...

  5. 利用freemarker+SAX解析xml的方式对excel文件字段校验

    利用freemarker对参数进行校验这篇文章主要用到的技术点: 自定义注解的使用反射机制SAX解析xmlFreemarker的运用我们在工作中经常需要上传excel文件,然后在对文件中的字段进行校验 ...

  6. C#中对EXCEL保存的SAVEAS方法说明

    这两天做的导出报表的项目中,因为出现了一些问题所以对于excel一些方法参数有了一些认识, 首先:开始生成的是.xls格式的excel文件,但是某个sheet发现我本来dataTable的数据有8万多 ...

  7. 【JavaWeb】导入Excel并进行校验

    一.需要实现的目标 1.界面编写 2.导入表读取表名,进行校验,后台匹配(判断此表的名称是否能够模糊匹配上) 3.确定表存在,读取其中的数据,暂存 4.正则表达式数据校验(判断是否已存在,数据是否符合 ...

  8. C#生成Excel保存到服务器端并下载

    using MongoDB.Bson; using Newtonsoft.Json.Linq; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; ...

  9. vba比较日期大小,定义日期;vba让excel保存

    Private Sub CommandButton1_Click()Dim i, j As IntegerDim a As Datea = #10/1/2013#j = 2i = 2' If  Wor ...

随机推荐

  1. Xcode升级更新后,恢复cocoapods以及插件的方法

    今天将手机系统更新到iOS9.3了,在Xcode7.1上做真机调试,提示找不到适合的SDK,才知道必须要升级Xcode才行,于是升级Xcode到7.3. 升级之后遇到很多麻烦,cocoapods没有了 ...

  2. 【洛谷 P1352】没有上司的舞会

    树形dp #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; ...

  3. javaBean与Map<String,Object>互转

    背景:有时候想不通阿帕奇的BeanUtils是怎么进行map和Bean互相转化的. 工作闲暇之余,自己写个一小段代码,一探究竟,试试才发现,原来一切并非我们想的那么什么和复杂. 注:这里只是简单实例, ...

  4. oprofile 安装使用

    ubuntu上要编译安装oprofile.现在版本(0.9.9)的oprofile的编译需要binutil-dev libpopt-dev apt-get install binutil-dev li ...

  5. Hadoop-1.2.1 安装步骤小结(ubuntu)

    1.安装ubuntu系统 如果不使用云服务器,可以使用虚拟机WmWare安装,具体安装步骤这里就不讲了,ubuntu系统下载地址:http://www.ubuntu.com/download/desk ...

  6. Http Status 参考

    http://tool.oschina.net/commons?type=5 状态码 含义 100 客户端应当继续发送请求.这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝.客户 ...

  7. 解读SQL Server 2014可更新列存储索引——存储机制

    概述 SQL Server 2014被号称是微软数据库的一个革命性版本,其性能的提升的幅度是有史以来之最. 可更新的列存储索引作为SQL Server 2014的一个关键功能之一,在提升数据库的查询性 ...

  8. MVP

    引自: http://www.cnblogs.com/Leo_wl/archive/2013/05/03/3056299.html http://www.codeproject.com/Article ...

  9. Android Message里传送的数据[转]

    package org.hualang.handlertest; import android.app.Activity; import android.os.Bundle; import andro ...

  10. memcache(一)概述

    概述 memcache是一种支持分布式的缓存系统,基于网络连接(当然它也可以使用localhost)方式完成服务,本身它是一个独立于应用的程序或守护进程(Daemon方式). 本地缓存 memcach ...