一次调暗许多变量并在其中使用循环变量?

问题描述 投票:1回答:1

我有很多报告说我从中提取信息然后放入新的字段。我想知道是否有一个很好的方法来构建它,以便根据报告类型,变量变暗以引用我需要的单元格,但内置循环计数器。这是我正在寻找的快速模型:

sub demo()
dim i as long
dim a,b,c,d,e as range

for i = 1 to 100
   if cells(i,1).value2 like "Financials-Q4" Then
     set a = cells(i,21).value2
     set b = cells(i,44).value2
     set c = cells(i,65).value2
  elseif cells(i,1).value2 like "Amor-Q4" Then
     set a = cells(i,100).value2
     set b = cells(i,97).value2
     set c = cells(i,157).value2
     set d = cells(i,89).value2
  end if
next i

 'Start using variables
 for i = 1 to 100
   If a = b Then
      c = "Does not compute"
   Else
      c = "Does compute"
   End if
next i

目前我的代码基本上只是引用每个单独的单元格值,这是非常耗时的清理/更改。

excel vba excel-vba
1个回答
1
投票

应该有用的东西:

Sub Demo()
    Dim i As Long
    Dim a As Range
    Dim b As Range
    Dim c As Range, d As Range, e As Range

    For i = 1 To 100
        If Cells(i, 1).Value2 = "Financials-Q4" Then
            Set a = Cells(i, 24).Value2
            Set b = Cells(i, 24).Value2
            Set c = Cells(i, 24).Value2
        ElseIf Cells(i, 1).Value2 = "Amor-Q4" Then
            Set a = Cells(i, 100).Value2
            Set b = Cells(i, 97).Value2
            Set c = Cells(i, 157).Value2
            Set d = Cells(i, 89).Value2
        End If
    Next
End Sub

假设你不需要Like,但只是=,因为你没有使用和*?标志使用Like

这是使用Like的一些示例,结果在即时窗口中(source):

?"Vito6" Like "V?to6"
True
?"Vito6" Like "Vito#"
True
?"Vito6" Like "V*6"
True
?"Vito6" Like "Vit[a-z]6"
True
?"Vito6" Like "Vit[A-Z]6"
False
?"Vito6" Like "Vit[!A-Z]6"
True
?"12 34" Like "## ##"
True
?"12 34" Like "1[0-9] [0-9]4"
True

关于使用qazxsw poi而不是qazxsw poi - qazxsw poi

© www.soinside.com 2019 - 2024. All rights reserved.