excel search data return 1 row

2011-05-26 9:15 am
我個表有N咁多個row and col

我想打一data, say date
就會list out 所有, 有關既data field

e.g. member tbl
Name (DOB) (SEX) (ID)(JointDate)
Jose (6/10/1988) (m) (m3200) (05/05/2010)
Kelly 10/02/1990 f f1980 06/10/2010
Mose 11/23/1978 m m1020 05/05/2010


我input DOB, 會show 晒 member 既所有data
(會show, Jose, Mose兩位member 既所有data)

how?
識寫MS SQL, 但以前都係connect to MS database
冇試過用excel..........

excel可唔可以做到好似database咁result 個tbl list出黎?
如果可以, 點SET?
更新1:

Thanks for help me to typing the code out :D but, i'm wondering how do u create a button in excel ?? i couldn't find a button option in there actually i'm not familiar with excel..can u tell me step by step, how do i create a button in excel, i dun even know where i should put the code in:(

更新2:

嗯,,,, 其實3個option都唔太似, 我所想, 但第3個close to what i think 但, 唔需要用<工作表2> 用一個button 就可以了 好似database咁, create a button and it will promtp a digalog box, and i input a data, it returns a list :D

更新3:

哦, 同一張sheet都OK, 因我都係會printout出黎就冇用, 可以undo or 唔save :D sorry, 因差唔多要開工, 所以打英文會快D :( (公司個keyboard好硬, 打中文係咁打錯字) 我用緊好似係xp version, 仲之唔係2003啦:D 我係唔識點樣係excel度加Button, 我搵到有加oject既選項 因我見你code 係 click cmd_button先開始個event 但可以加係邊度? 係咪VBA個度? 我真係唔多熟excel, 我同佢唔係朋友lol

更新4:

wa!! yahoo e-mail度見到你回, 但只係見到第2點........ 第1點睇唔到, 可以我走漏眼=,=" 明天我返好試下:D 謝謝你:D

回答 (3)

2011-05-26 7:45 pm
✔ 最佳答案
暫時念到有三個方法:吾知你問既係咪尼d..
1. 資料 > 篩選 > 進階篩選
( 大前題下要自己先建立好條件 .<---吾明再問..)

2. 用Userform 入面既ListView方法 ( 尼個好似煩d..有排寫野.)

3. 我估你係尼一種方法.
假設你而家工作表1,係你既Data 內容,你想利用工作表2的條利輸入後,運用Excel VBA- SQL 方法 List Out 出黎:
sheet1 NameDOBSEXIDJoint DateJose1995/03/23M2335/5/2010Kelly2007/03/16F2296/10/2010Mose1999/11/27M3055/5/2010
sheet2
  M  NameDOBSEXIDJoint Date


VBA :

Private Sub CommandButton1_Click()

Dim objCnn As Object, strSql As String
If [A1] <> "" Then strSql = strSql & " and Name like '%" & UCase([A1].Value) & "%'"
If [B1] <> "" Then strSql = strSql & " and Dob like '%" & UCase([B1].Value) & "%'"
If [C1] <> "" Then strSql = strSql & " and Sex like '%" & UCase([C1].Value) & "%'"
If [D1] <> "" Then strSql = strSql & " and ID like '%" & UCase([D1].Value) & "%'"
If [E1] <> "" Then strSql = strSql & " and Joint Date like '%" & UCase([E1].Value) & "%'"

If strSql = "" Then Exit Sub
strSql = "select * from [Sheet1$A:E] where " & Mid(strSql, 6)
Set objCnn = CreateObject("adodb.connection")
objCnn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
Range("A3:E65536").ClearContents
Range("A3").CopyFromRecordset objCnn.Execute(strSql)
objCnn.Close
Set objCnn = Nothing

End Sub




2011-05-26 11:55:26 補充:
代碼入面可以同時幫你符合多項符件的輸入,再List out出黎.
就好以我比你既例子.Sheet1 係Data, sheet2 [C1] 當你input "M" ( SEX ItEM)
就會係 a3開始 list out 對應的資料!
Jose 1995/03/23 M 233 40303
Mose 1999/11/27 M 305 40303

今早得閒小小幫你寫左, 但吾知岩吾岩你心水 ( 當比你借鑒一下)..希望可以幫到你.!

2011-06-01 17:54:49 補充:
謝謝 Nick兄的提醒, 嗯...但要點做好呢.?
(ps : 我真係無留意到你講既問題 "實質不能夠 get from recordset ")

其實我對 excel 同vba 都吾係正式學過,都只係單純自己自學.
太多專業既用詞,仲有佢既屬性我都吾係太了解.有時為左出結果,vba 寫左好多冤往路!~~

kaka~請指教 如果我吾用
Range("A3").CopyFromRecordset objCnn.Execute(strSql)
可以點寫呢 :D ?

2011-06-01 18:03:55 補充:
我手上量既案例都係甘寫..

吾通~我尼到出左問題~??
Set objCnn = CreateObject("adodb.connection")
objCnn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName

請nick兄指教!

2011-06-04 12:29:00 補充:
謝謝nick兄的指教,你戈個方法仲加好,簡單意明, 直接 = #" & [A1].Value & "#"

但我c過照你方法去改,但代碼去到. Range("A3").CopyFromRecordset objCnn.Execute(strSql) , 就出現 automation.的錯誤, ..
我又要點改呢~?

2011-06-08 11:29:53 補充:
太好了. 太好了. 謝謝Nick 的指教,
我而家明白了.
即係要區分返吾同 Input ( A1~E1) 的類型.
再定義SQL的資料格式.

本人受教了.~~ thx~~~!! ^_^
2011-06-02 8:29 am
其實要由你地比既code, 我要慢慢改返岩自己用既code
要明白D syntex解咩

1) [E1]....... refer to 什麼?colume E, row 1?
2)ojbConn, 最尾個ThisWorkbook.FullName, 使唔使改名?(自己個file名)

