根据总行高

时间:2017-07-11 19:39:00

标签: excel vba vbscript

有一点背景知识,我有一个vbs代码,它从多个excel文件中提取,然后将它们粘贴到word文档中以创建报告。一切都像它应该的那样,但是我需要能够根据它们的组合行高度选择一系列行。这样做的原因是因为我正在复制图形并将其作为图片粘贴到单词中,然后代码打开另一个excel文件并选择数据并将其粘贴到单词中作为图形下方的图片然后继续循环直到它已经完成了所有文件。我可以在图表下方放置8个正常大小的行(14.3)或总行高114.4,如果使用的行范围具有大于此值的组合行高,则需要转到下一页。

我现在拥有的是代码将查看使用的范围,如果它大于8行,它将选择使用的范围并将其粘贴到下一页或如果使用的范围小于8,它将选择一个即使它们是空的,也总共有8行,并粘贴它以使整个报告中看起来均匀。然而,有时候数据少于8行但是必须增加一行或两行以适应所有文本(列宽必须固定,因为它必须适应word文档的设置区域因此,使8行的总行高大于114.4,并且它将不再适合图表下方。在这种情况下,我需要它根据114.4的组合行高而不仅仅是8行进行选择。

我希望我已经解释了我需要做些什么以获得一些急需的帮助。下面是我目前试图根据行高选择数据的尝试...它会在没有错误的情况下运行代码,但是它只会偏离使用的范围,我无法获取指向行高的代码什么,如果它只是忽略了那段代码。

Sub Events(f)
    Set objWkb3 = objExcel.Workbooks.Open(f)
    LastRowEvents = objWkb3.Sheets("Events").usedRange.Rows.Count
    LastRowEvents1 = objWkb3.Sheets("Events").usedRange.Rows
    LastRowEvents2 = objWkb3.sheets("Events").usedRange.RowHeight
    LastRowEvents3 = objWkb3.sheets("Events").Range("F1:F8").RowHeight


    'Selection of Events
    If LastRowEvents > 8 then
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture
    objWord1.Selection.TypeParagraph
    objWord2.Selection.TypeParagraph
    Elseif LastRowEvents2 > 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture
    objWord1.Selection.TypeParagraph
    objWord2.Selection.TypeParagraph
    Elseif LastRowEvents2 = 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture
    Else objWkb3.Sheets("Events").Range("A1:F8").CopyPicture
    End If

    'Events to PJR
    wsh.sleep 1500
    objWord1.Selection.Paste
    objWord1.Selection.MoveRight
    objWord1.Selection.TypeParagraph

    'Events to Gas
    objWord2.Selection.Paste
    objWord2.Selection.MoveRight
    objWord2.Selection.TypeParagraph

    'objWkb3.Save
    objWkb3.Close(0)
End Sub

1 个答案:

答案 0 :(得分:0)

我一直在玩并尝试不同的东西,通过将.RowHeight更改为.Height我能够让代码按照我想要的方式工作。

Sub Events(f)
    Set objWkb3 = objExcel.Workbooks.Open(f)
    LastRowEvents = objWkb3.Sheets("Events").usedRange.Rows.Count
    LastRowEvents1 = objWkb3.Sheets("Events").usedRange.Rows
    LastRowEvents2 = objWkb3.sheets("Events").usedRange.Height
    LastRowEvents3 = objWkb3.sheets("Events").Range("A1:F8").Height
    MRow1 = Round(((114.4 - LastRowEvents2)/14.3),0)
    MRowT = MRow1 + LastRowEvents

    'Selection of Events
    If LastRowEvents > 8 then
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture
    objWord1.Selection.TypeParagraph
    objWord2.Selection.TypeParagraph
    Elseif LastRowEvents2 > 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture
    objWord1.Selection.TypeParagraph
    objWord2.Selection.TypeParagraph
    Elseif LastRowEvents2 = 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture
    Elseif LastRowEvents3 > 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & MRowT).CopyPicture
    Elseif LastRowEvents3 = 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture
    Else objWkb3.Sheets("Events").Range("A1:F8").CopyPicture
    End If

    'Events to PJR
    wsh.sleep 500
    objWord1.Selection.Paste
    objWord1.Selection.MoveRight
    objWord1.Selection.TypeParagraph

    'Events to Gas
    objWord2.Selection.Paste
    objWord2.Selection.MoveRight
    objWord2.Selection.TypeParagraph

    'objWkb3.Save
    objWkb3.Close(0)
End Sub