我正在使用下面的代码将excel工作表中的一行导出到word / pdf文件中。但是,它当前正在下载所有非空白行。我希望它能运行,以便在单元格下拉列表中选择一个参考号(“ CA2”)时,它仅下载该选定的行。请帮忙。谢谢。
Sub Download_Click()
Dim CustRow, CustCol, LastRow, TemplRow, Reference, RefRow As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet3
TemplRow = .Range("CI1").Value
TemplName = .Range("BV2").Value
Reference = .Range("CA2").Value
DocLoc = Sheet3.Range("CG2").Value
On Error Resume Next
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
End If
LastRow = .Range("A9999").End(xlUp).Row
For CustRow = 3 To LastRow
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False)
For CustCol = 1 To 70
TagName = .Cells(2, CustCol).Value
TagValue = .Cells(CustRow, CustCol).Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next CustCol
If .Range("BX2").Value = "PDF" Then
FileName = ThisWorkbook.Path & "\" & .Range("B" & CustRow).Value & "_" & .Range("C" & CustRow).Value & ".pdf"
WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
WordDoc.Close False
Else
FileName = ThisWorkbook.Path & "\" & .Range("B" & CustRow).Value & "_" & .Range("C" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName
End If
Next CustRow
WordApp.Quit
End With
End Sub
答案 0 :(得分:0)
据我所知,您的代码会将第3行的所有行导出到最后一行:
For CustRow = 3 To LastRow
如果只希望导出选定的行,则只需删除该循环并将其替换为单个值,因此该值仅运行一次。删除上面的行并替换为:
CustRow = .Range("CA2").Value
确保删除Next CustRow
。另外,请确保单元格CA2
包含一个数字值,该数字值直接指向所需的行。