ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 395|回复: 7

跟据 密码 权限 隐瞒 工作簿打开关闭其它表的列

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-3-10 13:25 | 显示全部楼层 |阅读模式
跟据 密码 权限 隐瞒 工作簿打开关闭其它表的列

打开EXCEL 输入 用户名 密码 对比“密码表”
如果 1.用户名:admin 密码:admin  正确 可以打开 "密码表",但隐藏其它工作表.
如果 2.其它用户名:密码 正确  显示除"密码表"出外的工作表
如果 3.其它用户名:密码 不正确  关掉EXCEL
如果 4.其它用户名:密码 正确 C2:C单元格内容如果是:"NO",工作簿其它工作表的"O:O,p:p,q:q,r:r,s:s,w:w,x:x,y:y,z:z,aa:aa“列隐藏


工作簿2.rar (20.14 KB, 下载次数: 5)

能用VBA 做吗

image.png


TA的精华主题

TA的得分主题

发表于 2023-3-10 13:31 | 显示全部楼层
弄个窗体,做个判断

TA的精华主题

TA的得分主题

发表于 2023-3-10 20:14 | 显示全部楼层
这类帖子论坛很多,自己搜搜看看吧,
要实现这些要求,没有任何技术难度,纯粹就是繁琐的力气活

TA的精华主题

TA的得分主题

发表于 2023-3-11 08:45 | 显示全部楼层
Private Sub CommandButton1_Click()
Dim rn As Range
yh = ComboBox1.Text
mm = Val(TextBox1.Text)
If yh = "" Then MsgBox "请选择用户名!": Exit Sub
If mm = "" Then MsgBox "请输入用密码!": Exit Sub
With ThisWorkbook.Worksheets("密码表")
    r = .Cells(Rows.Count, 1).End(xlUp).Row
    Set rn = .Range("a2:a" & r).Find(yh, , , , , , 1)
    If rn Is Nothing Then MsgBox "用户名错误,请重新输入!": Exit Sub
    xh = rn.Row
    If mm <> .Cells(xh, 2) Then MsgBox "密码错误,请重新输入!": Exit Sub
End With
Set wb = ThisWorkbook
For Each sh In wb.Worksheets
    If yh = "admin" Then
        If sh.Name = "密码表" Then
            sh.Visible = True
        Else
            sh.Visible = False
        End If
    Else
        If sh.Name = "密码表" Then
            sh.Visible = False
        Else
            sh.Visible = True
        End If
    End If
Next sh
Application.Visible = True
End
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then Cancel = True
End Sub
Private Sub UserForm_Initialize()
With ThisWorkbook.Worksheets("密码表")
    r = .Cells(Rows.Count, 1).End(xlUp).Row
    ar = .Range("a2:a" & r)
End With
Me.ComboBox1.List = ar
End Sub

TA的精华主题

TA的得分主题

发表于 2023-3-11 08:46 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
工作簿2.rar (21.59 KB, 下载次数: 9)

TA的精华主题

TA的得分主题

发表于 2023-3-11 08:47 | 显示全部楼层
隐藏列的代码没写,仅供参考,

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-3-14 11:58 | 显示全部楼层
Private Sub Workbook_Open()
    Dim username As String
    Dim password As String
    Dim name As String
    Dim nameRange As Range
    Dim pwdRange As Range
    Dim permRange As Range
    Dim ws As Worksheet
    Dim pwdSheet As Worksheet
    Dim hideRange As Range
    Dim protectRange As Range
    Dim tries As Integer
    tries = 0
   
    Set pwdSheet = ThisWorkbook.Worksheets("密码表")
   
    Do While tries < 3
        'Prompt for username and password
        username = InputBox("请输入用户名:")
        password = InputBox("请输入密码:")
        
        'Check if username and password are correct
        If username = "admin" And password = "admin" Then
            'Only show the password sheet
            pwdSheet.Visible = xlSheetVisible
            For Each ws In ThisWorkbook.Worksheets
                If ws.name <> "密码表" Then
                    ws.Visible = xlSheetHidden
                End If
            Next ws
            Exit Do
        Else
            'Look for name and password in the "密码表" sheet
            Set nameRange = pwdSheet.Range("A2:A100")
            Set pwdRange = pwdSheet.Range("B2:B100")
            Set permRange = pwdSheet.Range("C2:C100")
            For i = 1 To nameRange.Count
                If nameRange(i) = username And pwdRange(i) = password Then
                    name = nameRange(i)
                    If permRange(i) = "ON" Then
                        'Hide the password sheet and show other sheets
                        pwdSheet.Visible = xlSheetHidden
                        For Each ws In ThisWorkbook.Worksheets
                            If ws.name <> "密码表" Then
                                ws.Visible = xlSheetVisible
                                'Hide columns O to S and W to AA
                                Set hideRange = ws.Range("O:S,W:AA")
                                hideRange.EntireColumn.Hidden = True
                                'Protect columns O to S and W to AA with password "abcd"
                                Set protectRange = ws.Range("O:S,W:AA")
                                protectRange.Worksheet.Unprotect password:="abcd"
                                protectRange.Locked = True
                                ws.Protect password:="abcd"
                            End If
                        Next ws
                    ElseIf permRange(i) = "OK" Then
                        'Hide the password sheet and show other sheets
                        pwdSheet.Visible = xlSheetHidden
                        For Each ws In ThisWorkbook.Worksheets
                            If ws.name <> "密码表" Then
                                ws.Visible = xlSheetVisible
                                'Unhide columns O to S and W to AA
                                Set hideRange = ws.Range("O:S,W:AA")
                                hideRange.EntireColumn.Hidden = False
                                ws.Columns.ColumnWidth = 8.5
                                'Unprotect columns O to S and W to AA with password "abcd"
                                Set protectRange = ws.Range("O:S,W:AA")
                                protectRange.Worksheet.Unprotect password:="abcd"
                                protectRange.Locked = False
                                ws.Unprotect password:="abcd"
                            End If
                        Next ws
                    End If
                    Exit Do
                End If
            Next i
            tries = tries + 1
        End If
    Loop
   
    If tries = 3 Then
        'Close Excel
        Application.DisplayAlerts = False
        Application.Quit
    End If
