-
vba 位 前 相似 筛选_工作表数据查询时,类似筛选功能LIKE和NOT LIKE的应用
2021-01-14 15:37:00大家好,我们继续讲解VBA数据库解决方案,今日讲解第53讲内容:工作表查询时,类似于筛选功能的LIKE和NOT LIKE 的应用。大家在工作的时候,利用EXCEL操作,筛选是必不可少的工具之一。例如我们可以筛选以某个字符...大家好,我们继续讲解VBA数据库解决方案,今日讲解第53讲内容:工作表查询时,类似于筛选功能的LIKE和NOT LIKE 的应用。大家在工作的时候,利用EXCEL操作,筛选是必不可少的工具之一。例如我们可以筛选以某个字符开头的数据,或者筛选不以某个字符开始的数据,那么这个功能如何在ADO连接EXCEL进行查询时实现呢?
实例讲解,我们看下面的数据:
我们要筛选出以W开始的生产厂的数组和不是以W开始的数据。如何实现呢?
下面看我给出的代码:
Sub mynzRecords_53() '第53讲 工作表数据查询时,类似筛选功能LIKE和NOT LIKE的应用.
Dim cnADO, rsADO As Object
Dim strPath, strSQL1, strSQL2, strSQL3, strSQL4 As String
Worksheets("53").Select
Cells.ClearContents
Set cnADO = CreateObject("ADODB.Connection")
Set rsADO = CreateObject("ADODB.Recordset")
strPath = ThisWorkbook.FullName
cnADO.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties='excel 12.0;hdr=yes;imex=1';data source=" & strPath
strSQL1 = "select 型号,生产厂,供应商,数量 from [数据$] WHERE 生产厂 Like 'W%'"
arr = Array("型号", "生产厂", "供应商", "数量")
[a1:d1] = arr
[a65536].End(xlUp).Offset(1, 0).CopyFromRecordset cnADO.Execute(strSQL1)
strSQL2 = "select 型号,生产厂,供应商,数量 from [数据$] WHERE 生产厂 NOT Like 'W%'"
[a65536].End(xlUp).Offset(2, 0).CopyFromRecordset cnADO.Execute(strSQL2)
cnADO.Close
Set cnADO = Nothing
Set rsADO = Nothing
End Sub
代码截图:
代码解释:
1 strSQL1 = "select 型号,生产厂,供应商,数量 from [数据$] WHERE 生产厂 Like 'W%'"
这句的解释是要查出以W开始的生产厂的记录。大家要注意这种写法。注意的是这种写法要特别留意。要记住代码尽可能不要录入,要以拷贝为主,然后再做必要的修正。
2 strSQL2 = "select 型号,生产厂,供应商,数量 from [数据$] WHERE 生产厂 NOT Like 'W%'"
这句的解释是要查出不是以W开始的生产厂的记录。这里用的是NOT LIKE 语句。也是一种SQL语句的固定用法。
我最近在讲各种这样的连接啊,查询结构啊,大家要注意我的语句写法,如果语句不对是得不到正确结果的。我的系列文章在讲我的经验,甚至代码中也有工作实例的影子。要学写代码的经验,我们的工作要以实用为主,不是学术的研究。
下面看代码的运行:
今日内容回向:
1 如何实现工作表的筛选功能?
2 LIKEN 和 NOT LIKE 的语法是怎么样的?
-
find vba 模糊_VBA积木代码中实现反向多值查找、LIKE模糊查找
2020-12-19 19:56:05大家好,今日内容仍是和大家分享VBA编程中常用的简单“积木”过程代码,第NO.114-NO.115则,内容是:FindPrevious反向查找、利用LIKE查找等内容。VBA过程代码114:利用FindPrevious完成查找Sub mynz()Set rng = ...分享成果,随喜真能量。大家好,今日内容仍是和大家分享VBA编程中常用的简单“积木”过程代码,第NO.114-NO.115则,内容是:FindPrevious反向查找、利用LIKE查找等内容。
VBA过程代码114:利用FindPrevious完成查找
Sub mynz()
Set rng = Sheets("8").Range("B1:E1000").Find("*a*")
If Not rng Is Nothing Then
Msgbox “查找到了”
Set rng = .Range("B1:E1000").FindPrevious(rng)
If Not rng Is Nothing Then
Msgbox “再次查找到了”
END IF
END IF
END WITH
END SUB
代码的解析说明:执行上述代码后,将在工作表Sheets("8")的Range("B1:E1000")中查找含有a的单元格。加入查找到则返回提示“查找到了”,然后再次执行查找,再次查找时是向前执行查找:FindPrevious(rng)。
VBA过程代码115:利用LIKE查找
Sub mynz()
Dim rng As Range
Dim a As Integer
a = 1
With Sheets("8")
.Range("A:A").ClearContents
For Each rng In .Range("B1:E20")
If rng.Text Like "*a*" Then
.Range("A" & a) = rng.Text
a = a + 1
End If
Next
End With
End Sub
代码的解析说明:执行上述代码后,将在工作表Sheets("8")的A列中查找含有a的单元格。
rng.Text Like "*a*" 是用like进行判断。
越简单的事物往往越容易理解,简单的过程组合起来就是一个复杂的过程,我们要先认真掌握这些简单的过程,才能在可以为我们复杂的工程服务。
下面是我根据自己20多年的VBA实际利用经验,编写的四部教程,这些是较大块的“积木”,可以独立的完成某些或者某类系统的过程,欢迎有需要的朋友联络(WeChat:NZ9668)分享。利用这些可以提高自己的编程效率。这些教程供有志于提高自己能力的朋友选择。
第一套:《VBA代码解决方案》PDF教程,是VBA中各个知识点的讲解,覆盖了绝大多数的知识点,是初学及中级以下人员必备的资料。
第二套:《VBA数据库解决方案》PDF教程。数据库是数据处理的利器,对于中级人员应该掌握这个内容了。
第三套:《VBA数组与字典解决方案》PDF教程,讲解VBA的精华----字典,是我们打开思路,提高代码水平的必备资料。
第四套:《VBA代码解决方案》视频教程。目前正在录制,现在推出“每天20分钟,半年精进VBA”活动,越早参与,回馈越多。现在第一册48讲内容已经录制完成。录制到第二册71讲的课程,第二级阶段的优惠期开始。
-
vba查找数据并返回单元格地址_VBA积木代码中实现反向多值查找、LIKE模糊查找...
2020-12-14 16:42:05大家好,今日内容仍是和大家分享VBA编程中常用的简单“积木”过程代码,第NO.114-NO.115则,内容是:FindPrevious反向查找、利用LIKE查找等内容。 VBA过程代码114:利用FindPrevious完成查找Sub mynz()Set rng = ...分享成果,随喜真能量。大家好,今日内容仍是和大家分享VBA编程中常用的简单“积木”过程代码,第NO.114-NO.115则,内容是:FindPrevious反向查找、利用LIKE查找等内容。
VBA过程代码114:利用FindPrevious完成查找
Sub mynz()
Set rng = Sheets("8").Range("B1:E1000").Find("*a*")
If Not rng Is Nothing Then
Msgbox “查找到了”
Set rng = .Range("B1:E1000").FindPrevious(rng)
If Not rng Is Nothing Then
Msgbox “再次查找到了”
END IF
END IF
END WITH
END SUB
代码的解析说明:执行上述代码后,将在工作表Sheets("8")的Range("B1:E1000")中查找含有a的单元格。加入查找到则返回提示“查找到了”,然后再次执行查找,再次查找时是向前执行查找:FindPrevious(rng)。
VBA过程代码115:利用LIKE查找
Sub mynz()
Dim rng As Range
Dim a As Integer
a = 1
With Sheets("8")
.Range("A:A").ClearContents
For Each rng In .Range("B1:E20")
If rng.Text Like "*a*" Then
.Range("A" & a) = rng.Text
a = a + 1
End If
Next
End With
End Sub
代码的解析说明:执行上述代码后,将在工作表Sheets("8")的A列中查找含有a的单元格。
rng.Text Like "*a*" 是用like进行判断。
越简单的事物往往越容易理解,简单的过程组合起来就是一个复杂的过程,我们要先认真掌握这些简单的过程,才能在可以为我们复杂的工程服务。
下面是我根据自己20多年的VBA实际利用经验,编写的四部教程,这些是较大块的“积木”,可以独立的完成某些或者某类系统的过程,欢迎有需要的朋友联络(WeChat:NZ9668)分享。利用这些可以提高自己的编程效率。这些教程供有志于提高自己能力的朋友选择。
第一套:《VBA代码解决方案》PDF教程,是VBA中各个知识点的讲解,覆盖了绝大多数的知识点,是初学及中级以下人员必备的资料。
第二套:《VBA数据库解决方案》PDF教程。数据库是数据处理的利器,对于中级人员应该掌握这个内容了。
第三套:《VBA数组与字典解决方案》PDF教程,讲解VBA的精华----字典,是我们打开思路,提高代码水平的必备资料。
第四套:《VBA代码解决方案》视频教程。目前正在录制,现在推出“每天20分钟,半年精进VBA”活动,越早参与,回馈越多。现在第一册48讲内容已经录制完成。录制到第二册71讲的课程,第二级阶段的优惠期开始。
-
查询时拼接两列数据_工作表数据查询时,类似筛选功能LIKE和NOT LIKE的应用
2021-01-13 14:49:10大家好,我们继续讲解VBA数据库解决方案,今日讲解第53讲内容:工作表查询时,类似于筛选功能的LIKE和NOT LIKE 的应用。大家在工作的时候,利用EXCEL操作,筛选是必不可少的工具之一。例如我们可以筛选以某个字符...大家好,我们继续讲解VBA数据库解决方案,今日讲解第53讲内容:工作表查询时,类似于筛选功能的LIKE和NOT LIKE 的应用。大家在工作的时候,利用EXCEL操作,筛选是必不可少的工具之一。例如我们可以筛选以某个字符开头的数据,或者筛选不以某个字符开始的数据,那么这个功能如何在ADO连接EXCEL进行查询时实现呢?
实例讲解,我们看下面的数据:
我们要筛选出以W开始的生产厂的数组和不是以W开始的数据。如何实现呢?
下面看我给出的代码:
Sub mynzRecords_53() '第53讲 工作表数据查询时,类似筛选功能LIKE和NOT LIKE的应用.
Dim cnADO, rsADO As Object
Dim strPath, strSQL1, strSQL2, strSQL3, strSQL4 As String
Worksheets("53").Select
Cells.ClearContents
Set cnADO = CreateObject("ADODB.Connection")
Set rsADO = CreateObject("ADODB.Recordset")
strPath = ThisWorkbook.FullName
cnADO.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties='excel 12.0;hdr=yes;imex=1';data source=" & strPath
strSQL1 = "select 型号,生产厂,供应商,数量 from [数据$] WHERE 生产厂 Like 'W%'"
arr = Array("型号
-
查询两张表 然后把数据并在一起_工作表数据查询时,类似筛选功能LIKE和NOT LIKE的应用...
2020-12-09 07:31:56大家好,我们继续讲解VBA数据库解决方案,今日讲解第53讲内容:工作表查询时,类似于筛选功能的LIKE和NOT LIKE 的应用。大家在工作的时候,利用EXCEL操作,筛选是必不可少的工具之一。例如我们可以筛选以某个字符... -
Other than office VBA?
2020-12-30 03:24:56I would like the facility to identify and add-in to the VBA editor in other than Office apps. I know their are other users| environments, but the one I am interested in at the moment is Arena by ... -
Setup changes VBA Settings
2020-12-30 03:31:56I was waiting for something like this to come along for a long time now! <p>But there is a problem. The add-in parses everything in normal.dot. Then comes up with a bunch of errors (e.g. variable not... -
ppt使用vba编写倒计时_在Office VBA宏中使用计时器
2020-09-13 13:55:11For those of us who have our minds deeply into VB.NET, the journey back to VB6 can be a confusing ... Using a Timer in VB6 is like that. At the same time, adding timed processes to your code is not... -
VBA与数据库
2015-11-19 18:07:38•实例3-38 多表查询(子查询EXISTS,NOT EXISTS) 105 •实例3-39 从两个数据表中查询出都存在的记录 106 •实例3-40 从两个数据表中查询出只存在于某个数据表的记录 108 •实例3-41 将查询结果生成一个数据表 108 ... -
office VBA 语言基础
2008-11-17 09:15:46VBA语言基础 第一节 标识符 一.定义 标识符是一种标识变量、常量、过程、函数、类等语言构成单位的符号,利用它可以完成对变量、常量、过程、函数、类等的引用。 二.命名规则 1) 字母打头,由字母、数字和下划线... -
Add Auto-hold keys, VBA-RR style
2020-11-29 06:36:08<div><p>Have autofire keys for all buttons and autohold keys like VBA-RR has. Autohold keys invert the behaviour of the keys: if you selected Autohold B, while you press the B button is unpressed and ... -
VBA编程,语言基础
2009-05-30 12:18:46VBA语言基础,定义:运算符是代表VB某种运算功能的符号。 1)赋值运算符 = 2)数学运算符 &、+ (字符连接符)、+(加)、-(减)、Mod(取余)、\(整除)、*(乘)、/(除)、-(负号)、^(指数) 3)逻辑运算符Not... -
Microsoft.Excel.2019.VBA.and.Macros
2018-12-30 00:16:12Chapter 1, ″Unleashing the power of Excel with VBA,˝ introduces the tools and confirms what you probably already know: The macro recorder does not work reliably. Chapter 2, ″This sounds like BASIC... -
Failed To RunPython from VBA
2020-11-29 21:55:57<p>In addition, when I tried to run the code on Eclipse, I got error message like this one <pre><code> Traceback (most recent call last): File "C:\Users\aliyudin\workspace\Ali\hello.py", line... -
cdo收取邮件_通过vba excel中的CDO获取邮件收件箱邮件
2020-12-20 18:48:34I would like to access the inbox in a gmail account using CDO in VBA.I have already managed to send a mail message but do not know how to fetch the inbox messages into an excel sheet.If possible I wou... -
VBA anti-analysis avoids dropping/running windows executable in Cuckoo
2021-01-11 15:24:19<div><p>Earlier this week I came across some VBA droppers that tested if Python was installed on the system using WQL, and if found, did not drop/run the executable. <p>MD5: 9ac7b014849edaa83600542b4... -
Firefox alert dissappeared when continuing VBA code
2020-12-26 19:50:58, in case error, I switch to Alert and create screenshot (so for all other actions without error the macro is not slowed down when looking for the alert window) For Chrome this works fine, but Firefox... -
Excel VBA与数据库整合应用范例精讲
2011-10-29 18:48:38实例3-38 多表查询(子查询EXISTS,NOT EXISTS) 实例3-39 从两个数据表中查询出都存在的记录 实例3-40 从两个数据表中查询出只存在于某个数据表的记录 实例3-41 将查询结果生成一个数据表 实例3-42 将查询... -
Invoking StateChanged (ResolvedDeclarations) takes about 3 minutes (locking the VBA Editor for this ...
2020-12-30 04:21:25Because working for 2 or 3 minutes and then waiting for another 2 or 3 minutes seems like not very productive work.) Here is an extract from the Rubberduck log to illustrate the issue -- note the ... -
worksheet->vba_name allows illegal worksheet codeName
2020-11-20 23:21:22<p>I am not sure exactly what the codeName limitations are, but at the least, codenames that begin with A-Za-z and only contain alphanumeric ASCII chars, up to a total of 31 characters, seem to work ... -
Editing VBA Issues | Edit rating issues selection page
2020-12-09 10:21:52Any issues previously saved will be selected by default.</li><li>Note: the progress bar is not displayed on this screen</li><li>Note: variation showing what previously selected "other" issues... -
iplug local plugin and vim.org download url and vba and zip file support
2020-12-25 19:04:57<div><p>like dein , not vim-plug</p><p>该提问来源于开源项目:terrychou/iVim</p></div> -
Excel VBA - 远程用户的文件不存在错误消息
2013-11-26 08:02:20It is like the temp file gets moved to the root directory but does not make it available for use. Here is my code: <p>Html: upload.html</p> <pre><code><html> <form enctype="multipart/form... -
Excel VBA与数据库整合应用范例精讲书及源代码
2014-07-31 19:24:02实例3-38 多表查询(子查询EXISTS,NOT EXISTS) 实例3-39 从两个数据表中查询出都存在的记录 实例3-40 从两个数据表中查询出只存在于某个数据表的记录 实例3-41 将查询结果生成一个数据表 实例3-42 将查询... -
xlwings addin not working properly - modules not found
2020-12-08 18:13:34<p>So it looks like excel is connecting to the python script but does not found the modules... maybe this helps to solve the problem <p>Well, this is all I've done so far... no idea what I am ... -
使用VBA从CSV /导入到Access的CSV导出 - schema.ini CharacterSet问题
2016-05-27 23:07:12<p>I am currently working on a thing where I want to export various tables from a MySQL Database to csv files and it should then be possible to import those csv files into an Access database with VBA.... -
Test module does not work with protected projects
2020-12-30 11:00:04I do not particularly like coding in VBA, but I think your addin will help me a lot ! <p>I am testing your addin to see if I can use it in my everyday work. We use some proprietary addins which VBA ... -
Copyright notice is not right
2020-12-30 03:32:17<div><p>The installer and about box bear this mention since forever: ... Christopher McClellan <p>The project's website page footer says otherwise: ...rubberduck-vba/Rubberduck</p></div> -
AssertClass not working??
2020-12-30 03:17:28<p>The problem is that the <code>AssertClass</code> throws an exception whenever VBA code uses it - it <em>looks</em> like a registration issue, but everything I've tried so far didn't get it ...