:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser
🪷 คำแสดงเจตนา
ขอผลแห่งการให้ความรู้นี้ จงกลับไปยังผู้ที่เป็นเจ้าของเดิม แม้ข้าพเจ้าจะไม่รู้จักท่านก็ตาม ขอให้แสงแห่งปัญญาที่ท่านเคยจุดไว้ ได้กลับไปเติมเต็มชีวิตของท่านอีกครั้ง และขอให้เจตนาของข้าพเจ้าเป็นการคืนความดีอย่างสงบ

Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่

Post a reply


This question is a means of preventing automated form submissions by spambots.
Smilies
:D :thup: :cp: :flw: :rz: :sg: :tt: :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :ard: :arl: :aru: :| :mrgreen: :geek: :ugeek:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#10

by snasui » Mon Jun 01, 2020 5:26 pm

pangjung39 wrote: Mon Jun 01, 2020 4:50 pm เงื่อนไขคือ ถ้า AA10 และ AA11 บวกกันไม่เท่ากับ 0
:D การคำนวณใน VBA ก็เหมือนกับการคำนวณทั่วไป จากที่ถามมาสามารถเขียนเป็น

If Worksheets(wksAllSheets(i)).Range("AA10").Value + Worksheets(wksAllSheets(i)).Range("AA11").Value <> 0 Then

หรือถ้ามั่นใจว่า A10:A11 คือตัวเลขที่แท้จริง ไม่ใช่เก็บตัวเลขแบบ Text ก็ใช้การรวมเข้ามาช่วยได้ เช่นด้านล่างครับ

If Application.Sum(Worksheets(wksAllSheets(i)).Range("AA10:AA11").Value) <> 0 Then

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#9

by pangjung39 » Mon Jun 01, 2020 4:50 pm

snasui wrote: Thu May 28, 2020 6:26 pm :D Code ที่เขียนมาคือ Export ทุกชีตพร้อมกันทีเดียวไปเป็น 1 ไฟล์

การที่จะไม่ Export ออกไปก็ไต้องไม่เลือกชีตนั้น ๆ ก่อนที่จะ Export ครับ

หากผมเข้าใจถูกต้องตัวอย่างการปรับ Code จะเป็นตามด้านล่างครับ

Code: Select all

'Other code
Dim exptShs() As Variant, i As Integer, j As Integer

'Set references up-front
Set wksSheet1 = ThisWorkbook.Sheets("nm4-1")
wksAllSheets = Array("nm4-1", "nm4-2", "nm4-3", "nm4-4", "nm4-5", "nm8-1", "nm8-2", "nm8-3", "nm8-4", "nm8-5")
strFilepath = "N:\U-CM\08 GPC InterPipeline and I-4 TruckLoad and Ole&Aro MatTrans\8.04 MAT (GMM)\8.10.01 Billing\Olefins\Excise Olefins\REPORT EXCISE\"

For i = 0 To UBound(wksAllSheets)
    If Worksheets(wksAllSheets(i)).Range("AA10").Value <> 0 Then
        ReDim Preserve exptShs(j)
        exptShs(j) = wksAllSheets(i)
        j = j + 1
    End If
Next i

'Create the full Filename using cells D6, E6 and F6
With wksSheet1

    'Assemble the string cell-by-cell, "D6 E6-F6"
    strFilename = strFilepath & "report" & Format(Range("O4"), "d-mmm-yyyy") & ".pdf"


End With

'    'Save the Array of worksheets (which will be selected) as a PDF
ThisWorkbook.Sheets(exptShs).Select
wksSheet1.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strFilename, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

