Excel macro to print sheets based on multiple values

て烟熏妆下的殇ゞ 提交于 2019-12-20 07:39:50

问题


I have a sheet with a macro that when i change a value in the cell E5, automatically fill certain fields in sheet1, searches and return values from a table on other sheet - sheetTable).

My goal now is to select a range of values in a column on sheetTable and assign each one of them to Cell E5 on sheet1, and print each one.

So let's say I select 3 cells with values: 45, 50 and 66. When I run the macro it will assign 45 to cell E5 and print sheet1, then it will assign 50 to cell E5 and print sheet1 and finally will assign 66 to cell E5 and print sheet1.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim shape As Excel.shape
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("e5")


If Not Application.Intersect(KeyCells, Range(Target.Address)) _
       Is Nothing Then


For Each shape In ActiveSheet.Shapes
    shape.Delete
Next

InsertPictureInRange Range("f2").Value & Range("e5").Value & ".jpg", _
    Range("c9")
InsertPictureInRange Range("f2").Value & Range("e5").Value & "_1.jpg", _
    Range("i9")

If Range("e5").Value = "" Then Exit Sub

End If

End Sub

回答1:


The following will run through a selection and paste the values in your cell E5, on Sheet1, and then open the "PrintToFile" dialogue box. From there, you would have to manually input the file location and name.

Dim Rng As Range
Set Rng = Selection

Dim Sheet1 As Worksheet
Set Sheet1 = Sheets("Sheet1")

    For Each Cell In Rng
        Sheet1.Range("e5").Value = Cell.Value
        Sheet1.PrintOut Copies:=1, printtofile:=True, collate:=True, ignoreprintareas:=False
    Next Cell

This gets you halfway there, at least. Perhaps someone with more experience than I will be able to add how one might input the file location and name using VBA.



来源:https://stackoverflow.com/questions/46972036/excel-macro-to-print-sheets-based-on-multiple-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!