VBA,语法,将单元格范围组合在一起以获取公共变量

Posted

技术标签:

【中文标题】VBA,语法,将单元格范围组合在一起以获取公共变量【英文标题】:VBA, syntax, group range of cells together for common variable 【发布时间】:2013-02-06 21:19:24 【问题描述】:

我正在编写一个小脚本(如下)。该脚本遍历数据集上的行,并根据 2 个选择案例语句的某些意外情况将 1 或 0 放置在单元格中。它工作得很好,但我想知道是否有办法将单元格范围分组在一起,在其中放置一个 0。

Sub compVal()

Dim WB As Workbook  'an object of type workbook
Dim WS1 As Worksheet ' objects of type worksheet

Set WB = ActiveWorkbook ' reference WB to the current Workbook
Set WS1 = Worksheets("Sheet1") 'Reference to Sheet 1 of the current workbook

'loop through sheet1's rows
Dim i As Integer

For i = 2 To WS1.UsedRange.Rows.Count

   Select Case WS1.Cells(i, 1).Value 'first cell from row i

      Case "Target"

         Select Case WS1.Cells(i, 2).Value

            Case 1

               WS1.Cells(i, 3).Value = 1
               WS1.Cells(i, 4).Value = 0
               WS1.Cells(i, 5).Value = 0
               WS1.Cells(i, 6).Value = 0

            Case 2

               WS1.Cells(i, 3).Value = 0
               WS1.Cells(i, 4).Value = 0
               WS1.Cells(i, 5).Value = 1
               WS1.Cells(i, 6).Value = 0

         End Select

      Case "NonTarget"

         Select Case WS1.Cells(i, 2).Value

            Case 1

               WS1.Cells(i, 3).Value = 0
               WS1.Cells(i, 4).Value = 1
               WS1.Cells(i, 5).Value = 0
               WS1.Cells(i, 6).Value = 0

            Case 2

               WS1.Cells(i, 3).Value = 0
               WS1.Cells(i, 4).Value = 0
               WS1.Cells(i, 5).Value = 0
               WS1.Cells(i, 6).Value = 1
         End Select

      End Select
Next i

结束子

【问题讨论】:

将它们组合在一起是什么意思? 您可以通过使用(例如)WS1.Cells(i, 3).Resize(1, 4).Value = Array(1, 0, 0, 0) 来分配您的单元格值来整合您的代码。 【参考方案1】:

这是代码重用的一个很好的例子:

Sub compVal()

Dim WB As Workbook  'an object of type workbook
Dim WS1 As Worksheet ' objects of type worksheet

Set WB = ActiveWorkbook ' reference WB to the current Workbook
Set WS1 = Worksheets("Sheet1") 'Reference to Sheet 1 of the current workbook

'loop through sheet1's rows
Dim i As Long ' USE LONG FOR CELL REFERENCES, THERE ARE A LOT OF ROWS POSSIBLE : )

For i = 2 To WS1.UsedRange.Rows.Count

   Select Case WS1.Cells(i, 1).Value 'first cell from row i

      Case "Target"

         Select Case WS1.Cells(i, 2).Value

            Case 1

               AddColumns WS1, i, 1, 0, 0, 0

            Case 2

               AddColumns WS1, i, 0, 0, 1, 0

         End Select

      Case "NonTarget"

         Select Case WS1.Cells(i, 2).Value

            Case 1

               AddColumns WS1, i, 0, 1, 0, 0

            Case 2

               AddColumns WS1, i, 0, 0, 0, 1

         End Select

      End Select
Next i
End Sub

Sub AddColumns(WS As Worksheet, i As Long, c As Variant, d As Variant, e As Variant, f As Variant)
    WS.Cells(i, 3).Resize(1, 4).Value = Array(c, d, e, f)

End Sub

还可以引入其他效率,例如将四个参数 c、d、e 和 f 替换为单个整数,其中

0  = 0,0,0,0
1  = 1,0,0,0
2  = 0,1,0,0
...
15 = 1,1,1,1

【讨论】:

请注意,您应该使用Dim i As Long,因为如果您有很多行,则 Int 是不够的 太好了,谢谢大家的提醒。在编写脚本方面,我仍然很年轻,所以任何花絮都会有很大帮助。再次感谢大家

以上是关于VBA,语法,将单元格范围组合在一起以获取公共变量的主要内容,如果未能解决你的问题,请参考以下文章

VBA excel Target.Address =单元格范围

VBA TextBox将每行的值传递给单元格范围

跨多个工作表的 VBA 宏

将图表从单元格范围更改为命名范围

Vba 代码在单元格范围内复制特定值

Excel VBA函数在单元格范围内查找值