【问题标题】:Probleme to ave auto excel on pdf在pdf上自动生成excel的问题
【发布时间】:2019-08-21 09:48:18
【问题描述】:

我对以 pdf 格式记录我的 Excel 文件的网站有点担心。

我希望将记录的文件记录在文件中,与代表百分比的单元格的值相比。示例:I2 单元格 = 5% 因此,我的文件将被命名为 Document 5%,我希望它记录在 Roasts 5% 文件中。除了我发现它记录在其他地方。

感谢您的帮助

Sub Bouton_PDF()

Dim NomFichier As String
Dim Chemin As String

Select Case Range("I2").Text
    Case Is = "1%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE" & Range("I2").Text
    Case Is = "2%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 2%"
    Case Is = "3%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 3%"
    Case Is = "4%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 4%"
    Case Is = "5%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 5%"
   Case Is = "6%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 6%"
   Case Is = "7%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 7%"
   Case Is = "8%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 8%"
   Case Is = "9%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 9%"
   Case Is = "10%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 10%"
   Case Is = "11%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 11%"
   Case Is = "12%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 12%"
  Case Is = "13%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 13%"
  Case Is = "14%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 14%"
  Case Is = "15%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 15%"
  Case Is = "16%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 16%"
  Case Is = "17%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 17%"
  Case Is = "18%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 18%"
  Case Is = "19%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 19%"
   Case Is = "20%"
        Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 20%"
End Select



NomFichier = "GRILLE_TARIFIAIRE_RETAIL_ATLAS_NEGOCE_REMISE_" & Range("I2").Text


            Range("A1:H81").Select
            Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NomFichier, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
            OpenAfterPublish:=True

End Sub

【问题讨论】:

  • “其他地方”在哪里?因为这会给你一个关于正在发生的事情的线索(而不是你认为应该发生的事情)。你不使用Chemin。您可以将其简化为"Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE" & Range("I2").Text,但也许文本需要特定的格式才能将“%”元素放入其中。

标签: excel vba pdf save


【解决方案1】:

在这一行:

Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NomFichier, _

您只说 PDF 的文件名应该是什么 - 而不是您要保存 PDF 的文件夹。因此,默认情况下,PDF 可能会保存到保存工作簿的文件夹或其他文件夹中。

另外,我认为某些文件系统将文件名(和文件路径)限制为特定的字符限制。如果您的文件路径或文件名太长,您可能会收到错误,并且 PDF 可能无法保存。

尽管您可能需要更改 CheminNomFichier 的值(因为我不清楚您想要实现什么),但请尝试下面的代码。

Option Explicit

Sub Bouton_PDF()

    Dim percentageValue As String
    percentageValue = Range("I2").Text ' This will refer to cell I2 of whatever sheet is active whilst the code is running.

    Dim Chemin As String ' This is the folder that your PDF will be saved in.
    Chemin = "Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE " & percentageValue & "\"

    Dim NomFichier As String ' This is the name of the PDF.
    NomFichier = "GRILLE_TARIFIAIRE_RETAIL_ATLAS_NEGOCE_REMISE_" & percentageValue & ".pdf"

    ' Say the percentage is 2%:
    ' Then Chemin will be Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 2%\
    ' And NomFichier will be GRILLE_TARIFIAIRE_RETAIL_ATLAS_NEGOCE_REMISE_2%.pdf
    ' And the full file path will be Y:\Sebiha\GRILLE TARIFAIRE\GRILLE TARIFAIRE 1%\GRILLE_TARIFIAIRE_RETAIL_ATLAS_NEGOCE_REMISE_2%.pdf

    ' Again, this will refer to cells A1:H81 of whatever sheet is active whilst the code is running. Include the worksheet's name if you can.
    Range("A1:H81").ExportAsFixedFormat Type:=xlTypePDF, Filename:=Chemin & NomFichier, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

End Sub

【讨论】:

  • 如果文件夹不存在,或者单元格 I2 中的值是 25% 或 15,7% 怎么办?请注意,OP 正在使用Select Case(缺少Case Else)可能不仅仅是为了选择文件夹,还可能是为了验证I2 值。
  • @EEM 对我来说,if Len(dir(chemin, vbdirectory)) > 0 then 似乎是检查文件夹是否存在的更好方法。如果仍然需要验证百分比,可以将其强制转换为数字,与数字比较运算符(><)进行比较,并检查value mod 1 = 0 是否只允许某个范围内的整数。似乎是 20 个大部分重复的案例陈述的更好、更短的替代方案。只是我的意见。
  • 您应该将其添加到您的答案中。但是为什么在验证百分比时“只允许整数”呢?
  • @EEM 当我说强制类型时,我的意思是解析字符串 "5%" 以将其转换为 5(而不是 0.05)和 "5.6%" 转换为 5.6 - 因为我认为我们只对字符串的那一部分感兴趣。但是,是的,我稍后会在我不使用移动设备时修改我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 2012-01-13
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多