我有一个发票模板,其中 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
的值?
微软文档:
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