根据数据验证下拉列表返回偏移单元格值

问题描述 投票:0回答:1

我有一个发票模板,其中 Sheet1!A10 中包含数据验证下拉列表,列表位置位于 Sheet3!B2:B4 中。

我想根据下拉列表选择 (Sheet3!B2:B4) 返回 Sheet3!A2:A4 中的值,以使用下拉列表中的偏移值保存文件副本。

Sub FileSaveAs()
    Dim custlist As String
    Dim custname As String
    Dim path As String
    Dim filename As String

    custlist = Sheets(1).DropDowns(Range("A10:C10"))
    custname = Application.WorksheetFunction.VLookup(custlist, Sheet3.Range("a:b"), 1, False)
    path = "C:\Users\files" 
    filename = custname 

    Sheet1.Copy

    With ActiveWorkbook
        .Sheets(1).Name = "Invoice"
        .SaveAs filename:=path & filename, FileFormat:=51
        .Close
    End With
End Sub

如何根据下拉选项

custname
设置
custlist
的值?

excel vba validation drop-down-menu vlookup
1个回答
0
投票
  • 像普通单元格一样读取带有数据验证下拉列表的单元格的值。

微软文档:

StrComp 函数

Option Explicit

Sub FileSaveAs()
    Dim custList As String
    Dim custName As String
    Dim sPath As String
    custList = Sheet1.Range("A10").Value
    sPath = "C:\Users\files\"
    If Len(custList) > 0 Then
        For Each c In Sheet3.Range("B2:B4")
            If StrComp(c.Value, custList, vbTextCompare) = 0 Then
                custName = c.Offset(0, -1).Value
            End If
        Next
    End If
    If Len(custList) > 0 Then
        Sheet1.Copy
        With ActiveWorkbook
            .Sheets(1).Name = "Invoice"
            .SaveAs filename:=sPath & custName, FileFormat:=51
            .Close
        End With
    End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.