代码之家  ›  专栏  ›  技术社区  ›  Chopin

将值复制到单独的Excel工作表VBA

  •  2
  • Chopin  · 技术社区  · 6 年前

    code 自动复制特定 cells 从主人那里 sheet 床单 . 当一个特定的 value 签订 Column B

    细胞 在哪里 在里面 B列 等于 Faults Raised 床单 有一个单独的 script 隐藏/取消隐藏 columns 基于其他各种 'values' B列 .

    已输入, Columns B:C, AC:AE, BP 如图所示。但是当我试图自动复制时 B列 如图所示。我找不到 C, AC:AE BP 复制?我做错什么了?

    Option Explicit
    
    Sub FilterAndCopy()
      Dim sht1 As Worksheet, sht2 As Worksheet
    
      Set sht1 = Sheets("SHIFT LOG")
      Set sht2 = Sheets("FAULTS RAISED")
    
      sht2.UsedRange.ClearContents
      Dim rng As Range
    
      With sht1.Cells(2, "B").CurrentRegion
          .Range("B:BP").EntireColumn.Hidden = False ' unhide columns
          .AutoFilter
          .AutoFilter 2, "Faults Raised"
          .SpecialCells(xlCellTypeVisible).Copy sht2.Cells(6, 2)
          .AutoFilter
    
          .Range("C:AA").EntireColumn.Hidden = True ' hide columns
          sht2.Range("C:AA").EntireColumn.Delete ' delete 'sht2' columns
          .Range("AE:BN").EntireColumn.Hidden = True ' hide columns
          sht2.Range("AE:BN").EntireColumn.Delete ' delete 'sht2' columns
      End With
    

    末端接头

    1 回复  |  直到 6 年前
        1
  •  2
  •   user4039065 user4039065    6 年前

    Option Explicit
    
    Sub FilterAndCopy()
        Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
    
        Set sht1 = Worksheets("SHIFT LOG")
        Set sht2 = Worksheets("FAULTS RAISED")
    
        sht2.UsedRange.ClearContents
    
        With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
            .Cells.EntireColumn.Hidden = False ' unhide columns
            If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
            'within B:BP, column B is the first column
            .AutoFilter field:=1, Criteria1:="Faults Raised"
            'within B:BP, Columns B:C, AC:AE, BP are referenced as .Columns A:B, AB:AD, BO
            .Range("A:B, AB:AD, BO:BO").Copy Destination:=sht2.Cells(6, "B")
            .Parent.AutoFilterMode = False
    
            'no need to delete what was never there
            'within B:BP, Columns C:AA, AE:BN, BP are referenced as .Columns B:Z, AD:BM
            .Range("B:Z").EntireColumn.Hidden = True ' hide columns
            .Range("AD:BM").EntireColumn.Hidden = True ' hide columns
        End With
    End Sub