'Other code
ผมขออนุญาติ รบกวนถามเพิ่มเติม พอดีผม ต้องการ เช็คค่าที่ "AA11" ด้วย เหมือนที่เช็คที่ AA10
(ผมลองมาหลายวิธี Search หาคำสั่งจาก GOOGLE)
เงื่อนไขคือ ถ้า AA10 และ AA11 บวกกันไม่เท่ากับ 0 ให้ทำการ Save / ถ้ารวมกันแล้ว เท่ากับ 0 ไม่ต้องSave
ผมตั้ง Range คำสั่งนี้มัน ERROR ครับ
ขอบพระคุณเป็นการล่วงหน้านะครับ
For i = 0 To UBound(wksAllSheets)
If Worksheets(wksAllSheets(i)).Range("AA10:AA11").Value <> 0 Then
ReDim Preserve exptShs(j)
exptShs(j) = wksAllSheets(i)
j = j + 1
End If

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#8

by pangjung39 » Mon Jun 01, 2020 12:27 pm

ขอบพระคุณมากๆเลยครับ

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#7

by snasui » Thu May 28, 2020 6:26 pm

:D Code ที่เขียนมาคือ Export ทุกชีตพร้อมกันทีเดียวไปเป็น 1 ไฟล์

การที่จะไม่ Export ออกไปก็ไต้องไม่เลือกชีตนั้น ๆ ก่อนที่จะ Export ครับ

หากผมเข้าใจถูกต้องตัวอย่างการปรับ Code จะเป็นตามด้านล่างครับ

Code: Select all

'Other code
Dim exptShs() As Variant, i As Integer, j As Integer

'Set references up-front
Set wksSheet1 = ThisWorkbook.Sheets("nm4-1")
wksAllSheets = Array("nm4-1", "nm4-2", "nm4-3", "nm4-4", "nm4-5", "nm8-1", "nm8-2", "nm8-3", "nm8-4", "nm8-5")
strFilepath = "N:\U-CM\08 GPC InterPipeline and I-4 TruckLoad and Ole&Aro MatTrans\8.04 MAT (GMM)\8.10.01 Billing\Olefins\Excise Olefins\REPORT EXCISE\"

For i = 0 To UBound(wksAllSheets)
    If Worksheets(wksAllSheets(i)).Range("AA10").Value <> 0 Then
        ReDim Preserve exptShs(j)
        exptShs(j) = wksAllSheets(i)
        j = j + 1
    End If
Next i

'Create the full Filename using cells D6, E6 and F6
With wksSheet1

    'Assemble the string cell-by-cell, "D6 E6-F6"
    strFilename = strFilepath & "report" & Format(Range("O4"), "d-mmm-yyyy") & ".pdf"


End With

'    'Save the Array of worksheets (which will be selected) as a PDF
ThisWorkbook.Sheets(exptShs).Select
wksSheet1.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strFilename, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

'Other code

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#6

by pangjung39 » Thu May 28, 2020 3:18 pm

ขอบคุณครับ แต่ยังไม่ใช่อะครับ ผมก็ไม่รู้วิธีเขียนว่าจะทำอย่างไร

-----------
โดยปกติ Code จะทำการSave ชีทที่เลือกทุกตัวเป็น PDF
แต่ผมอยากแปลง ให้ Macro ไปเช็คที่ AA10 ถ้า AA10 เป็น 0 ไม่ต้อง Save ชีทนั้นๆ ครับ แต่ชีทอื่นที่ AA10 มีค่าตั้งแต่ 1 ขึ้นไปก็ Save ปกติครับ

ขอบพระคุณครับ
logic wrote: Wed May 27, 2020 4:30 pm แบบนี้หรือเปล่าครับ

Code: Select all

