首页 > 其他分享 >VBA脚本

VBA脚本

时间:2023-11-29 23:06:40浏览次数:35  
标签:脚本 VBA End Sub Column St1 Rng1 Col

Private Const St1Name = "下拉菜单源"

Private Const Rng1Address = "B6:B25"


Private Sub Worksheet_Change(ByVal Target As Range)

'多级下拉菜单触发宏

'编制人:王备(千顷云)

'本代码免费提供学习使用,但请勿用于商业售卖和课程使用。

'网站转载请注明来源。


'当改变目标单元格数量大于1时不触发

'当单元格被删除时不触发

On Error Resume Next

If Target.Count > 1 Then Exit Sub

If Err.Number <> 0 Then

   Err.Clear

   On Error GoTo 0

   Exit Sub

End If


'定义常用变量

Dim St1 As Object, Col As Integer, Rng1 As Object, RngX As Object, Rng2 As Object

Set St1 = Sheets(St1Name)

Col = St1.[a1].End(xlToRight).Column

Set Rng1 = Range(Rng1Address)


'范围外单元格不触发

If Target.Row > Rng1(Rng1.Count).Row Or Target.Row < Rng1(1).Row Then Exit Sub

If Target.Column < Rng1.Column Or Target.Column > Rng1.Column + Col - 2 Then Exit Sub


Application.EnableEvents = False

'清空右边内容

Set Rng2 = Range(Cells(Target.Row, Target.Column + 1), Cells(Target.Row, Rng1.Column + Col - 1))

Rng2.Value = ""

Rng2.Validation.Delete


'新增右边单元格下拉菜单

Set Cel = Target.Offset(0, 1)

Cel.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _

xlBetween, Formula1:=Str1(Cel)

If Target.Value <> "" Then Cel.Select


Application.EnableEvents = True

End Sub




Sub A_新建下拉菜单表()

'使用选中目标表格的命令,看是否产生错误值来判断工作表是否已经存在

'如果选择表格命令出现错误,表示不存在该表,就新建一个

On Error Resume Next

Sheets(St1Name).Select

If Err.Number <> 0 Then

   Sheets.Add

   ActiveSheet.Name = St1Name

End If

Sheets(St1Name).Cells(1, 1).Select

Err.Clear

On Error GoTo 0

End Sub


Sub B_下拉菜单表删除重复项()

'定义常用变量

Dim St1 As Object, Col As Integer, arr()

Set St1 = Sheets(St1Name)

Col = St1.[a1].End(xlToRight).Column


ReDim arr(0 To Col - 1)

For i = 0 To Col - 1

   arr(i) = i + 1

Next

'利用Excel自带的删除重复项功能

St1.Cells.RemoveDuplicates Columns:=(arr), Header:=xlYes

End Sub


Sub C_初始化下拉列表区域()


'定义常用变量

Dim St1 As Object, Col As Integer, Rng1 As Object, RngX As Object

Set St1 = Sheets(St1Name)

Col = St1.[a1].End(xlToRight).Column

Set Rng1 = Range(Rng1Address)

Set RngX = Rng1.Resize(Rng1.Rows.Count, Col)


Application.EnableEvents = False

RngX.Value = ""


Rng1.Validation.Delete

Rng1.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _

   xlBetween, Formula1:=Str1(Rng1(1))


With RngX.Interior

   .Pattern = xlSolid

   .PatternColorIndex = xlAutomatic

   .ThemeColor = xlThemeColorAccent6

   .TintAndShade = 0.799981688894314

   .PatternTintAndShade = 0

End With

Rng1(1).Select

Application.EnableEvents = True

End Sub


Function ValFor(Cel)

'判断单元格是否有数据验证

On Error Resume Next

a = Cel.Validation.Formula1

b = False

If Err.Number = 0 Then b = True

Err.Clear

On Error GoTo 0

ValFor = b

End Function


Function Str1(Cel)

'获取下拉列表


'常用变量

Dim St1, Col

Set St1 = Sheets(St1Name)

Col = St1.[a1].End(xlToRight).Column


'获取当前行内容数组

Dim Col2, Ind, StrArr(), BooArr()

Col2 = Range(Rng1Address).Column

Ind = Cel.Column - Col2 + 1

StrArr() = Application.Transpose(Range(Cells(Cel.Row, Col2), Cells(Cel.Row, Col2 + Col - 1)).Value)


'创建字典并赋值

Dim d As Object

Set d = CreateObject("Scripting.Dictionary")

