简体   繁体   English

如何转置一行然后在VBA中将其从Excel 2007导出为PDF

[英]How to transpose a single row then export that to PDF from excel 2007 in VBA

Im very very new to coding, especially in VBA, and Im having problems I hope someone can help me with. 我对编码非常陌生,尤其是在VBA中,而且我遇到问题,希望有人可以帮助我。 I've tried a few solutions online but I cant seem to get it working. 我已经在线尝试了一些解决方案,但似乎无法正常工作。 Im also stuck in excel 2007 :/ 我也陷入了Excel 2007:/

I have a worksheet that has a userform I've built in VBA that will add some data to a bunch of columns. 我有一个工作表,该工作表具有我在VBA中建立的用户窗体,该用户窗体会将一些数据添加到一列中。 Ive also got some formulas that modify some of that data and produce a unique identifier to each one, this is important later. Ive还获得了一些公式,这些公式可以修改某些数据并为每个数据生成唯一的标识符,这在以后很重要。

Im trying to export each individual row (with the shared headers) into a PDF, and I figure transposing it to 2 columns would work much better. 我试图将每一行(带有共享标题)导出为PDF,我认为将其转换为2列会更好。 So far I can only export the whole worksheet and it comes out terrible with 88 pages of mostly nothing. 到目前为止,我只能导出整个工作表,它的88页几乎什么都没有,结果很糟糕。 The hard mode of this is that I also want it to be named the unique identifier and hyperlinked back into the spreadsheet under that cell.. 困难的模式是我还希望将其命名为唯一标识符,然后超链接回到该单元格下的电子表格。

I know im asking a lot but if anyone can send me in the right direction Id really appreciate it, even links to others. 我知道我要问的很多,但是如果有人能按正确的方向发送给我,我将非常感谢,甚至可以与他人联系。 I dont know enough to even know what Im looking for. 我还不知道我在寻找什么。 Any comments at all are appreciated. 任何评论都表示赞赏。

My code so far, again, sorry for the mess; 到目前为止,我的代码仍然很抱歉。

Many thanks for any help at all! 非常感谢您的任何帮助

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

This will transpose a header row and a row of data. 这将转置标题行和数据行。

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

Usage 用法

 ExportTransposedRow Rows(1),Rows(3) 

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

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