如何根据组合框中的值更改代码引用的工作表?

huangapple go评论59阅读模式
英文:

How can I make my code change the worksheet it is referencing depending on the value in a combobox?

问题

Sure, here's the translated portion of your text:

我有一个稍微难以理解的问题。我是一个非常没有经验的VBA用户,正在制作一个销售录入表单。目前,我有一个组合框,其中填充了工作簿上不同工作表的名称。

我有“下一个”和“上一个”按钮,使用以下代码从不同行中提取信息:

Private Sub BHSDNEXTTAPBUTTONLF_Click()
With Me.BHSDROWLABELLF
If Not IsEmpty(Worksheets("GG TAPS").Cells(.Caption + 2, 20)) Then
.Caption = Val(.Caption) + 1
End If

Me.BHSDADDRESSLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 23)
Me.BHSDALTPHONELF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 26)
Me.BHSDCAMPAIGNSLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 34)
Me.BHSDCCPDLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 31)
Me.BHSDCOMPANYNAMELF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 21)
Me.BHSDCSZLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 24)
Me.BHSDCVVLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 4)
Me.BHSDEMAILLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 22)
Me.BHSDEXPLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 3)
Me.BHSDHIGHAMOUNTLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 25)
Me.BHSDHOWWHOLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 32)
Me.BHSDLASTCARDLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 2)
Me.BHSDMAINNUMBERLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 19)
Me.BHSDPOSS1LF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 27)
Me.BHSDPOSS2LF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 28)
Me.BHSDPOSS3LF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 29)
Me.BHSDTAPNAMELF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 20)
Me.BHSDWHATWHYLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 33)
Me.BHSDZIPCODELF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 5)
End With
End Sub

代替Worksheets("GG TAPS").Cells,我希望工作表的名称是当前在组合框中选择的名称。这是否可能?

英文:

I have a bit of a head scratcher here. I am a very inexperienced VBA user, and I am making a Sales entry form. Right now, I have a combo box that is populating with the names of different sheets on the workbook. 如何根据组合框中的值更改代码引用的工作表?

I have "next" and "previous" buttons that pull up info from different rows using this code:

Private Sub BHSDNEXTTAPBUTTONLF_Click()
With Me.BHSDROWLABELLF
If Not IsEmpty(Worksheets("GG TAPS").Cells(.Caption + 2, 20)) Then
.Caption = Val(.Caption) + 1
End If

Me.BHSDADDRESSLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 23)
Me.BHSDALTPHONELF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 26)
Me.BHSDCAMPAIGNSLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 34)
Me.BHSDCCPDLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 31)
Me.BHSDCOMPANYNAMELF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 21)
Me.BHSDCSZLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 24)
Me.BHSDCVVLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 4)
Me.BHSDEMAILLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 22)
Me.BHSDEXPLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 3)
Me.BHSDHIGHAMOUNTLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 25)
Me.BHSDHOWWHOLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 32)
Me.BHSDLASTCARDLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 2)
Me.BHSDMAINNUMBERLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 19)
Me.BHSDPOSS1LF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 27)
Me.BHSDPOSS2LF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 28)
Me.BHSDPOSS3LF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 29)
Me.BHSDTAPNAMELF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 20)
Me.BHSDWHATWHYLF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 33)
Me.BHSDZIPCODELF.Value = Worksheets("GG TAPS").Cells(.Caption + 1, 5)

End With
End Sub

Instead of Worksheets("GG TAPS").Cells, I want the worksheet to be the name that is currently selected in the combo box. Is this possible?

答案1

得分: 4

与对象一起工作。您的代码将变得更加简单处理。

这是您正在尝试的吗?(未经测试

我已经注释了代码。但如果您遇到问题,请在下面留下评论。

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    
    '检查用户是否在组合框中选择了一个值
    '使用相关的组合框更改ComboBox1
    If ComboBox1.ListIndex = -1 Then
        MsgBox "选择工作表的名称"
        Exit Sub
    End If
    
    '处理在填充组合框时的任何拼写错误
    On Error Resume Next
    Set ws = ThisWorkbook.Sheets(ComboBox1.Value)
    On Error GoTo 0
    
    If ws Is Nothing Then
        MsgBox "此工作簿没有名称为" & ComboBox1.Value & "的工作表"
        Exit Sub
    End If
    
    '然后,您可以使用工作表对象
    If Not IsEmpty(ws.Cells(BHSDROWLABELLF.Caption + 2, 20).Value2) Then
        BHSDROWLABELLF.Caption = Val(BHSDROWLABELLF.Caption) + 1
    End If
    
    '或者使用WITH - END WITH
    
    With ws
        If Not IsEmpty(.Cells(BHSDROWLABELLF.Caption + 2, 20).Value2) Then
            BHSDROWLABELLF.Caption = Val(BHSDROWLABELLF.Caption) + 1
        End If
    End With
    
    '其余的代码
End Sub
英文:

Work with Objects. Your code will become much simpler to handle.

Is this what you are trying? (UNTESTED)

I have commented the code. But if you face an issue then leave a comment below.

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    
    '~~> Check if the user has selected a value in Combo box
    '~~> Change ComboBox1 with the relevant combo box
    If ComboBox1.ListIndex = -1 Then
        MsgBox "Select the name of the worksheet"
        Exit Sub
    End If
    
    '~~> This is to handle any typo while populating the combo
    On Error Resume Next
    Set ws = ThisWorkbook.Sheets(ComboBox1.Value)
    On Error GoTo 0
    
    If ws Is Nothing Then
        MsgBox "This workbook doesn't have the sheet with the name " & ComboBox1.Value
        Exit Sub
    End If
    
    '~~> And then you can use the worksheet Object
    If Not IsEmpty(ws.Cells(BHSDROWLABELLF.Caption + 2, 20).Value2) Then
        BHSDROWLABELLF.Caption = Val(BHSDROWLABELLF.Caption) + 1
    End If
    
    '~~> OR using WITH - END WITH
    
    With ws
        If Not IsEmpty(.Cells(BHSDROWLABELLF.Caption + 2, 20).Value2) Then
            BHSDROWLABELLF.Caption = Val(BHSDROWLABELLF.Caption) + 1
        End If
    End With
    
    '
    '~~> Rest of the code
    '
End Sub

huangapple
  • 本文由 发表于 2023年5月21日 11:57:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定