row1 = St1.[a1].End(xlDown).Row

Dim arr()

arr = St1.Range(St1.Cells(1, 1), St1.Cells(row1, Col)).Value

For i = 2 To row1

   '判断是否满足条件

   boo = 0

   For m = 1 To UBound(StrArr)

   If m < Ind And StrArr(m, 1) <> Replace(arr(i, m), ",", "_") Then boo = boo + 1

   Next

   

   '满足条件的才被计入字典

   If St1.Cells(i, Ind).Value <> "" And boo = 0 Then

       d(Replace(St1.Cells(i, Ind).Value, ",", "_")) = ""

   End If

Next

arr = d.Keys

Str1 = Join(arr, ",")


End Function



标签:脚本,VBA,End,Sub,Column,St1,Rng1,Col
From: https://blog.51cto.com/u_16395461/8620455

相关文章

  • centos 实现开机运行脚本
    Craftedby[Genie](https://marketplace.visualstudio.com/items?itemName=genieai.chatgpt-vscode)Youcentos实现开机运行脚本Genie在CentOS系统中,如果您希望在开机时自动运行一个脚本,可以采用以下几种方法之一来实现:方法1:使用cron的@reboot打开当前用户的crontab编......
  • shell脚本5---信号处理
    信号的类别信号值描述1SIGHUP挂起进程2SIGINT终止进程3SIGQUIT停止进程9SIGKILL无条件终止进程15SIGTERM优雅的终止进程17SIGSTOP无条件停止进程,但不是终止进程18SIGTSTP停止或暂停进程,但不是终止进程19SIGCONT继续运行停止的进......
  • 油猴脚本屏蔽元素
    //==UserScript==//@nameBlockZhihuTag//@namespaceyour-namespace//@version1.0//@descriptionBlockaspecifictagonZhihuwebsite//@matchhttps://www.zhihu.com/*//@grantnone//==/UserScript==(function(){......
  • 【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-Az
    问题描述AzureWebJob执行Powershell脚本报错 Select-AzContext:Theterm'Select-AzContext'isnotrecognizedasthenameofacmdlet,function,scriptfile,oroperableprogram.Checkthespellingofthename,orifapathwasincluded,verifythatthepa......
  • secureCRT脚本登录迈普交换机报错
    参考:http://www.lingchenliang.com/post/1799.htmlhttps://blog.csdn.net/qq_25294171/article/details/8515845832位的windowd7中在CRT6.2里手动输入IP能正常ssh登迈普交换机路由器,当使用以前的配置备份脚本如下登录就报错(注:params(3)密码(2)用户名(1)IP) cmd="/SSH2/L"&params(......
  • 使用cursor编写python脚本
    环境python3.7pycharmcursor需要安装pandas库python基本常用语法注释#后面写的是注释#单行注释变量python是一种弱类型的语言,一个变量得到类型可以变化变量名区分大小写#创建数据n1=1#整数n2=1.2#浮点数#字符串是用单引号括起来的,还可以用双引......
  • 只需根据接口文档,就能轻松开发 get 和 post 请求的脚本,你会做吗?
     一般的接口文档描述的内容:开发get请求的脚本,接口文档的描述如下:在loadrunner里面创建一个空脚本:在action空白处,点击insert—>step输入web_custom_request,双击选择该函数,填入如下几个参数值:生成的脚本如下:运行编译,看有没有语法错误:在日志里面看到返回了cod......
  • docker离线一键安装脚本
    下载安装文件https://download.docker.com/linux/static/stable/x86_64/https://github.com/docker/compose/releases本例安装文件下载地址https://download.docker.com/linux/static/stable/x86_64/docker-23.0.6.tgzhttps://github.com/docker/compose/releases/download/v......
  • python脚本中调用django环境
    #在脚本中调用djagno服务importosif__name__=='__main__':#1引入django配置文件os.environ.setdefault('DJANGO_SETTINGS_MODULE','day67.settings')#2让djagno启动importdjangodjango.setup()#3使用表模型fromapp01impor......
  • Linux 高级Shell脚本与用户管理(linux文件夹备份脚本)
    本文,我们将要学习Linux高级Shell脚本以及用户管理(重点)。下面,我们将开始探索几个使用的Shell脚本,然后介绍Linux中的用户管理。Shell脚本1创建动态目录首先,我们创建一个名为 create_directories.sh的脚本文件,这个将本将用于生成指定数量且具有动态名称的目录。以下是脚本......