循环遍历选定范围内的每个单元格 [英] Loop through each cell in a selected range

查看:40
本文介绍了循环遍历选定范围内的每个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Visual Studio 中创建一个程序,用于遍历未命名为 Excel 的范围.例如,用户选择A1"&A5"A94".在我的程序中,我可以将 range.count 属性视为值 3,但是如果我进行 Do Loop,我无法获取所选范围的第二个单元格值,但我找到了A2".我的程序是这样的:

I'm trying to make a procedure in Visual studio for looping through a range not named into Excel. For example the user selects "A1" & "A5" "A94". In my procedure I can see the range.count prorperty as value 3, but if I make a Do Loop I can't take the second cell value of the selected range but I find "A2". My procedure is this:

    Dim counter As Integer = 1
    Dim ActiveCell As Excel.Range

    Do
        ActiveCell = SelectedRange.Item(counter)
        ActiveCell.Select()
        MessageBox.Show(ActiveCell.Value2.ToString)
        counter = counter + 1
    Loop While counter <= SelectedRange.Count
    SelectedRange.Select()

推荐答案

这是一个简单的示例 sub,它将每个选定单元格的值设置为 1:

Here is a simple example sub which sets the value of every selected cell to 1:

Sub EditSelection()
    Dim rng As Range

    For Each rng In Selection
        rng.Value = 1
    Next rng
End Sub

如果在那之后你还在挣扎,请告诉我

Let me know if you're still struggling after that

这篇关于循环遍历选定范围内的每个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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