求大神帮忙写一段EXCEL VBA代码,要如图效果。人员数据大概有一万多行。多谢。

文件请从这个链接下载。http://pan.baidu.com/s/1jI2dVcq 密码:7nw0
2024-12-17 13:54:50
推荐回答(3个)
回答1:

代码的框架出来了,细节自己再完善一下

Private Sub CommandButton1_Click()
Dim M_TJ As String
Dim I, J, K As Integer
'M_TJ = ".T."
M_TJ = ""
If Cells(3, 6) <> "" Then
If M_TJ = "" Then
M_TJ = "字号 like " & "'%" & Cells(3, 6) & "%'"
Else
M_TJ = M_TJ & " AND 字号 = " & "'" & Cells(3, 6) & "'"
End If
End If
Dim CONN As New ADODB.Connection
Dim sql As String
Dim myRS As ADODB.Recordset
Set CONN = CreateObject("ADODB.Connection")
myW2 = "家族族谱查询.XLSM"
CONN.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended properties=Excel 12.0;Data Source=" & ThisWorkbook.Path & "\" & myW2
sql = " select 世代 ,字辈,谱名, 字号,排行,父亲,母亲,出生日期,辞世日期,墓园地址 from [汇总$A2:t703] WHERE " & M_TJ
Set myRS = CONN.Execute(sql)
Range("a8:q1000").ClearContents
Range("a8").CopyFromRecordset myRS
CONN.Close
Set CONN = Nothing
End Sub

回答2:

好像直接用函数也可以。

回答3:

我这里不能下载百度云盘,发文件给我,我帮你改一下。