: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

Constrain VBA search?

ฟอรัมถาม-ตอบปัญหาการใช้งาน Macro และ VBA
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. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
krisney
Member
Member
Posts: 4
Joined: Thu Nov 15, 2018 4:06 am

Constrain VBA search?

#1

Post by krisney »

Hello, in your example of MyAdvancedSearch, is there a way to constrain the search to look only at specific columns, rather than the whole range?

Thank you, Kristina
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: Constrain VBA search?

#2

Post by snasui »

:D Please attach your example file and point to a column that you want to search.
krisney
Member
Member
Posts: 4
Joined: Thu Nov 15, 2018 4:06 am

Re: Constrain VBA search?

#3

Post by krisney »

Hello, I've attached the file. The search box is in B1.

Problem 1: It doesn't seem to launch a search every time though. I have to click in the box and hit the Delete button for all of the data rows to be returned to normal (possibly I've done something wrong).

Problem 2: And for some reason it always includes the first row of data, even if the Search Term is not located in that row.

Problem 3: And if I could search for 3 keywords, that would be awesome :thup:

Problem 4: Constrain the search to only specific Columns?

Thank you for trying to help!
Kristina
Attachments
ForSNASUI.xlsm
(494.39 KiB) Downloaded 13 times
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: Constrain VBA search?

#4

Post by snasui »

:D Try this code.

Problem 1: In case of B1 is blank this code don't search anything and show all rows of data.

Problem 2: Because the old code starts with Row 4 instead of Row 3.

Problem 3: With the code below you can search many words separated by a comma (ex. Express, issue, BGP, recovery).

Problem 4: The code below will be searching on column C, you can change to any column by changing this line.
For Each r In .Range("c3", .Range("c" & .Rows.Count).End(xlUp))

Code: Select all

Sub MyAdvancedSearch()
    Application.ScreenUpdating = False
    Dim r As Range ', v As String
    Dim s As Variant, i As Integer
    Dim j As Integer
    With Sheets("Data")
        .Range("c2").CurrentRegion.EntireRow.Hidden = False
        If .Range("b1").Value = "" Then Exit Sub
        s = Split(.Range("b1").Value, ",")
        For Each r In .Range("c3", .Range("c" & .Rows.Count).End(xlUp))
            j = 0
            For i = 0 To UBound(s)
                If InStr(1, UCase(r.Value), Application.Trim(UCase(s(i)))) > 0 Then
                    j = j + 1
                End If
            Next i
            If j = 0 Then
                r.EntireRow.Hidden = True
            End If
        Next r
    End With
    Application.ScreenUpdating = True
End Sub
krisney
Member
Member
Posts: 4
Joined: Thu Nov 15, 2018 4:06 am

Re: Constrain VBA search?

#5

Post by krisney »

Wow - thank you! I have no idea what this new code means (I was just using yours from YouTube)! I'll have to do some research.
How could I change this line to search more than 1 column, or the whole table?

For Each r In .Range("c3", .Range("c" & .Rows.Count).End(xlUp))

Thank you VERY much - Kristina
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: Constrain VBA search?

#6

Post by snasui »

krisney wrote: Tue Nov 20, 2018 3:32 am How could I change this line to search more than 1 column, or the whole table?
:D You can change to the code below:

Code: Select all

Sub MyAdvancedSearch()
    Application.ScreenUpdating = False
    Dim r As Range, rln  As Range
    Dim s As Variant, i As Integer
    Dim j As Integer, str As Variant
    With Sheets("Data")
        .Range("c2").CurrentRegion.EntireRow.Hidden = False
        If .Range("b1").Value = "" Then Exit Sub
        s = Split(.Range("b1").Value, ",")
        For Each r In .Range("a3", .Range("a" & .Rows.Count).End(xlUp))
            j = 0
            For Each rln In r.Resize(1, 20)
                str = str & rln.Value
            Next
            For i = 0 To UBound(s)
                If InStr(1, UCase(str), Application.Trim(UCase(s(i)))) > 0 Then
                    j = j + 1
                End If
            Next i
            If j = 0 Then
                r.EntireRow.Hidden = True
            End If
        Next r
    End With
    Application.ScreenUpdating = True
End Sub
Sorry to let you know about the VBA question. One of the board rules members has to try to change the code from the answer to achieve the goal. If the issue persists or a new problem arises, you can request to solve the problem without restriction.
krisney
Member
Member
Posts: 4
Joined: Thu Nov 15, 2018 4:06 am

Re: Constrain VBA search?

#7

Post by krisney »

You are awesome Santipong, thank you!
Kristina
Post Reply