简体   繁体   English

Excel 2010:最佳使用CASE或IF定义SAVEAS位置(VBA)

[英]Excel 2010: Best use of CASE or IF to define SAVEAS location (VBA)

Program: Excel 2010 程序: Excel 2010
Experience: Basic 经验:基本

Question
I am wanting to save my workbook sheet (and generated .pdf) in locations dependent on a cell value, rather than writing 5 Subs, I want to write one using either IF or CASE . 我想将我的工作簿工作表(和生成的.pdf文件)保存在取决于单元格值的位置,而不是编写5个Subs, 而是想使用IFCASE编写一个 Subs。 I do have a static save location (dropbox), however I also need to save duplicates in the respective Cell locations. 我确实有一个静态保存位置(投递箱),但是我还需要在各个单元格位置中保存重复项。

I can't get the syntax correct to get either working. 我无法正确使用任何一种语法。

Sub saveManID()
    Dim sDB As String
    Dim sMDocs As String
    Dim sMBus As String
    Dim sName As String
    Dim sSel As String
    Dim sMan As String

    'define file name
    sName = Sheets("Statement").Range("B52").Text

    'define location name
    sDB = "E:\location dropbox\"
    sMDocs = "D:\My Documents\"
    sMBus = "D:\location alt\"

    '---- Either IF or CASE to define the SAVEAS location ----'  
    If Sheets("Statement").Range("J2").Text = "3" Then
        sMan = "G:\location\folder3\"

    If Sheets("Statement").Range("J2").Text = "4" Then
        sMan = "G:\location\folder4\"

    '---- end ----'   

    ActiveWorkbook.SaveAs Filename:=sMan & sName & Format(Date, "YYYYMMDD") & ".xls",_  
     FileFormat:=52, ReadOnlyRecommended:=False, CreateBackup:=False    

    Sheets("Statement").Range("A1:G49").ExportAsFixedFormat Type:=xlTypePDF, Filename:=sMan & sName & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


        End if
    End if
End Sub  

I need to get the Range("J2") to define location from the cell value & I need to create the name based on another cell value. 我需要获取Range("J2")来从单元格值中定义位置,并且需要基于另一个单元格值创建名称。

The Sub runs fine if I exclude the IF , but it means I have to have the code duplicated and a button assigned for each value. 如果我排除IF ,Sub可以很好地运行,但这意味着我必须重复代码并为每个值分配一个按钮。

To clarify - the Cell Value Range("J2") will determine where the file saves to, in the example above it is in the IF statement, which does not work. 需要说明的是-单元格值Range("J2")将确定文件保存到的位置,在上面的示例中,该文件在IF语句中不起作用。

To fix what you have written try moving your End If before your common code like this: 要修正您编写的内容,请尝试将End If移到常见代码之前,如下所示:

If Sheets("Statement").Range("J2").Text = "3" Then
    sMan = "G:\location\folder3\"
End If

If Sheets("Statement").Range("J2").Text = "4" Then
    sMan = "G:\location\folder4\"
End if

ActiveWorkbook.SaveAs Filename:=sMan & sName & Format(Date, "YYYYMMDD") & ".xls",_  
 FileFormat:=52, ReadOnlyRecommended:=False, CreateBackup:=False    

Sheets("Statement").Range("A1:G49").ExportAsFixedFormat Type:=xlTypePDF, Filename:=sMan & sName & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

Or like this: 或像这样:

If Sheets("Statement").Range("J2").Text = "3" Then
    sMan = "G:\location\folder3\"
ElseIf Sheets("Statement").Range("J2").Text = "4" Then
    sMan = "G:\location\folder4\"
End if

Or like this: 或像这样:

myVal = Sheets("Statement").Range("J2").Text
If myVal = "3" Then
    sMan = "G:\location\folder3\"
ElseIf myVal = "4" Then
    sMan = "G:\location\folder4\"
End if

To use a Select Case re-write it like this: 要使用Select Case ,请像下面这样重写它:

Select Case Sheets("Statement").Range("J2").Text
      Case "3"
           sMan = "G:\location\folder3\"

      Case "4"
           sMan = "G:\location\folder4\"

      Case Else
           'set sMan to your default dropbox location here
End Select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM