简体   繁体   English

宏根据数组重新排列列会引发错误

[英]Macro to re-arrange columns based on array throws error

I have been looking for a macro like this for a long time. 很长时间以来,我一直在寻找这样的宏。

The macro throws a 1004 error and the Columns(l + 1).Insert is highlighted in yellow 宏抛出1004错误,并且Columns(l + 1).Insert突出显示为黄色

This selection is not valid

Copy and past areas cannot overlap unless they'er the same size and shape

There are 107 rows, possibly the code is coping the whole column not just the 107 rows? 有107行,可能代码正在处理整个列,而不仅仅是107行? no idea on how the fix this 不知道如何解决这个问题

Thanks 谢谢

Sub f()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
On Error GoTo Skipit
HeaderNames = Array("RespID", "Subject", "Tag", "Strengths Comments", "Improvement Comments")
For l = 0 To UBound(HeaderNames)
    Columns(Rows(1).Find(HeaderNames(l), , xlValues, xlWhole).Column).Cut
    Columns(l + 1).Insert
Skipit:
Next
ActiveSheet.UsedRange.Offset(, l).ClearContents
Application.ScreenUpdating = True
Application.DisplayAlerts = True
On Error GoTo 0
End Sub

you can not copy and paste in the same place. 您不能在同一位置复制和粘贴。 this should work: 这应该工作:

Sub f()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
On Error GoTo Skipit
Dim HeaderNames, l As Long, colFrom As Long

HeaderNames = Array("RespID", "Subject", "Tag", "Strengths Comments", "Improvement Comments")
For l = 0 To UBound(HeaderNames)
    colFrom = Rows(1).Find(HeaderNames(l), , xlValues, xlWhole).Column
    If l + 1 <> colFrom Then Columns(colFrom).Cut: Columns(l + 1).Insert
Skipit:
Next
ActiveSheet.UsedRange.Offset(, l).ClearContents
Application.ScreenUpdating = True
Application.DisplayAlerts = True
On Error GoTo 0
End Sub

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

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