首页 > 解决方案 > 如何转置单行然后将其从 VBA 中的 excel 2007 导出为 PDF

问题描述

我对编码非常陌生,尤其是在 VBA 中,我遇到了一些问题,希望有人能帮助我。我在网上尝试了一些解决方案,但似乎无法正常工作。我也卡在 excel 2007 中:/

我有一个工作表,其中包含我在 VBA 中构建的用户窗体,它将向一堆列添加一些数据。我还有一些公式可以修改其中的一些数据并为每个数据生成一个唯一标识符,这在以后很重要。

我试图将每一行(带有共享标题)导出到 PDF 中,我认为将其转换为 2 列会更好。到目前为止,我只能导出整个工作表,结果很糟糕,88 页几乎什么都没有。困难的模式是我还希望它被命名为唯一标识符并超链接回该单元格下的电子表格。

我知道我问了很多,但如果有人能把我送到正确的方向,我真的很感激,甚至链接到其他人。我什至不知道我在寻找什么。任何意见都表示赞赏。

到目前为止,我的代码再次对混乱感到抱歉;

非常感谢您的帮助

Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1


 With ws
 '  .Unprotect Password:="password"
.Cells(iRow, 1).Value = Format(Date, "mm-dd-yyyy")
.Cells(iRow, 2).Value = Me.ComboBox7.Value
.Cells(iRow, 3).Value = Me.TextBox12.Value
.Cells(iRow, 4).Value = Me.TextBox11.Value
.Cells(iRow, 5).Value = Me.CheckBox4.Value
.Cells(iRow, 6).Value = Me.ListBox4.Value
 'Row 7 Reserved for Identifier Code!!
.Cells(iRow, 8).Value = Me.TextBox4.Value
.Cells(iRow, 9).Value = Me.TextBox6.Value
.Cells(iRow, 10).Value = Me.ComboBox5.Value
.Cells(iRow, 11).Value = Me.ComboBox6.Value
.Cells(iRow, 12).Value = Me.TextBox7.Value
.Cells(iRow, 13).Value = Me.TextBox8.Value
.Cells(iRow, 14).Value = Me.TextBox13.Value
.Cells(iRow, 15).Value = Me.TextBox14.Value
.Cells(iRow, 16).Value = Me.TextBox2.Value
.Cells(iRow, 17).Value = Me.TextBox5.Value
.Cells(iRow, 18).Value = Me.TextBox9.Value
.Cells(iRow, 19).Value = Me.ComboBox4.Value
.Cells(iRow, 20).Value = Me.TextBox10.Value
.Cells(iRow, 21).Value = Me.TextBox15.Value
.Cells(iRow, 22).Value = Me.CheckBox5.Value
.Cells(iRow, 23).Value = Me.CheckBox6.Value
.Cells(iRow, 24).Value = Me.CheckBox7.Value
.Cells(iRow, 25).Value = Me.CheckBox8.Value
.Cells(iRow, 26).Value = Me.CheckBox9.Value

End With


'Export to PDF.. needs work...

Private Sub CommandButton5_Click()
Application.ScreenUpdating = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:="C:\Users\Admin\Documents\Excel Testing\Export.pdf", _
        OpenAfterPublish:=False
 Application.ScreenUpdating = True

End Sub

标签: excelvbatransposeupdatingexport-to-pdf

解决方案


这将转置一个标题行和一行数据。

Sub ExportTransposedRow(Header As Range, Source As Range)

    With Worksheets.Add
        Header.Copy
        .Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        Source.Copy
        .Range("B1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

        .UsedRange.ExportAsFixedFormat Type:=xlTypePDF, _
                                       Filename:="C:\Users\Admin\Documents\Excel Testing\Export.pdf", _
                                       OpenAfterPublish:=False
        Application.CutCopyMode = False
        Application.DisplayAlerts = False
        .Delete
    End With

End Sub

用法

ExportTransposedRow Rows(1),Rows(3)

推荐阅读