Page 1 of 1

คำสั่ง IF กบั Hyperlink ค่ะ

Posted: Sun Aug 21, 2011 8:13 pm
by panida
ดิฉันเขียน VBA สร้างสูตร If ไว้แล้วค่ะ
คือ ถ้า cell B3 เป็นคำว่า "not yet pay" ให้ เป็นเหมือนเดิมคือ แสดงคำว่า "not yet pay"
ถ้าไม่ใช่ (ไม่ใช่คำว่า "not yet pay") ให้นำค่านั้นไป hyperlink ไฟล์ ที่มีชื่อเดียวกับคำนั้น

เช่น ถ้าเป็น "not yet pay" ก็แสดงผลเป็นคำว่า "not yet pay"
ถ้าเป็น "AP1" ก็ให้ hyperlink ไฟล์ชื่อ "AP1

ปัญหาคือว่า พอกลับมาเป็นคำว่า "not yet pay"
มันก็ยังติดค่า hylink อยู่ ทำยังไงถึงไม่ติดเป็นตัวอักษรปกติดีค่ะ
รบกวนอาจารย์ช่วยดูทีค่ะ

ขอบคุณค่ะ
พนิดา

Code: Select all

    Range("B3").Select
    If Range("B3").Value = "not yet pay" Then
    Range("B3").Value = "not yet"
    
    Else
    Range("B3").Select
    Dim s As String
    s = Range("B3").Value
    ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:= _
    "C:\Users\Administrator\Desktop\Countermeasure\" & s & ".xls", TextToDisplay:=Range("B3").Value
    
    End If

Re: คำสั่ง IF กบั Hyperlink ค่ะ

Posted: Sun Aug 21, 2011 11:35 pm
by panida
สวัสดีค่ะ อาจารย์
ดิฉันลองพยายามทุกวิถีทางแล้ว
พบว่าลอง delete cell ก่อนลองค่อยทำตาม loop เดิม
พอช่วยกำจัด hyperlink ได้
ไม่แน่ใจว่าอาจารย์จะมีวิธีที่ดีกว่านี้รึเปล่า
ดิฉันลองทำได้เท่านี้ค่ะ

Code: Select all

    Range("B3").Select
    If Range("B3").Value = "not yet pay" Then
    Selection.ClearContents
    Range("B3").Value = "not yet"
    ActiveCell.Select

    Else
    Range("B3").Select
    Dim s As String
    s = Range("B3").Value
    ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:= _
    "C:\Users\Administrator\Desktop\Countermeasure\" & s & ".xls", TextToDisplay:=Range("B3").Value
    
    End If

Re: คำสั่ง IF กบั Hyperlink ค่ะ

Posted: Mon Aug 22, 2011 4:07 pm
by snasui
:D ลองปรับ Code เป็นตามด้านล่างครับ

Code: Select all

Sub test1()
    With Range("B3")
        .Hyperlinks.Delete
        If .Value = "not yet pay" Then
            .Value = "not yet"
        Else
         .Hyperlinks.Add Anchor:=Range("B3"), Address:= _
            "C:\Users\Administrator\Desktop\Countermeasure\" & .Value & ".xls", TextToDisplay:=.Value
        End If
    End With
End Sub

Re: คำสั่ง IF กบั Hyperlink ค่ะ

Posted: Mon Aug 22, 2011 11:05 pm
by panida
ขอบคุณค่ะ
พนิดา