Sub Picture2_Click()
    Dim wksAllSheets As Variant
    Dim wksSheet1 As Worksheet
    Dim strFilename As String, strFilepath As String
    Dim i As Integer
    'Set references up-front
    Set wksSheet1 = ThisWorkbook.Sheets("nm4-1")
    wksAllSheets = Array("nm4-1", "nm4-2", "nm4-3", "nm4-4", "nm4-5", "nm8-1", "nm8-2", "nm8-3", "nm8-4", "nm8-5")
    strFilepath = "N:\U-CM\08 GPC InterPipeline and I-4 TruckLoad and Ole&Aro MatTrans\8.04 MAT (GMM)\8.10.01 Billing\Olefins\Excise Olefins\REPORT EXCISE\"
    
    For i = 0 To UBound(wksAllSheets)
        If Worksheets(wksAllSheets(i)).Range("AA10").Value = 0 Then
            MsgBox "ตรวจสอบ Sheet " & wksAllSheets(i)
            Exit Sub
        End If
    Next i
    'Create the full Filename using cells D6, E6 and F6
    With wksSheet1

        'Assemble the string cell-by-cell, "D6 E6-F6"
        strFilename = strFilepath & "report" & Format(Range("O4"), "d-mmm-yyyy") & ".pdf"


    End With
    
'    'Save the Array of worksheets (which will be selected) as a PDF
    ThisWorkbook.Sheets(wksAllSheets).Select
    wksSheet1.ExportAsFixedFormat _
              Type:=xlTypePDF, _
              Filename:=strFilename, _
              Quality:=xlQualityStandard, _
              IncludeDocProperties:=True, _
              IgnorePrintAreas:=False, _
              OpenAfterPublish:=True

    'Make sure all the worksheets are NOT left selected
    wksSheet1.Select
End Sub

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#5

by logic » Wed May 27, 2020 4:30 pm

แบบนี้หรือเปล่าครับ

Code: Select all

Sub Picture2_Click()
    Dim wksAllSheets As Variant
    Dim wksSheet1 As Worksheet
    Dim strFilename As String, strFilepath As String
    Dim i As Integer
    'Set references up-front
    Set wksSheet1 = ThisWorkbook.Sheets("nm4-1")
    wksAllSheets = Array("nm4-1", "nm4-2", "nm4-3", "nm4-4", "nm4-5", "nm8-1", "nm8-2", "nm8-3", "nm8-4", "nm8-5")
    strFilepath = "N:\U-CM\08 GPC InterPipeline and I-4 TruckLoad and Ole&Aro MatTrans\8.04 MAT (GMM)\8.10.01 Billing\Olefins\Excise Olefins\REPORT EXCISE\"
    
    For i = 0 To UBound(wksAllSheets)
        If Worksheets(wksAllSheets(i)).Range("AA10").Value = 0 Then
            MsgBox "ตรวจสอบ Sheet " & wksAllSheets(i)
            Exit Sub
        End If
    Next i
    'Create the full Filename using cells D6, E6 and F6
    With wksSheet1

        'Assemble the string cell-by-cell, "D6 E6-F6"
        strFilename = strFilepath & "report" & Format(Range("O4"), "d-mmm-yyyy") & ".pdf"


    End With
    
'    'Save the Array of worksheets (which will be selected) as a PDF
    ThisWorkbook.Sheets(wksAllSheets).Select
    wksSheet1.ExportAsFixedFormat _
              Type:=xlTypePDF, _
              Filename:=strFilename, _
              Quality:=xlQualityStandard, _
              IncludeDocProperties:=True, _
              IgnorePrintAreas:=False, _
              OpenAfterPublish:=True

    'Make sure all the worksheets are NOT left selected
    wksSheet1.Select
End Sub

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#4

by pangjung39 » Wed May 27, 2020 3:44 pm

อันนี้คือชุด Code ทั้งหมดนะครับ อยากจะเพิ่มในส่วนของการตรวจสอบค่า AA10 ทุกชีท ถ้า เท่ากับ 0 ไม่ต้อง Save อะครับ

Code: Select all