我最要係入date, 再retrieve data, 點樣由, string to date


Range("A3:E65536").ClearContents
A3:E65536 呢個應該係指, 要搵既range麼?

2011-06-02 00:31:00 補充:
[E1] <> "" Then strSql = strSql & " and [Joint Date] = #" & [E1].Value & "#"

[Joint Date] ="#", #=什麼?
[Joint Date]又係指咩? (我成日當左佢係array, 當用左個[] lol
2011-05-27 4:33 pm
Command button 等選項喺 control tool bar 入面, advanced function 嘅 tool bars 可以喺 view > tool bar 入面剔選開啟, control 就係你想揾嗰個, 所有 buttons 喺入面選用, 揀咗 button type, 喺 worksheet 合適位置 left click > hold > 向右下拉 > release 定出 button 大小, 加入 user defined functional code 要 match 返 button name。

2011-06-01 10:20:11 補充:
Stanley 嘅 sql query 看似有顧及 date fields, 但實質不能夠 get from recordset。

2011-06-01 22:16:35 補充:
[B1] <> "" Then strSql = strSql & " and Dob = #" & [B1].Value & "#"

[E1] <> "" Then strSql = strSql & " and [Joint Date] = #" & [E1].Value & "#"

2011-06-01 22:20:00 補充:
其實我都係自學中。。

2011-06-02 01:50:15 補充:
[E1] 係 VBA syntax, 等於 range("E1"), 指 cell range address;

ThisWorkbook.FullName 係目前 running code 嘅 workbook 全名(連 folder path), 妳考慮到 remote workbook 至改啦;

string <> date, cause syntax error;

A3:E65536 係 Stanley 預設嘅 output location, clear old and paste new every time;

2011-06-02 01:50:59 補充:
SQL query 以 statement 溝通, 用兩個 " 作 endings, 識改可以加插 variables;

'3' = 文字 3; '%3' = 文字 *3 (end of 3);

'%M%' = 文字 *M* (contain M);

#5/5/2010# = 日期; 123 = 數目;

[Joint date] 係 recordset 嘅 data field, 單字 fields 可以 omit [ ]。

2011-06-04 17:39:37 補充:
Stanley, 你可能未太 catch 到意見 010 嘅註解, 三種 data types, sql 用語會有別:

文字 data fields: A1, C1 同 D1
… = '%" [A1].value "%'"
same as =*xxx*

日期 data fields: B1 同 E1
… = #" [B1].value "#"
same as =m/d/yyyy

數字 data fields: quantity
… = [Z1].value
same as =12345

2011-06-05 17:17:22 補充:
漏晒啲 & 添… lol

文字 data fields: A1, C1 同 D1
… = '%" & [A1].value & "%'"
same as =*xxx*

日期 data fields: B1 同 E1
… = #" & [B1].value & "#"
same as =m/d/yyyy

數字 data fields: quantity
… = " & [Z1].value
same as =12345

2011-06-08 13:17:17 補充:
大家互相交流啫~ 你成個 recordset structure 好精簡, 我都學到唔少嘢, 我已經將佢記錄在案, 多謝晒~ ^ ^


收錄日期: 2021-04-30 01:20:13
原文連結 [永久失效]:
http://hk.knowledge.yahoo.com/question/question?qid=7011052600077

檢視 Wayback Machine 備份