Excel教程:实用的VBA代码,赶紧保藏!
<span style="color: black;"><strong style="color: blue;">回复[</strong><strong style="color: blue;">目录</strong><strong style="color: blue;">]学习113篇Excel教程</strong></span><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/k6kCsib3eJMyNDmBGzGRtxMH3iaH2Y4ymrult7ic3Qxz8fZib5LJF78otFT256MRPNwoSMUx1E4frA4Lsiaj2P6XwSA/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><strong style="color: blue;">全套Excel视频教程,扫码观看!</strong></span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">编按: </span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">哈喽,<span style="color: black;">大众</span>好!昨天和<span style="color: black;">大众</span>承诺,“在看”数超过30,<span style="color: black;">博主</span>明天就继续爆肝更新VBA!感谢<span style="color: black;">大众</span>的支持,没想到昨天的<span style="color: black;">文案</span>“在看”数<span style="color: black;">已然</span>快100了!既然如此,那<span style="color: black;">博主</span>自然<span style="color: black;">亦</span><span style="color: black;">不可</span>辜负<span style="color: black;">大众</span>的信任,今天<span style="color: black;">咱们</span>继续学习VBA!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【前言】</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上一篇文章中,作者E图表述给<span style="color: black;">大众</span>分享了关于RANGE对象引用的<span style="color: black;">办法</span>。<span style="color: black;">经过</span>回馈的信息来看,和作者想的<span style="color: black;">同样</span>——确实有<span style="color: black;">非常多</span>的<span style="color: black;">朋友</span>,<span style="color: black;">晓得</span>并且想学习EXCEL VBA知识。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那就让<span style="color: black;">咱们</span>继续上次的内容,依然来讲RANGE。<span style="color: black;">亦</span>许<span style="color: black;">朋友</span>们会问,<span style="color: black;">为何</span>还是RANGE呢?<span style="color: black;">由于</span>在EXCEL中,只要是数据处理的问题,最后都会归结为单元格的引用和操作。即便是操作其他的对象,<span style="color: black;">最后</span><span style="color: black;">亦</span>会将内存中计算<span style="color: black;">或</span>提取的数据结果,赋值到单元格中来,<span style="color: black;">因此</span>说<span style="color: black;">基本</span>永远是<span style="color: black;">基本</span>,根基稳了,高级操作<span style="color: black;">才可</span>压的住。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">单元格的偏移</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在工作表函数中,有一个OFFSET函数,作者E图表述之前<span style="color: black;">亦</span>专门写过它的<span style="color: black;">文案</span>《<a style="color: black;">月薪上万必学10大excel函数之十:它身负5大绝技,是动态统</a>计之王<span style="color: black;">》,不<span style="color: black;">熟练</span>这个函数的<span style="color: black;">朋友</span><span style="color: black;">能够</span><span style="color: black;">瞧瞧</span>,<span style="color: black;">由于</span><span style="color: black;">倘若</span><span style="color: black;">朋友</span>们理</span>解了这个函数的原理,<span style="color: black;">那样</span>对下面的两个RANGE属性,将有很大的<span style="color: black;">帮忙</span>。</span></p><strong style="color: blue;"><span style="color: black;">温馨提示:加入下面QQ群</span></strong><strong style="color: blue;"><span style="color: black;">:1043683754,</span></strong><strong style="color: blue;"><span style="color: black;">下载教程配套的课件练习操作。</span></strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNBD1clsic64kHta8l6SGxJIhPxpZp17ru4ozaL4aTduCPosePO2x7keQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.RANGE.OFFSET</span></strong><strong style="color: blue;"><span style="color: black;">属性</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">大众</span><span style="color: black;">能够</span>看出OFFSET的写法都是<span style="color: black;">同样</span>的,在工作表函数中,OFFSET函数是既有偏移的功能,<span style="color: black;">亦</span>有指定引用范围是几行几列的功能。可是在VBA中RANGE.OFFSET属性<span style="color: black;">仅有</span>偏移的<span style="color: black;">功效</span>。如下代码:</span></p><span style="color: black;">Sub 求和()</span><span style="color: black;"> Dim rg As Range</span><span style="color: black;"> Set rg = Range("C2")</span><span style="color: black;">rg.Value = rg.Offset(0, -2) + rg.Offset(0, -1)</span><span style="color: black;">End Sub</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNSNdlqnuOsacE2icFUSvW2YQSvzsoG6VznJ4BJ4mTibdjJWQEekicmZWdQ/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【代码解析】:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line1</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">定义rg为单元格变量;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line2</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">将单元格<span style="color: black;">位置</span>C2赋值给rg变量;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line3</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">rg向左偏移两个位置的单元格+rg向左偏移一个位置的单元格,最后将计算结果赋值给rg变量。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span>上面的代码解析,<span style="color: black;">咱们</span><span style="color: black;">能够</span>看出OFFSET属性的<span style="color: black;">功效</span>,其<span style="color: black;">含义<span style="color: black;">便是</span><span style="color: black;">按照</span>基点单元格,<span style="color: black;">根据</span>参数进行偏移,以偏移后的位置<span style="color: black;">做为</span>新的基点。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNchctv88RJhSS8jTFOJCyb6sJNn5H8ziabF8v42ibholicOYoibtc2oH8dQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.RANGE.RESIZE</span></strong><strong style="color: blue;"><span style="color: black;">属性</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">相<span style="color: black;">针对</span>一个基点的位置,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>VBA来<span style="color: black;">选取</span>引用区域的面积有多大,<span style="color: black;">这儿</span>就需要<span style="color: black;">运用</span>RANGE.RESIZE属性了,如下代码:</span></p><span style="color: black;">Sub 统计()</span><span style="color: black;">Range("B5") = Application.WorksheetFunction.Sum(Range("B2").Resize(1, 3))</span><span style="color: black;"> Range("B6") = Application.WorksheetFunction.Average(Range("B2").Resize(1, 3))</span><span style="color: black;">Range("B7") = Application.WorksheetFunction.Max(Range("B2").Resize(1, 3))</span><span style="color: black;"> Range("B8") = Application.WorksheetFunction.Min(Range("B2").Resize(1, 3))</span><span style="color: black;">End Sub</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNHtsWsn2HLHTclwymbiacA8ZfpgBcQXMNCNvaCGjAKdxdBlLGNYnSCsA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【代码解析】:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line1</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">调用工作表函数SUM,对基于单元格<span style="color: black;">位置</span>B2<span style="color: black;">起始</span>的1行3列的区域进行求和运算,并将结果赋值给单元格B5;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line2</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">调用工作表函数AVERAGE,原理同Line1,对该区域进行平均值运算,并将结果赋值给单元格B6;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line3</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">调用工作表函数MAX,原理同Line1,对该区域进行最大值运算,并将结果赋值给单元格B7;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line4</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">调用工作表函数MIN,原理同Line1,对该区域进行最小值运算,并将结果赋值给单元格B8;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">尤其</span>提示</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Application.WorksheetFunction</span><span style="color: black;">这句代码<span style="color: black;">便是</span>调用工作表函数的语句,后面接写工作表函数,与<span style="color: black;">平常</span><span style="color: black;">运用</span>函数的<span style="color: black;">办法</span><span style="color: black;">同样</span>,只需记得单元格对象的表达方式,<span style="color: black;">写成RANGE<span style="color: black;">或</span>CELLS<span style="color: black;">就可</span>。这个语句<span style="color: black;">咱们</span>在前一篇<span style="color: black;">文案</span>《<a style="color: black;">花了</a>6小时,仅为你写出一篇能够学懂的VBA教程!》</span>中<span style="color: black;">亦</span><span style="color: black;">说到</span>过。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span>上面的代码解析,<span style="color: black;">咱们</span><span style="color: black;">能够</span>看出RESIZE属性的<span style="color: black;">功效</span>,其<span style="color: black;">含义<span style="color: black;">便是</span><span style="color: black;">按照</span>基点单元格,<span style="color: black;">根据</span>参数进行扩展,以扩展后的区域<span style="color: black;">做为</span>新的操作对象。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNvyJYRzusBaLvfhc8AMKSHxaZibMCPgydaj2WPsCqIjO4LtPX2RbopJA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">小结</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">RANGE的OFFSET属性和RESIZE属性的结合,和工作表函数OFFSET的功能是<span style="color: black;">同样</span>的,这在以后<span style="color: black;">咱们</span>讲到数组、字典时,都会有<span style="color: black;">非常多</span>的应用,<span style="color: black;">期盼</span><span style="color: black;">朋友</span>们能够在<span style="color: black;">文案</span>中有所<span style="color: black;">把握</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">“首行、首列”与“末行、末列”</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在本小节中,作者E图表述将带领<span style="color: black;">大众</span><span style="color: black;">一块</span>学习在<span style="color: black;">运用</span>EXCEL VBA过程中最频繁的语句之一,“<span style="color: black;">查询</span>数据源的起末范围,以及引出行列号”。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在之前的学习中,<span style="color: black;">咱们</span><span style="color: black;">已然</span>说过,单元格的操作将是<span style="color: black;">朋友</span>们日后<span style="color: black;">运用</span>EXCEL VBA中操作最多的对象(<span style="color: black;">无</span>之一),<span style="color: black;">因此</span><span style="color: black;">针对</span>引用一个单元格区域,<span style="color: black;">晓得</span>这个区域的起末范围,<span style="color: black;">针对</span>快速写出对应的引用表达式是<span style="color: black;">非常</span>关键的。例如下面的数据源:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNug7ePbRcVKRGvzKmOBKY2lT1k29PwtNPicsTBuvurvnmWKPzgncSoZA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这般</span>的一个数据源,有几行几列呢?很简单,数一数,15行8列。<span style="color: black;">然则</span><span style="color: black;">电脑不会像<span style="color: black;">咱们</span><span style="color: black;">这般</span>“数”,计算机的任何操作都是基于人工赋予的指令。</span><span style="color: black;">因此</span>它需要<span style="color: black;">咱们</span>写出EXCEL<span style="color: black;">能够</span>识别的语言,<span style="color: black;">才可</span>执行和<span style="color: black;">咱们</span><span style="color: black;">同样</span>“数”的过程。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.RANGE.END</span></strong><strong style="color: blue;"><span style="color: black;">属性</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>都<span style="color: black;">晓得</span>一个小技巧,<span style="color: black;">便是</span>在一个工作表数据源范围内,选中某一个单元格,分别按:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">CTRL+←,能找到连续区域的左边界;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">CTRL+→,能找到连续区域的右边界;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">CTRL+↑,能找到连续区域的上边界;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">CTRL+↓,能找到连续区域的下边界。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在VBA中有<span style="color: black;">一样</span>效果的语句,<span style="color: black;">便是</span>RANGE.END属性,对应的规律<span style="color: black;">亦</span>是相当的易懂易记,不信你看下表:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNkUXlpXTpmmImKiceVl4iaXJxVSqKvrlmafFjFtDxNv4PibRG9InRDRZsA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">照片</span>有误end3<span style="color: black;">暗示</span>↑,end4<span style="color: black;">暗示</span>↓。举个例子看一下,数据源如下:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNvXicGB7CH4EKdGMn0O6CW5cszpvGNNhHiaYuJRIutqncWeORC6H6aXKA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为了更好地说明代码的应用,作者E图表述去掉了行号和列号的标题栏,用VBA来确定这个行列的起止范围,代码如下:</span></p><span style="color: black;">Sub 末行末列()</span><span style="color: black;"> With Sheets("范围确认")</span><span style="color: black;"> a = .Range("A100000").End(xlUp).Row</span><span style="color: black;"> b = .Range("AX1").End(1).Column</span><span style="color: black;"> .Cells(a + 2, b - 1) = "末行号是:" & a</span><span style="color: black;">.Cells(a + 3, b - 1) = "末列号是:" & b</span><span style="color: black;"> End With</span><span style="color: black;">End Sub</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNF1Dm5oukUbz0q8LuQibeGawtrppyoOBWY4hBViaPELuAUoVuo19FB4PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【代码解析】:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line1</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">WITH语句,提取代码中相同的父级对象(<span style="color: black;">Sheets("范围确认")</span>),<span style="color: black;">做为</span>公共从属关系;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line2</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">以A100000单元格为基点,End(xlUp)向上取连续区域的边界,用Row属性<span style="color: black;">表示</span>此边界单元格的行号,并赋值给变量a;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line3</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">以AX1单元格为基点,End(1)向左取连续区域的边界,用Column属性<span style="color: black;">表示</span>此边界单元格的列号,并赋值给变量b;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line4</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">Cells(a+2,b-1),<span style="color: black;">咱们</span>上次<span style="color: black;">已然</span>说过<span style="color: black;">怎样</span>用CELLS表达单元格<span style="color: black;">位置</span>,<span style="color: black;">那样</span>把末行下移两行,末列左移一列,<span style="color: black;">做为</span>输入单元格,输入字符串,<span style="color: black;">这儿</span>的&和工作表中的&是一样的效果;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line5</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">原理同Line4;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line6</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">WITH语句的结束语句。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这段代码中只写出了End(xlUp)和End(xlLeft)的用法,<span style="color: black;">大众</span><span style="color: black;">能够</span>尝试着写写其他的效果。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">小思考</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.<span style="color: black;">为何</span>我的单元格基点,用了“A100000”和“AX1”?</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.<span style="color: black;">为何</span>网上有的代码会用A65000这个单元格行号?</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.<span style="color: black;">倘若</span>要找到首行<span style="color: black;">或</span>首列,是不是从末行再End(xlUp)<span style="color: black;">便是</span>首行,<span style="color: black;">或</span>从数据中部的某个单元格End(xlUp)<span style="color: black;">亦</span>是首行;首列同理?</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你还<span style="color: black;">无</span>思路,就想想CTRL+箭头,你是从哪个单元格<span style="color: black;">做为</span>基点的。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">单元格的“行”和“列”</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">行列交叉,整齐有序,纵横间<span style="color: black;">便是</span><span style="color: black;">咱们</span>的电子表格,<span style="color: black;">那样</span>“既要纵横,岂能不谈行列”。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span>上面的代码,<span style="color: black;">咱们</span>应该<span style="color: black;">已然</span><span style="color: black;">发掘</span>RANGE.ROW和RANGE.COLUMN两个属性,分别是<span style="color: black;">表率</span>指定单元格的“行号”和“列号”,这个记起来应该<span style="color: black;">亦</span>不难,毕竟在工作表函数中,<span style="color: black;">亦</span>有ROW函数和COLUMN函数。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那<span style="color: black;">朋友</span>们可能会说,工作表函数中<span style="color: black;">亦</span>有ROWS和COLUMNS函数,机智如你,是的,VBA中<span style="color: black;">亦</span>有 RANGE.ROWS和RANGE.COLUMNS属性。<span style="color: black;">那样</span>结合这些属性,<span style="color: black;">咱们</span><span style="color: black;">一块</span>举一个案例看一下:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNvOGO5M0lpFvWtyqDHfw5QPs0dqX2VwXuvVQq6mLFrcmOt0G3WDz3icw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【Step1】:新建一个工作表,命名为“RC属性”;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【Step2】:在工程窗口中,双击“RC属性”工作表对象,在通用菜单栏中<span style="color: black;">选取</span>Worksheet;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【Step3】:在右边的菜单栏中<span style="color: black;">选取</span>SelectionChange事件(<span style="color: black;">选择</span>改变时,触发代码);</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【Step4】:输入代码如下:</span></p><span style="color: black;">Private Sub Worksheet_SelectionChange(ByVal Target As Range)</span><span style="color: black;"> If Target.Row <= 10 And Target.Column = 1 Then</span><span style="color: black;">a = Target.Row</span><span style="color: black;"> Range(Cells(1, 3), Cells(a, 3 + a - 1)).Select</span><span style="color: black;"> Set Rng = Selection.Rows</span><span style="color: black;"> MsgBox Rng.Address</span><span style="color: black;"> End If</span><span style="color: black;">End Sub</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/k6kCsib3eJMwhTOkH3Peu5jf8j1zyYJKNkNMNY3o6zf44RRnGHg86b6qeXmDuTDibXTb3ibCMJibaPOt5ZibYl0RnRQ/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【代码解析】:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line1</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">判断当被选中对象的行号<span style="color: black;">少于</span>等于10,并且列号等于1的时候;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line2</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;">将被选中的单元格行号赋值给一个变量a;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line3</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;"><span style="color: black;">运用</span>Range(cells,cells)的表达式,构建一个<span style="color: black;">按照</span>备选单元格行号,列出的单元格区域。<span style="color: black;">例如</span>:<span style="color: black;">选取</span>第3行,就构建以C1单元格为左上角的3*3单元格矩阵,并且选中这个区域;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line4</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;"><span style="color: black;">运用</span>ROWS属性,将SELECTION(被选中的单元格)中<span style="color: black;">触及</span>的单元格赋值给Rng变量;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Line5</span></strong><strong style="color: blue;"><span style="color: black;">:</span></strong><span style="color: black;"><span style="color: black;">运用</span>MSGBOX函数,<span style="color: black;">表示</span>Rng.ADDRESS(<span style="color: black;">位置</span>)属性信息框。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【编后语】</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">今天的信息量又是不少(我从来不写单一的内容),<span style="color: black;">每日</span>进步一点点,忘了就回来再<span style="color: black;">瞧瞧</span>,持之以恒的前提下,更应该明白“天道酬勤”的道理。“多看、多练、多总结”永远是成功者的不二法门。</span></p><strong style="color: blue;">扫二维码</strong><strong style="color: blue;">免费学Excel等视频</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/k6kCsib3eJMzLMNY3tmydYrMXwms5VVgIyTvkIdB7lmxzeuYFk4aMNZIthiaqNe6b3on1No13UPp6T3jNPWDEVOA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><img src="https://mmbiz.qpic.cn/mmbiz_png/b96CibCt70iabwjyojLhA03PtxUnkNPREnYMIXQoTgtW6libdMibQVNic3eZhU08VyWfSQWdI82zIYSzSTq4Y3YV9nQ/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Excel教程<span style="color: black;">关联</span><span style="color: black;">举荐</span></strong></p><a style="color: black;"><span style="color: black;">花了6小时,仅为你写出一篇能够学懂的VBA教程!</span></a><a style="color: black;"><span style="color: black;">Excel教程:19秒,搞定16000行Excel数据自动分组编号!</span></a><a style="color: black;"><span style="color: black;">再见Ctrl+C!提取1000个文件名,用它10秒都嫌多!【Excel教程】</span></a><strong style="color: blue;"><span style="color: black;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">让工作提速百倍的「Excel极速贯通班」</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">↓ 点击<span style="color: black;"><strong style="color: blue;"><span style="color: black;">阅读原文</span></strong></span>,可直接购买。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">倘若</span>对你有<span style="color: black;">帮忙</span>,点下“在看”呦</span><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></p>
回顾历史,我们感慨万千;放眼未来,我们信心百倍。
页:
[1]