: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

ค้นหารหัสที่ถูกต้องและออก comment dymamic ให้ตรง

ฟอรัมถาม-ตอบปัญหาการใช้งานสูตรและฟังก์ชัน Excel
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. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
bkkrong
Member
Member
Posts: 60
Joined: Mon Jan 26, 2015 4:07 pm
Excel Ver: 2010

ค้นหารหัสที่ถูกต้องและออก comment dymamic ให้ตรง

#1

Post by bkkrong »

ผมพยายามสร้าง comment โดยให้ตรวจสอบ ว่า ใน sheet1! column : J มีค่าเป็น ."Y" หรือไม่ ถ้าไม่มี ไม่ออก comment ถ้ามี ให้กลับไป check --> holdingBuy!B4:AZ38 ว่าตรงกับรหัส item อะไร ใน sheet1! column : D ที่ตรงกัน เพื่อนำเอาค่า comment ใน holdingBuy!B43:AZ43 ที่ตรงกันมาออก comment

ประเด็นคือ การออก comment ออกได้ตรงตามรหัส แต่
1 ผมไม่สามารถเขียน code ให้ระบบตรวจรหัส ใน sheet1! column : D = lookup holdingBuy!B43:AZ43 ได้ และให้มันไล่เช็คลงมาเรื่องๆ เพื่อออก comment ผมจึงทำได้แค่ input box ทดลอง (อยากเปลี่ยนตรงนี้)
2 ผมทดลอง ปรากฎว่า แม้ผมให้ target ออก comment sheet ที่ 1 แต่เมื่อไรผมไป sheet อื่น มันออก comment ใน sheet ที่ active นั้น
3 จะปรับขนาดพื้นที่ comment อย่างไร ที่อ่านได้หมด ส่วนใหญ่ตกขอบหมด
กรุณาช่วยปรับ code ให้ด้วยครับ

ขอบคุณครับ

****module4******

Code: Select all

' test comment almost OK

Sub Find_First()
    Dim FindString As String
    Dim cmt As String
    Dim cm As comment
    Dim rng As Range
     Dim sResult As String
      Dim tar As Range: Set scr = Worksheets("holdingBuy").Range("B43:AZ43") 'lookup comment
     Dim src As Range: Set tar = Worksheets("sheet1").Range("J3:J13") 'set location comment
         
         
     For i = 0 To tar.Rows.Count - 1
        For j = 0 To tar.Columns.Count - 1
     
    'FindString = ActiveCell.Offset(0, -6).Value  ' ----------how to
    FindString = InputBox("Enter a Search value")
    If Trim(FindString) <> "" Then
        With Sheets("holdingBuy").Range("B4:AZ38")
            Set rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not rng Is Nothing Then
          cmt = Worksheets("holdingBuy").Cells(43, rng.Column).Value
            
            MsgBox cmt
            ' found item_id
            
        If Worksheets("sheet1").Cells(tar.Row + i, tar.Column + j).Value = "Y" Then
           MsgBox "find Yes"
    sResult = cmt

        With Cells(tar.Row + i, tar.Column + j)
            .ClearComments
            .AddComment
            .comment.Text Text:=sResult
        End With
        Else
         MsgBox "not holdingbuy!!!"
       ' With Cells(tar.Row + i, tar.Column + j)
           ' .ClearComments
        
           ' .AddComment
           ' .comment.Text Text:=sResult
         ' End With
           
         
          End If
     
      
           ' .comment.Text Text:="Column A = & val1"
               'Sheets("holdingBuy").rng("A1").AddComment().Text = "Regular Comment"
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If
     Next j
        
    Next i
            

End Sub
You do not have the required permissions to view the files attached to this post.
bkkrong
Member
Member
Posts: 60
Joined: Mon Jan 26, 2015 4:07 pm
Excel Ver: 2010

Re: ค้นหารหัสที่ถูกต้องและออก comment dymamic ให้ตรง

#2

Post by bkkrong »

pic
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31257
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: ค้นหารหัสที่ถูกต้องและออก comment dymamic ให้ตรง

#3

Post by snasui »

:D ตัวอย่าง Code ครับ

Code: Select all

Sub Test0()
    Dim rAll As Range, r As Range
    Dim rhAll As Range, rh As Range
    Dim strCmt As String, found As Boolean
    
    With Sheets("Sheet1")
        Set rAll = .Range("j3", .Range("j" & .Rows.Count).End(xlUp))
        For Each r In rAll
            If r.Value = "Y" Then
                found = False
                With .Parent.Sheets("holdingBuy")
                    Set rhAll = .Range("b4:az38")
                    For Each rh In rhAll
                        If rh.Value = r.Offset(0, -6).Value Then
                            strCmt = .Cells(43, rh.Column)
                            found = True
                            Exit For
                        End If
                    Next rh
                End With
                If found Then
                    If r.comment Is Nothing Then
                        r.AddComment
                    End If
                    r.comment.Text strCmt
                End If
            Else
                If Not r.comment Is Nothing Then
                    r.ClearComments
                End If
            End If
        Next r
    End With
End Sub
Post Reply