【问题标题】:How to offset 2 cells of a range in VBA?如何在 VBA 中偏移一个范围的 2 个单元格?
【发布时间】:2012-10-26 12:21:46
【问题描述】:

我正在编写一个宏来检查一个值是否为真,如果是,则获取该行的第一个单元格并存储它。例如:

|191|c:users\public\test.dbf |True |
|192|c:users\public\test2.dbf |假|
|193|c:users\public\test2.dbf |False|

我想遍历这个数组,当一个值为 True 时,我需要存储 191 和 c:users\public\test.dbf(对于这个例子;要存储的实际值将取决于行)。

这是我的代码:

For Each strPath In Worksheets("OPTIONS").Range("F2:H6")
    newChemin = strPath
    If strPath <> True And strPath <> False Then Chemin = strPath

    If strPath.Value = "True" Then
        For Each Cell In Worksheets("OPTIONS").Range("F2:F6")
            If Cell = Chemin Then
                strChemin = Chemin
                Exit For
            End If
        Next Cell

        Set wsSheet1 = wbBook.Worksheets("DB" & strChemin)        
        If strPath.Value = "TRUE" Then
            If FichierExiste(Chemin) Then
                Line = Line + 1
            Else
                Chemin = Worksheets("OPTIONS").Cells(7, 7).Value & "\" & strChemin & "\ancretbd.mdb"
                If FichierExiste(Chemin) Then
                    '* Appel de la Method mod_Importation
                    Call Importation_Totale
                Else
                    MsgBox "Desolé, le fichié demandé n'existe pas !"
                End If
            End If
        End If
    End If
Next strPath

【问题讨论】:

  • 您需要执行多少次?
  • 请不要用 str 编码范围对象strPath,因为它会混淆而不是澄清。将其更改为(例如)rPath 以获得更一般的清晰度。
  • 因此,作为参考 - “Chemin”(法语)表示“Path”(英语),“FichierExiste”表示“FileExists”。您能否告诉我们您如何存储要保存的值?为什么要对strPath.Value = "True" 进行多次检查? Line 变量有什么用?

标签: vba excel


【解决方案1】:

由于法语 (??) 变量,很难理解这段代码在做什么。 如果你能描述一下什么是 chemin,也许更容易理解你所指的行的哪一部分。

但是,由于你的strPath实际上是一个Range,你可以简单地引用它的偏移量!

For Each strPath In Worksheets("OPTIONS").Range("F2:H6")
    If strPath.Value = "True" Then
        Set wsSheet1 = wbBook.Worksheets("DB" & strPath.Offset(0, -1).Value
        If FichierExiste(strPath.Offset(0, -2).Value) Then
        Line = Line + 1
        Else
            Chemin = Worksheets("OPTIONS").Cells(7, 7).Value & "\" & strChemin & "\ancretbd.mdb"
            If FichierExiste(Chemin) Then
                 '* Appel de la Method mod_Importation
                 Call Importation_Totale
            Else
                MsgBox "Desolé, le fichié demandé n'existe pas !"
            End If
        End If
    End If
Next strPath

这可能不是 100% 正确(因为我不清楚代码的功能),并且偏移量可能是错误的(上例中的 -1 和 -2)。但我希望这能给你一个解决问题的想法!

【讨论】:

    【解决方案2】:

    在我看来,H 列是包含 TRUE 或 False 的列。在这种情况下,如果您只想检查此列并相应地更新列 F 和 G,那么这个简化的代码可以为您完成这项工作:

    For Each strPath In Worksheets("OPTIONS").Range("H2:H6")
         If strPath.Value = "True" Then
             Range("F" & strPath.Row).Value = "191"
             Range("G" & strPath.Row).Value = "c:users\public\test.dbf"
    
         End If
    Next
    

    【讨论】:

      猜你喜欢
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      相关资源
      最近更新 更多