发布网友 发布时间:2022-04-26 14:08
共5个回答
懂视网 时间:2022-04-10 06:11
Sub NextSeven_CodeFrame() ‘应用程序设置 Application.ScreenUpdating = False Application.DisplayAlerts = False Application.Calculation = xlCalculationManual ‘错误处理 On Error GoTo ErrHandler ‘计时器 Dim StartTime, UsedTime As Variant StartTime = VBA.Timer ‘变量声明 Dim wb As Workbook Dim sht As Worksheet Dim Rng As Range Dim Arr As Variant Dim EndRow As Long Dim oSht As Worksheet Dim DataPath As String Dim SQL As String Dim EndDate As Date Dim StartDate As Date Dim Client As String ‘实例化对象 Set wb = Application.ThisWorkbook Set sht = wb.Worksheets("凭证录入") Set oSht = wb.Worksheets("客户明细") DataPath = wb.FullName usertxt = Application.InputBox("请输入开始日期", "开始日期", , , , , , 2) If usertxt = False Then Exit Sub StartDate = Format(CDate(usertxt), "yyyy-mm-dd") usertxt = Application.InputBox("请输入结束日期", "结束日期", , , , , , 2) If usertxt = False Then Exit Sub EndDate = Format(CDate(usertxt), "yyyy-mm-dd") usertxt = Application.InputBox("请输入客户姓名", "客户姓名", , , , , , 2) If usertxt = False Then Exit Sub Client = CStr(usertxt) oSht.UsedRange.Offset(1).Clear Set Rng = oSht.Range("A2") SQL = "SELECT * FROM [" & sht.Name & "$A3:V] WHERE 出货客户=‘" & Client & "‘ AND ( 出货日期 Between #" & StartDate & "# AND #" & EndDate & "# )" SQL = SQL & " ORDER BY 型号 ASC" If RecordExistsRunSQL(DataPath, SQL) = True Then GetRecordSetIntoRange DataPath, SQL, Rng End If ‘运行耗时 UsedTime = VBA.Timer - StartTime MsgBox "本次运行耗时:" & Format(UsedTime, "0.0000000秒") ErrorExit: ‘错误处理结束,开始环境清理 Set wb = Nothing Set sht = Nothing Set Rng = Nothing Application.ScreenUpdating = True Application.DisplayAlerts = True Application.Calculation = xlCalculationAutomatic Exit Sub ErrHandler: If Err.Number <> 0 Then MsgBox Err.Description & "!", vbCritical, "错误提示!" ‘Debug.Print Err.Description Err.Clear Resume ErrorExit End If End Sub Public Sub GetRecordSetIntoRange(ByVal DataPath As String, ByVal SQL As String, ByVal Rng As Range) ‘对传入数据源地址进行判断 If Len(DataPath) = 0 Or Len(Dir(DataPath)) = 0 Then _ MsgBox "数据源地址为空或者数据源文件不存在!", vbInformation, "NS Excel Studio": Exit Sub ‘对传入SQL语句进行判断 If Len(SQL) = 0 Then _ MsgBox "SQL语句不能为空!", vbInformation, "NS Excel Studio": Exit Sub ‘对象变量声明 Dim cnn As Object Dim rs As Object ‘数据库引擎——Excel作为数据源 Const DATA_ENGINE As String = "Provider=Microsoft.jet.OLEDB.4.0;" & _ "Extended Properties=‘Excel 8.0;HDR=YES;IMEX=2‘; Data Source= " ‘创建ADO Connection 连接器 实例 Set cnn = CreateObject("ADODB.Connection") ‘On Error Resume Next ‘创建 ADO RecordSet 记录集 实例 Set rs = CreateObject("ADODB.RecordSet") ‘连接数据源 cnn.Open DATA_ENGINE & DataPath ‘执行查询 返回记录集 rs.Open SQL, cnn, 1, 1 ‘Set RS = CNN.Execute(SQL) ‘复制记录集到指定Range Rng.CopyFromRecordset rs ‘关闭记录集 rs.Close ‘关闭连接器 cnn.Close ‘释放对象 Set rs = Nothing Set cnn = Nothing End Sub Public Function RecordExistsRunSQL(ByVal DataPath As String, ByVal SQL As String) As Boolean ‘对传入数据源地址进行判断 If Len(DataPath) = 0 Or Len(Dir(DataPath)) = 0 Then RecordExistsRunSQL = False MsgBox "数据源地址为空或者数据源文件不存在!", vbInformation, "NS Excel Studio" Exit Function End If ‘对传入SQL语句进行判断 If Len(SQL) = 0 Then RecordExistsRunSQL = False MsgBox "SQL语句不能为空!", vbInformation, "NS Excel Studio" Exit Function End If ‘对象变量声明 Dim cnn As Object Dim rs As Object ‘数据库引擎——Excel作为数据源 ‘Const DATA_ENGINE As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ ‘ "Extended Properties=‘Excel 12.0;HDR=YES;IMEX=2‘; Data Source= " ‘数据库引擎——Excel作为数据源 Const DATA_ENGINE As String = "Provider=Microsoft.jet.OLEDB.4.0;" & _ "Extended Properties=‘Excel 8.0;HDR=YES;IMEX=2‘; Data Source= " ‘创建ADO Connection 连接器 实例 Set cnn = CreateObject("ADODB.Connection") On Error Resume Next ‘创建 ADO RecordSet 记录集 实例 Set rs = CreateObject("ADODB.RecordSet") ‘连接数据源 cnn.Open DATA_ENGINE & DataPath ‘执行查询 返回记录集 rs.Open SQL, cnn, 1, 1 ‘返回函数结果 If rs.RecordCount > 0 Then RecordExistsRunSQL = True Else RecordExistsRunSQL = False End If ‘关闭记录集 rs.Close ‘关闭连接器 cnn.Close ‘释放对象 Set rs = Nothing Set cnn = Nothing End Function
20170112xlVBA查询SQL
标签:orm form close bool val bug sql语句 div his
热心网友 时间:2022-04-10 03:19
我也是个新手,趁此机会学习了一下,技术不高,写的太长了,基本符合要求,除了那生产信息,销售信息的合并单元格外
select abcg.型号,阶段,生产日期,生产数,不良数,销售日期,销售数量 from (select abc.型号,阶段,日期,生产日期,生产数,不良数 from (select a.型号,阶段,日期 from [表A$] a left join (select 型号,生产日期 as 日期 from [表B$] UNION select 型号,销售日期 as 日期 from [表C$]) bc on a.型号=bc.型号) abc left join (select 型号,生产日期,SUM(生产数) AS 生产数,SUM(不良数) AS 不良数 from [表B$] group by 型号,生产日期) bg ON abc.型号=bg.型号 and abc.日期=bg.生产日期) abcg left join (select 型号,销售日期,SUM(销售数量) AS 销售数量 from [表C$] group by 型号,销售日期) cg on abcg.型号=cg.型号 and abcg.日期=cg.销售日期 order by abc.型号,abc.日期
刚少了个排序
追问看这个比较头晕呵呵,你能告诉下你的思路吗?追答1,把BC表的型号和日期用union连接成不重复的,命名为bc,两个字段,型号,日期
2,把a表和bc通过left join生成字段为型号,阶段,日期的新表,命名为abc
3,把b和c表分别按型号和日期汇总,分别命名为bg,cg
4,最后用两个left join把bg,cg,加到abc里去,其中的日期项用来使bg,cg的记录按日期对应
热心网友 时间:2022-04-10 04:37
Excel VBA SQL如何多表联合查询的方法如下:热心网友 时间:2022-04-10 06:12
select abcg.型号,阶段,生产日期,生产数,不良数,销售日期,销售数量 from (select abc.型号,阶段,日期,生产日期,生产数,不良数 from (select a.型号,阶段,日期 from [表A$] a left join (select 型号,生产日期 as 日期 from [表B$] UNION select 型号,销售日期 as 日期 from [表C$]) bc on a.型号=bc.型号) abc left join (select 型号,生产日期,SUM(生产数) AS 生产数,SUM(不良数) AS 不良数 from [表B$] group by 型号,生产日期) bg ON abc.型号=bg.型号 and abc.日期=bg.生产日期) abcg left join (select 型号,销售日期,SUM(销售数量) AS 销售数量 from [表C$] group by 型号,销售日期) cg on abcg.型号=cg.型号 and abcg.日期=cg.销售日期 order by abc.型号,abc.日期热心网友 时间:2022-04-10 08:03
Select