我有code
,可以将特定的cells
从主sheet
自动复制到另一个sheet
。当在value
中输入特定的Column B
时,可以实现此目的。
对于下面的示例,我要复制cells
中value
等于Column B
的所有Faults Raised
。我遇到的问题是主sheet
有一个单独的script
,该columns
根据输入到'values'
中的其他各种Column B
来隐藏/取消隐藏Faults Raised
。>
输入Columns B:C, AC:AE, BP
时,将显示Column 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
吗?我在做什么错了?
$array = json_encode($array);
$array = json_decode($array);
$array = (array)$array;
结束子
答案 0 :(得分:1)
您的.CurrentRegion正在阻塞您要在其中工作的区域。在sht1.Columns(“ B:BP”)。CurrentRegion中,.Range(“ B:BP”)。EntireColumn错误寻址或.AutoFilter字段:= 1引用了A列。相交可以帮助克服其中的一些问题问题。
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