for循环中的VBA公式设置-语法问题



嗨,我正在尝试获得一些帮助,看看为什么这在我设置的宏中不起作用。调试器导致问题的区域位于第二个选择。公式区域。

Sub PrintAllonges()
'
' PrintAllonges Macro
'
' Keyboard Shortcut: Ctrl+Shift+Y
'
Dim pdfName As String, FullName As String, Path As String, lRow As Long

Set oFSO = CreateObject("Scripting.FileSystemObject")
Path = CreateObject("WScript.Shell").specialfolders("Desktop")
' Create Desktop Folder if not exists
If oFSO.FolderExists(Path & "Allonges") Then
Else
MkDir Path & "Allonges"
End If

'Turn off Screen Update
Sheets("MissingAllonges").Select
lRow = Cells(Rows.Count, 1).End(xlUp).Row
MsgBox (lRow)
Sheets("AllongeTemplate").Select
Application.ScreenUpdating = False

For i = 2 To lRow
Range("G6").Select
Selection.Formula = "=MissingAllonges!I" & i
Range("E11").Select
Selection.Formula = _
"=TEXT(MONTH(MissingAllonges!D" & i & "),""mmmm"")&"" ""&DAY(MissingAllonges!D" & i & ")&"", ""&YEAR(MissingAllonges!D" & i & ")"""

pdfName = Sheets("AllongeTemplate").Range("H7").Value & " - " & Sheets("AllongeTemplate").Range("G6").Value & " Allonge"
FullName = Path & "Allonges" & pdfName & ".pdf"

ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FullName, OpenAfterPublish:=False
Next i

Application.ScreenUpdating = True

End Sub

我把这个放进去,并开始为其他公式工作,我正在更新循环,但我无法让它工作,并在语法上出现错误。

公式末尾有额外的引号。

修正后的公式为:

.Formula = "=TEXT(MONTH(MissingAllonges!D" & i & "),""mmmm"")&"" ""&DAY(MissingAllonges!D" & i & ")&"", ""&YEAR(MissingAllonges!D" & i & ")"

但我同意@BigBen的观点,即公式可以简化,即:

.Formula = "=TEXT(MissingAllonges!D" & i & ", ""mmmm d, yyyy"")"

最新更新