如何在列中的单元格中循环,以及如何在Excel VBA中查找列表的最新日期 [英] How to To Loop through cells in a column, and to find the latest date of the list in Excel VBA

查看:108
本文介绍了如何在列中的单元格中循环,以及如何在Excel VBA中查找列表的最新日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要遍历 A列的实际工作,该值在一定范围内具有相同的值,在该范围内,必须在 B列中用注释检查最新日期,然后在 C列中打印带有日期的注释,请帮助我找到该问题的解决方案..或者为找到该问题的解决方案提供指南...请查看屏幕截图以获取明确的信息.先进专家

Actual work to loop through the Column A which has same value of with certain range, with that range have to check the latest date with the comment in Column B and print the comment with date in Column C kindly help me to find the solution for this problem.. Or Guide to find the solution for this problem... Kindly check the Screenshot for clear information.. Thanks in Advance Experts

推荐答案

循环可能是所有计算机编程主题中最强大的功能.请参阅下面的示例,以获取有关如何实现目标的一些想法.

Loops are probably the most powerful things is all computer programming topics. Please see the examples below for some ideas of how to achieve your goals.

对于范围中的每个单元格每个示例的数据

For Each cell in a range data for each example

在Excel中对VBA进行编程时,您要做的最常见的事情之一就是遍历指定范围内的一组单元格,如下面的示例所示,它将数据"工作表上的4个单元格的地址和值打印到立即窗口:$ B $ 2:a,$ C $ 2:b,$ B $ 3:1,$ C $ 3:2.

One of the most common things you will do when programming VBA in Excel is looping though a collection of cells in a specified range, as in the example below which prints the Address and Value of 4 cells on the 'Data' worksheet to the immediate window: $B$2:a, $C$2:b, $B$3:1, $C$3:2.

Dim rng As Range: Set rng = Application.Range("Data!B2:C3")
Dim cel As Range
For Each cel In rng.Cells
    With cel
        Debug.Print .Address & ":" & .Value
    End With
Next cel

在连续的单元格中循环每个示例的数据

Loop through the cells in a row data for each example

下面的代码显示了如何循环使用RowIndex:= 2的行中的单元格.将其应用于右侧工作表中的数据后,将返回1、2.从中我们可以看到行是从rng的起点算起的,因此该行在工作表上为3,在rng内为2.此外,仅会拍摄设置范围rng内的单元格.

The code below shows how to loop through the cells in the row with RowIndex:=2. Applied to the data in the sheet on the right this will return 1, 2. From this we see that rows are counted from the starting point of rng, so the row is 3 on the worksheet, 2 inside rng. Also, only cells inside the set range rng are taken.

Dim rng As Range: Set rng = Application.Range("Data!B2:C3")
Dim i As Integer
For i = 1 To rng.Rows.Count
    Debug.Print rng.Cells(RowIndex:=2, ColumnIndex:=i).Value
Next

在列中的单元格中循环

下面的代码显示了如何遍历ColumnIndex:= B的列中的单元格.将其应用于右侧工作表中的数据后,将返回a,1、2.由此可见,列是从rng的起点算起的,因此该列在工作表上为C,在rng内为B.此外,仅会拍摄设置范围rng内的单元格.

The code below shows how to loop through the cells in the column with ColumnIndex:=B. Applied to the data in the sheet on the right this will return a, 1, 2. From this we see that columns are counted from the starting point of rng, so the column is C on the worksheet, B inside rng. Also, only cells inside the set range rng are taken.

Dim rng As Range: Set rng = 
Dim i As Integer
For i = 1 To rng.Rows.Count
    Debug.Print rng.Cells(RowIndex:=i, ColumnIndex:="B").Value
Next

浏览范围内的列

下面的代码显示了如何遍历范围B2:C4中的列.应用于右边工作表中的数据后,将返回2、3.由此可见,列是从工作表的起点算起的.

The code below shows how to loop through the columns in the Range B2:C4. Applied to the data in the sheet on the right this will return 2, 3. From this we see that columns are counted from the starting point of the worksheet.

Dim rng As Range: Set rng = Application.Range("B2:C4")
Dim col As Range
For Each col In rng.Columns
    Debug.Print col.Column
Next col

浏览范围内的行

下面的代码显示了如何遍历范围B2:C4中的行.应用于右侧工作表中的数据后,将返回2、3、4.由此可见,行是从工作表的起点算起的.

The code below shows how to loop through the rows in the Range B2:C4. Applied to the data in the sheet on the right this will return 2, 3, 4. From this we see that rows are counted from the starting point of the worksheet.

Dim rng As Range: Set rng = Application.Range("B2:C4")
Dim col As Range
For Each col In rng.Rows
    Debug.Print col.Row
Next col

遍历范围内的区域每个2个区域的数据示例

Loop through the areas in a range data for each 2 areas example

通常,我们假定范围具有矩形形状,但这不是必须的.右边的示例表显示了一个包含两个区域的选择:Selection.Address返回$ B $ 2:$ C $ 3,$ F $ 2:$ F $ 3.由于相交"方法或其他原因,也可能发生这种情况.要分别处理这两个范围,可以从Areas集合中进行选择:

Often we assume a range to have a rectangular shape, but this need not be the case. The example sheet on the right shows a selection containing two areas: Selection.Address returns $B$2:$C$3,$F$2:$F$3. Such a situation may also occur as a result of the Intersect method, or other causes. To handle the two ranges separately can can pick then from the Areas collection:

Dim rng As Range: Set rng = Application.Selection
Dim rngArea As Range
For Each rngArea In rng.Areas
    Debug.Print rngArea.Address
Next rngArea

这篇关于如何在列中的单元格中循环,以及如何在Excel VBA中查找列表的最新日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