我是VBA的新手,所以这可能是一个简单的问题。
首先,我搜索一个工作表以查找包含单词“ TABLES”的列号。
然后,我要检查此列的单元格是否等于400到450之间的数字,并且当条件为true时,我将创建打印预览。
Sub CustomPrint()
Dim Lost As Variant
Lost = TABLES
With ActiveSheet.Range("A1").CurrentRegion
Set c = .Find(Lost, LookIn:=xlValues)
End With
For i = 400 To 450 Step 1
If (ActiveSheet.Columns(c).Value = i) Then ActiveSheet.PrintOut preview:=True
Next
End Sub
ActiveSheet.Columns(c).Value = i->这是我收到错误消息的地方。
谢谢。
答案 0 :(得分:2)
我认为这对您有用。一旦找到TOTAL
( string ,而不是 variant ),它将遍历该行(从TOTAL
开始)直到最后一行。然后,它将查看该范围内是否有任何单元格在400 |之间。 450并显示为true时显示打印预览
Sub CustomPrint()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim Found As Range, SearchRange As Range, MyCell As Range, LRow as Long
Set Found = ws.Range("A1").CurrentRegion.Find("TABLES", LookIn:=xlValues)
If Found Is Nothing Then Exit Sub
LRow = ws.Cells(ws.Rows.Count, Found.Column).End(xlUp).Row
Set SearchRange = ws.Range(ws.Cells(Found.Row + 1, Found.Column), ws.Cells(LRow, Found.Column))
For Each MyCell In SearchRange
If MyCell > 400 And MyCell < 450 Then
MyCell.WorkSheet.PrintOut Preview:=True
End If
Next MyCell
End Sub
答案 1 :(得分:0)
由于每个匹配值都需要打印输出,因此我想您需要将打印区域与匹配单元格相关:
Option Explicit
Sub CustomPrint()
Dim cell As Range
On Error GoTo ExitSub
With Range("A1").CurrentRegion.Find(what:="TABLE", LookIn:=xlValues, lookat:=xlWhole)
For Each cell In .Resize(.Parent.Cells(Rows.Count, .Column).End(xlUp).Row - .Column + 1).SpecialCells(xlCellTypeConstants, xlNumbers)
If cell.Value2 > 400 And cell.Value2 < 450 Then cell.Resize(20, 20).Offset(-WorksheetFunction.Min(10, cell.Row - 1), -WorksheetFunction.Min(10, cell.Column - 1)).PrintOut preview:=True ' this sort of center the printout to the current cell and its 20x20 surroundings
Next
End With
ExitSub:
End Sub