【发布时间】:2014-02-11 14:55:51
【问题描述】:
我正在尝试让我的 VBA 代码在具有 3 列的表中进行 vlookup 匹配,当它找到匹配项时,它应该将正确的工作表复制到另一个文件中。
我有一个必须输入用户名的用户表单。然后它将这个用户名(A 列)与(C 列)中带有工作表名称的单元格匹配。如何定义此 vlookup 以匹配然后复制多张工作表。我应该如何格式化 C 列中的工作表名称?
我的桌子现在看起来像这样: A:用户(例如“Alexandra”) B:密码(例如“Test 1”) C:工作表(例如“工作表 1、工作表 2”)
谢谢!
编辑:
我当前的密码授权代码,这是基于用户表单的:
Private Sub cmdLogin_Click()
Dim RowNo As Long
Dim Id As String, pw As String
Dim ws As Worksheet
Dim aCell As Range
On Error GoTo ErrorHandler
If Len(Trim(txtLogin)) = 0 Then
txtLogin.SetFocus
MsgBox "Gebruikersnaam moet ingevuld zijn."
Exit Sub
End If
If Len(Trim(txtPassword)) = 0 Then
txtPassword.SetFocus
MsgBox "Wachtwoord moet ingevuld zijn."
Exit Sub
End If
Application.ScreenUpdating = False
Set ws = Worksheets("PW")
Id = LCase(Me.txtLogin)
Set aCell = ws.Columns(1).Find(What:=Id, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> If match found
If Not aCell Is Nothing Then
RowNo = aCell.Row
'~~> Rest of your code. For example if the password is
'~~> Stored in Col B then
'~~> Replace txtPassword with the actual name of password textbox
If Me.txtPassword = aCell.Offset(, 1) Then
Call CopySheets()
Unload Me
Else
MsgBox "De ingevoerde gegevens zijn onjuist. Probeer het opnieuw.", vbOKOnly
End If
Else '<~~ If not found
MsgBox "De ingevoerde gegevens zijn onjuist. Probeer het opnieuw.", vbOKOnly
End If
CleanExit:
Set ws = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
MsgBox Err.Description
Resume CleanExit
End Sub
现在我需要代码来根据 txtLogin 中的输入复制工作表。
【问题讨论】:
-
如果您有代码,您应该将其包含在您的问题中。这到底是哪一部分给你带来了问题?
-
已添加代码。从 txtlogin vlookup 输入的复制代码给我带来了问题。我不知道如何开始这段代码。