End Sub


Sub Workbook_BeforeClose_1()
    Dim ws As Worksheet
    Dim hideRange As Range
    Dim protectRange As Range
   
    For Each ws In ThisWorkbook.Worksheets
        If ws.name = "表" Then
            ws.Visible = xlSheetVisible
        Else
            If Not ws.Visible And ws.ProtectContents Then
                ws.Unprotect password:="abcd"
                ws.Visible = xlSheetVisible
                ws.Protect password:="abcd"
            ElseIf Not ws.Visible Then
                ws.Visible = xlSheetVisible
            End If
        End If
        
        Set hideRange = ws.Range("O:S,W:AA")
        Set protectRange = ws.Range("O:S,W:AA")
        ws.Unprotect password:="abcd"
        On Error Resume Next 'Ignore errors caused by hidden and/or protected columns
        hideRange.EntireColumn.Hidden = False
        On Error GoTo 0 'Reactivate error messages
        protectRange.Locked = False
        ws.Protect password:="abcd", DrawingObjects:=True, Contents:=True, Scenarios:=True, _
            AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, _
            AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
    Next ws
End Sub

Sub Workbook_BeforeClose_2()
    Dim ws As Worksheet
    Dim hideRange As Range
    Dim protectRange As Range
   
    '显示名为“表”的工作表并使用密码“abcd”取消保护O至S列和W至AA列
    For Each ws In ThisWorkbook.Worksheets
        If ws.name = "表" Then
            ws.Visible = xlSheetVisible '显示工作表
        Else
            ws.Visible = xlSheetHidden '隐藏工作表
        End If
        
        Set hideRange = ws.Range("O:S,W:AA") '指定要隐藏的列
        Set protectRange = ws.Range("O:S,W:AA") '指定要保护的列
        ws.Unprotect password:="abcd" '取消保护工作表
        On Error Resume Next '忽略由于隐藏和/或保护列而引起的错误
        hideRange.EntireColumn.Hidden = False '隐藏指定的列
        On Error GoTo 0 '重新激活错误消息
        protectRange.Locked = False '取消保护指定的列
        '保护工作表,允许内容、格式、排序、筛选、使用数据透视表,并使用密码“abcd”进行保护
        ws.Protect password:="abcd", DrawingObjects:=True, Contents:=True, Scenarios:=True, _
            AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, _
            AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
    Next ws
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call Workbook_BeforeClose_1
Call Workbook_BeforeClose_2
End Sub




TA的精华主题

TA的得分主题

 楼主| 发表于 2023-3-14 16:25 | 显示全部楼层
本帖最后由 snqig 于 2023-3-14 16:58 编辑


