简体   繁体   English

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

[英]Dimming many variables at once and using loop variable inside of them?

I have a number of reports that I am pulling information from and then placing into new fields.我有许多报告,我正在从中提取信息,然后将其放入新字段中。 I'm wondering if there's a good way to strucutre it so that, depending on the report type, variables are dimmed to reference the cells that I need, but with the loop counter built-in.我想知道是否有一种很好的方法来构建它,以便根据报告类型,变量变暗以引用我需要的单元格,但内置循环计数器。 Here is a quick mock-up of what I'm looking for:这是我正在寻找的快速模型:

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

Currently my code is basically just referencing each individual cell value which is extremely time consuming to clean up / change around.目前我的代码基本上只是引用每个单独的单元格值,清理/更改非常耗时。

Something that should work:应该工作的东西:

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

Assuming that you do not need Like , but simply = , because you have not used and * or ?假设您不需要Like ,而只需要= ,因为您还没有使用 and *? signs to use the Like .标志使用Like

This is some example of the usage of Like with the results in the immediate window ( source ):这是将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

Concerning the usage of Integer instead of Long - Why Use Integer Instead of Long?关于使用Integer而不是Long - 为什么使用 Integer 而不是 Long?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM