简体   繁体   中英

excel vba if cell contains value print area in excel spreadsheet?

I am trying to use vba code to print an area of excel (AC120:AT128) when a cell value contains "X" At the moment my code prints out the whole spread sheet but I only want it to print out the selected cell area.

Can someone please show me how I can achieve this? thanks in advance.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim celltxt As String
celltxt = ActiveSheet.Range("AQ98").Text
If InStr(1, celltxt, "X") Then

 Dim ws As Worksheet
 For Each ws In Worksheets
 If ws.Range("AQ98") = "X" Then
 ws.PrintOut
 End If
 Next ws

Else

End If

End Sub

I'm not sure what version of Excel you're using, but in Office 2010, you can simply use.

Range("AC120:AT128").PrintOut()

Or

Selection.PrintOut()

You can also use the set print area function, eg

ws.PageSetup.PrintArea = "AC120:AT128"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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