Private Sub Workbook_Open()
    Dim user As String
    Dim password As String
    Dim ws As Worksheet
    Dim hideColumns As Variant
    Dim protectSheet As Boolean
   
    ' Define the array of columns to hide/protect
    hideColumns = Array("税率%", "含税单价", "未税单价", "净价合计", "已开票金额", _
                        "价税合计", "交货日期", "已交货", "结存数量", "已交货金额", _
                        "未税合计已交货", "未开票金额", "结存金额", "未税已交货总价")
   
    ' Prompt user for username and password
    user = InputBox("Enter your username:")
    password = InputBox("Enter your password:")
   
    ' Check username and password
    If user = "admin" And password = "admin" Then
        ' Show only the "密码表" worksheet and hide others
        For Each ws In ThisWorkbook.Worksheets
            If ws.name <> "密码表" Then
                ws.Visible = xlSheetHidden
            End If
        Next ws
    Else
        ' Check if user exists in "密码表" worksheet
        For Each ws In ThisWorkbook.Worksheets
            If ws.name = "密码表" Then
                If ws.Range("A:A").Find(user) Is Nothing Then
                    ' User not found, show error message and close workbook
                    MsgBox "Invalid username or password."
                    ThisWorkbook.Close False
                    Exit Sub
                Else
                    ' User found, check password
                    If ws.Range("B" & ws.Range("A:A").Find(user).Row) <> password Then
                        ' Incorrect password, show error message and close workbook
                        MsgBox "Invalid username or password."
                        ThisWorkbook.Close False
                        Exit Sub
                    Else
                        ' Correct password, hide "密码表" worksheet and unhide others
                        ws.Visible = xlSheetHidden
                        For Each ws In ThisWorkbook.Worksheets
                            If ws.name <> "密码表" Then
                                ws.Visible = xlSheetVisible
                            End If
                        Next ws
                        ' Determine if user has "ON" or "OK" permission
                        protectSheet = True
                        If ws.Range("C" & ws.Range("A:A").Find(user).Row) = "OK" Then
                            protectSheet = False
                        End If
                        ' Hide/protect specified columns
                        For Each ws In ThisWorkbook.Worksheets
                            If ws.name <> "密码表" Then
                                For Each col In hideColumns
                                    If ws.Range("1:1").Find(col) Is Nothing Then
                                        MsgBox "Column " & col & " not found in worksheet " & ws.name & "."
                                    Else
                                        ws.Range(ws.Range("1:1").Find(col), ws.Range("1:1").Find(col).End(xlDown)).EntireColumn.Hidden = protectSheet
                                        ws.Protect password:="password"
                                    End If
                                Next col
                            End If
                        Next ws
                        Exit Sub
                    End If
                End If
            End If
        Next ws
    End If
End Sub

Sub Workbook_BeforeClose_1()
'取消保护所有工作表
For Each ws In Worksheets
ws.Unprotect
Next ws
'取消隐藏保护指定列
Dim hideColumns As Variant
hideColumns = Array("税率%", "含税单价", "未税单价", "净价合计", "已开票金额", "价税合计", "交货日期", "已交货", "结存数量", "已交货金额", "未税合计已交货", "未开票金额", "结存金额", "未税已交货总价")

For Each ws In Worksheets
    If ws.Visible = xlSheetVisible Then '只对可见工作表执行操作
        For Each colName In hideColumns
            If Not ws.Range("1:1").Find(colName) Is Nothing Then '判断列是否存在
                ws.Columns(ws.Range("1:1").Find(colName).Column).Hidden = False '取消隐藏列
                ws.Protect '保护工作表
            End If
        Next colName
    End If
Next ws
End Sub

Sub Workbook_BeforeClose_2()
'隐藏指定工作表并取消保护所有工作表
For Each ws In Worksheets
If ws.name = "表" Then
ws.Visible = xlSheetVisible '显示指定工作表
'取消保护所有工作表
For Each s In ActiveWorkbook.Sheets
s.Unprotect
Next s
'取消隐藏保护指定列
Dim hideColumns As Variant
hideColumns = Array("税率%", "含税单价", "未税单价", "净价合计", "已开票金额", "价税合计", "交货日期", "已交货", "结存数量", "已交货金额", "未税合计已交货", "未开票金额", "结存金额", "未税已交货总价")
For Each s In ActiveWorkbook.Sheets
If s.Visible = xlSheetVisible Then '只对可见工作表执行操作
For Each colName In hideColumns
If Not s.Range("1:1").Find(colName) Is Nothing Then '判断列是否存在
s.Columns(s.Range("1:1").Find(colName).Column).Hidden = True '隐藏列
s.Protect '保护工作表
End If
Next colName
End If
Next s
Else
ws.Visible = xlSheetHidden '隐藏其他工作表
End If
Next ws
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call Workbook_BeforeClose_1
Call Workbook_BeforeClose_2
End Sub


上面代码 For Each ws In uTools_1678784264542.png 错误代码 能我查一下吗
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-30 00:29 , Processed in 0.039471 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表