返回列表 上一主題 發帖

[發問] 關於不固定欄 自動列印至最後一筆資料

[發問] 關於不固定欄 自動列印至最後一筆資料

本帖最後由 starry1314 於 2016-4-24 19:32 編輯

請問如何從類別(A2)選擇右手邊第一個P.P位置後往下最後一筆資料,並列印

因欄位不固定且標題會有空白,不過空白格到時會隱藏

在從類別(T2) 往右至P.P欄往下最後一筆資料並列印

原預計使用 MATCH取得P.P所在欄數
但搭配range選擇欄數時會變成選整欄,使用ctrl+shift往上的指令也不會到最後一筆資料,反而手動按確會選擇
自動列印.rar (27.76 KB)

回復 1# starry1314


    給你參考,我的做法
  1. Option Explicit

  2. Sub 巨集1()
  3.     Dim c As Range
  4.     With Sheets("工作表1")
  5.         Set c = .Rows(3).Find("P.P", , , , , 1)
  6.         .PageSetup.PrintArea = "$A$2:" & .Cells(Rows.Count, c.Column).End(xlUp).Address '設定列印範圍
  7.         .PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False '列印
  8.         Set c = Nothing
  9.         Set c = .Rows(3).Find("P.P", , , , , 2)
  10.         .PageSetup.PrintArea = "$T$2:" & .Cells(Rows.Count, c.Column).End(xlUp).Address
  11.         .PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False '列印
  12.         Set c = Nothing
  13.     End With
  14. End Sub
複製代碼

TOP

回復 1# starry1314


    也可以這樣!
  1. Option Explicit

  2. Sub 巨集1()
  3.     Dim c As Range
  4.     With Sheets("工作表1")
  5.         Set c = .Rows(3).Find("P.P", , , , , 1) '往後尋找
  6.         .PageSetup.PrintArea = .Range(.Cells(Rows.Count, 1).End(xlUp), Cells(2, c.Column)).Address '設定列印範圍
  7.         .PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False '列印
  8.         Set c = Nothing
  9.         Set c = .Rows(3).Find("P.P", , , , , 2) '往前尋找
  10.         .PageSetup.PrintArea = .Range(.Cells(Rows.Count, 1).End(xlUp), Cells(2, c.Column)).Address '設定列印範圍
  11.         .PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False '列印
  12.         Set c = Nothing
  13.     End With
  14. End Sub
複製代碼

TOP

回復 3# lpk187


    實在太感謝了,find 真好用

TOP

回復 3# lpk187


    剛改了下用在篩選方面,全自動了....隨標題的改變自己找是第幾欄後再進行篩選後另存
又可以多偷懶了

TOP

如果只是設定列印範圍,
.PageSetup.PrintArea = "$A$2:" & .Cells(Rows.Count, c.Column).End(xlUp).Address
也可用:
Range(.[A2], .Cells(Rows.Count, c.Column).End(xlUp)).Name = "Print_Area"

相對速度較快!!!

TOP

如果只是設定列印範圍,
.PageSetup.PrintArea = "$A$2:" & .Cells(Rows.Count, c.Column).End(xlUp).Addre ...
准提部林 發表於 2016-4-26 11:43



    感謝教學!!

TOP

回復 3# lpk187
回復 6#

    Set c = .Rows(3).Find("P.P", , , , , 1) '往後尋找
想請問能否不要往前往後 可選擇找到的第幾個  目前遇到同一張有三張表,

目前只能先將後兩個表的P.P做點改變

TOP

本帖最後由 lpk187 於 2016-4-26 20:42 編輯

回復 8# starry1314

在此先感謝准提部林大大教授的"Print_Area"語法

    利用Range.Find說明範例修改一下,可以達到你的要求,就是再利用FindNext
  1. Sub 巨集1()
  2.     Dim c As Range, firstAddress$, myColumn%
  3.     With Sheets("工作表1")
  4.         Set c = .Rows(3).Find("P.P", LookIn:=xlValues)
  5.             If Not c Is Nothing Then
  6.                 firstAddress = c.Address
  7.                 myColumn = 1
  8.                 Do
  9.                      .Range(.Cells(2, myColumn), .Cells(Rows.Count, c.Column).End(xlUp)).Name = "Print_Area"
  10.                      .PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
  11.                     Set c = .Rows(3).FindNext(c)
  12.                     myColumn = myColumn + 19
  13.                 Loop While Not c Is Nothing And c.Address <> firstAddress
  14.             End If
  15.     End With
  16. End Sub
複製代碼

TOP

回復  starry1314

在此先感謝准提部林大大教授的"rint_Area"語法

    利用Range.Find說明範例修改一 ...
lpk187 發表於 2016-4-26 20:38


感謝...要研究一下 比上一個複雜了....

TOP

        靜思自在 : 滴水成河。粒米成蘿,勿輕己靈,勿以善小而不為。
返回列表 上一主題