我正在尝试让我的VBA代码在一个包含3列的表中进行vlookup匹配,当它找到匹配时,它应该将正确的表复制到另一个文件。
我有一个必须输入用户名的用户表单。然后它将此用户名(列A)与表单名称为(C列)的单元格匹配。如何定义此vlookup以匹配然后复制多个工作表。我应该如何格式化C列中的工作表名称?
我的表现在看起来像这样: 答:用户(例如“亚历山德拉”) B:密码(例如“测试1”) C:表格(例如“表1,表2”)
谢谢!
编辑:
我目前的密码授权代码,这是基于Userform:
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中的输入复制工作表。
答案 0 :(得分:0)
如果您将工作表名称放在ColumnS中,如下所示:
Sheet1|Sheet2|Sheet3
(即使用“管道”字符分隔)然后您可以执行以下操作:
'following from finding aCell....
Dim arr
arr = split(aCell.Offset(0,2),"|")
Sheets(arr).Copy '<<if you have a wb you want to copy to then put it here...