Sub Picture2_Click()
Dim wksAllSheets As Variant
    Dim wksSheet1 As Worksheet
    Dim strFilename As String, strFilepath As String
    
    'Set references up-front
    Set wksSheet1 = ThisWorkbook.Sheets("nm4-1")
    wksAllSheets = Array("nm4-1", "nm4-2", "nm4-3", "nm4-4", "nm4-5", "nm8-1", "nm8-2", "nm8-3", "nm8-4", "nm8-5")
    strFilepath = "N:\U-CM\08 GPC InterPipeline and I-4 TruckLoad and Ole&Aro MatTrans\8.04 MAT (GMM)\8.10.01 Billing\Olefins\Excise Olefins\REPORT EXCISE\"
    
    'Create the full Filename using cells D6, E6 and F6
    With wksSheet1

        'Assemble the string cell-by-cell, "D6 E6-F6"
        strFilename = strFilepath & "report" & Format(Range("O4"), "d-mmm-yyyy") & ".pdf"


    End With
    
'    'Save the Array of worksheets (which will be selected) as a PDF
    ThisWorkbook.Sheets(wksAllSheets).Select
    wksSheet1.ExportAsFixedFormat _
              Type:=xlTypePDF, _
              Filename:=strFilename, _
              Quality:=xlQualityStandard, _
              IncludeDocProperties:=True, _
              IgnorePrintAreas:=False, _
              OpenAfterPublish:=True

    'Make sure all the worksheets are NOT left selected
    wksSheet1.Select
End Sub
Attachments
Sample.xlsm
(97.99 KiB) Downloaded 6 times

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#3

by pangjung39 » Wed May 27, 2020 3:39 pm

logic wrote: Wed May 27, 2020 10:25 am การถามเรื่อง code อ่านกติกาข้อ 4 กับ 5 ด้วย ☝ เดี๋ยวจะไม่มีใครอาสาเข้ามาตอบครับ
ขอโทษด้วยครับ หาปุ่ม Edit กระทู้ไม่เจอ

Re: Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#2

by logic » Wed May 27, 2020 10:25 am

การถามเรื่อง code อ่านกติกาข้อ 4 กับ 5 ด้วย ☝ เดี๋ยวจะไม่มีใครอาสาเข้ามาตอบครับ

Save PDF ด้วย Macro ถ้าจะให้ดูค่าใน CELL ถ้า 0 ไม่ Save ต้องทำอย่างไรครับ

#1

by pangjung39 » Wed May 27, 2020 9:09 am

คือตอนนี้ผมใช้ Macro Save Sheet "nm4-1", "nm4-2", "nm4-3", "nm4-4", "nm4-5", "nm8-1", "nm8-2", "nm8-3", "nm8-4", "nm8-5"
แต่ปัญหาคือ บางชีทถ้าค่า AA10 = 0 ไม่ต้องการให้เซฟ ผมต้องเพิ่ม Code อย่างไรครับ


Sub Picture2_Click()
Dim wksAllSheets As Variant
Dim wksSheet1 As Worksheet
Dim strFilename As String, strFilepath As String

'Set references up-front
Set wksSheet1 = ThisWorkbook.Sheets("nm4-1")
wksAllSheets = Array("nm4-1", "nm4-2", "nm4-3", "nm4-4", "nm4-5", "nm8-1", "nm8-2", "nm8-3", "nm8-4", "nm8-5")
strFilepath = "N:\U-CM\08 GPC InterPipeline and I-4 TruckLoad and Ole&Aro MatTrans\8.04 MAT (GMM)\8.10.01 Billing\Olefins\Excise Olefins\REPORT EXCISE\"

'Create the full Filename using cells D6, E6 and F6
With wksSheet1

'Assemble the string cell-by-cell, "D6 E6-F6"
strFilename = strFilepath & "report" & Format(Range("O4"), "d-mmm-yyyy") & ".pdf"


End With

' 'Save the Array of worksheets (which will be selected) as a PDF
ThisWorkbook.Sheets(wksAllSheets).Select
wksSheet1.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strFilename, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True

'Make sure all the worksheets are NOT left selected
wksSheet1.Select
End Sub

Top