vba基于两列设置动态范围

时间:2015-03-20 19:16:51

标签: vba dynamic range

我正在尝试根据两列设置动态范围,我的标准是......

  1. 我有两个名为" Name"和"电话"
  2. 专栏"名称"总是在"电话"
  3. 的左侧

    我希望获得的范围是,

    1. 查找专栏"电话"
    2. 在列"电话"中设置范围其中第一个非空行和最后一行 在列"名称"
    3. 到目前为止,我能找到专栏" Phone"并且做剩余的事情,但不能将范围与列#34;名称"相关联,请参阅下面的宏并建议我应该如何做。

      嘿,先谢谢...

          Option Explicit
      
      Sub RulePicker()
          Dim ws As Worksheet
          Dim aCell As Range, Rng As Range
          Dim col As Long, lRow As Long
          Dim colName As String
      
          '~~> Change this to the relevant sheet
          Set ws = ThisWorkbook.Sheets("SEPY BUILD")
      
          With ws
              Set aCell = .Range("A1:ZZ1").Find(What:="Phone", LookIn:=xlValues, LookAt:=xlWhole, _
                          MatchCase:=False, SearchFormat:=False)
      
              '~~> If Found
              If Not aCell Is Nothing Then
                  col = aCell.Column
                  colName = Split(.Cells(, col).Address, "$")(1)
      
                  lRow = .Range(colName & .Rows.Count).End(xlUp).Row
      
                  '~~> This is your range
                  Set Rng = .Range(colName & "13:" & colName & lRow)
                      Rng.Formula = "=IF(RC[-9]="""","""",IF(LEFT(LOWER(RC[-9]),4)=""none"","""",VLOOKUP(LEFT(RC[-9],(FIND("" "",RC[-9],1)-1)),'Base Rule'!C[-10]:C[-5],2,0)))"
                      Rng.Value = Rng.Value
                      Rng.Offset(0, 1).FormulaR1C1 = "=IF(RC[-1]="""","""",VLOOKUP(RC[-1],Rules!C[-12]:C[-11],2,0))"
                      Rng.Offset(0, 2).FormulaR1C1 = "=IF(RC[-11]="""","""",IF(LEFT(LOWER(RC[-11]),4)=""none"","""",VLOOKUP(LEFT(RC[-11],(FIND("" "",RC[-11],1)-1)),'Base Rule'!C[-12]:C[-7],3,0)))"
                      Rng.Offset(0, 2).Value = Rng.Offset(0, 2).Value
                      Rng.Offset(0, 3).FormulaR1C1 = "=IF(RC[-12]="""","""",IF(LEFT(LOWER(RC[-12]),4)=""none"","""",VLOOKUP(LEFT(RC[-12],(FIND("" "",RC[-12],1)-1)),'Base Rule'!C[-13]:C[-8],4,0)))"
                      Rng.Offset(0, 3).Value = Rng.Offset(0, 3).Value
                      Rng.Offset(0, 4).FormulaR1C1 = "=IF(RC[-1]="""","""",VLOOKUP(RC[-1],Rules!C[-15]:C[-14],2,0))"
                      Rng.Offset(0, 5).FormulaR1C1 = "=IF(RC[-14]="""","""",IF(LEFT(LOWER(RC[-14]),4)=""none"","""",VLOOKUP(LEFT(RC[-14],(FIND("" "",RC[-14],1)-1)),'Base Rule'!C[-15]:C[-10],5,0)))"
                      Rng.Offset(0, 5).Value = Rng.Offset(0, 5).Value
                  Debug.Print Rng.Address
              '~~> If not found
              Else
                  MsgBox "Phone Header Not Found"
              End If
          End With
      End Sub
      

0 个答案:

没有答案