擅长的VBAsorting不同的活页之一

我想知道是否有办法让我的macros做下面的事情:

我希望它在sheet2上运行这个代码:

Dim arrColOrder As Variant, ndx As Integer Dim Found As Range, counter As Integer 'Place the column headers in the end result order you want. arrColOrder = Array("*Item1*", "*Item2*", "*Item3*", "*Item4*") counter = 1 Application.ScreenUpdating = False For ndx = LBound(arrColOrder) To UBound(arrColOrder) Set Found = Rows("1:1").Find(arrColOrder(ndx), LookIn:=xlValues, LookAt:=xlWhole, _ SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) If Not Found Is Nothing Then If Found.Column <> counter Then Found.EntireColumn.Cut Columns(counter).Insert Shift:=xlToRight Application.CutCopyMode = False End If counter = counter + 1 End If Next ndx Application.ScreenUpdating = True End Sub 

然后在sheet1上运行这个代码:

'这将为列A中基于Item1的Item2,Item3和Item4数据点添加三列。

  Columns("P:P").Select Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Range("P1").Select ActiveCell.FormulaR1C1 = "Item4" Columns("P:P").Select Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Range("P1").Select ActiveCell.FormulaR1C1 = "Item3" Columns("P:P").Select Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Range("P1").Select ActiveCell.FormulaR1C1 = "Item2" Range("P1:R1").Select Range("R1").Activate With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 15773696 .TintAndShade = 0 .PatternTintAndShade = 0 End With Range("P2").Select ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[10],Sheet1!C[-15]:C[-14],2,FALSE)" Range("P2").Select Selection.AutoFill Destination:=Range("P2:P" & Cells(Rows.Count, "Z").End(xlUp).Row) Range("Q2").Select ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[9],Sheet1!C[-16]:C[-14],3,FALSE)" Range("Q2").Select Selection.AutoFill Destination:=Range("Q2:Q" & Cells(Rows.Count, "Z").End(xlUp).Row) Range("R2").Select ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[8],Sheet1!C[-17]:C[-14],4,FALSE)" Range("R2").Select Selection.AutoFill Destination:=Range("R2:R" & Cells(Rows.Count, "Z").End(xlUp).Row) End Sub 

有没有办法? 到目前为止,所有这一切都运行在活动工作表上,这使得一切都变得糟糕起来。

我想要的是重新排列表2中的东西,所以当我在表1中进行V查找时,它们的顺序是正确的!

请帮忙,谢谢!

到目前为止,所有这一切都运行在活动工作表上,这使得一切都变得糟糕起来。

啊哈,你已经find了使用.Activate.Select一个陷阱。 这是一个很好的 SO线程,概述了如何避免使用。select。

关于你的问题,你如何在两张不同的纸上运行两个代码? 这并不难 – 只需创build两个Worksheetvariables,然后使用With语句。

例如,下面的代码将“A”放在Sheet1,单元格A1和Sheet2中的“A”,单元格A2:

 Sub test() Dim ws1 As Worksheet, ws2 As Worksheet 'Let's define our variables. For worksheets, like Ranges, you need to use 'Set' Set ws1 = Sheets("Sheet1") Set ws2 = Sheets("Sheet2") 'Now, we want to work with each specific worksheet. Let's choose one at a time. With ws1 .Cells(1, 1).Value = "A" ' Do other code here, to happen on ws1. Note the use of the '.' before '.Cells', this tells excel to use ' ws1.cells(1,1) ... End With With ws2 .Cells(2, 1).Value = "A" 'Do other stuff here for worksheet2 End With End Sub 

您的代码的“快速和肮脏”的修复方法是,在您要在Sheet2上使用的行之前,input行Sheets("Sheet2").Activate

编辑:关于使用ws1ws2更多注意事项:

 With ws1 .cells(1,1).Value = "A" End with 

ws1.cells(1,1).Value = "A" 。 您使用With因为您可以将所有代码放在那里,这是与Worksheet1运行。 在你的代码中,如果你创build了一个ws1ws2variables,并将它们设置为正确的表单,那么你可以把ws1. 在该工作表中要select的所有范围之前,以及ws2. 为第二个工作表。 那有意义吗?

如果要在工作表1中select范围“A1:B10”,则可以删除范围

 With ws1 .Range("A1:B10").Delete End with ' is same as ws1.range("A1:B10").Delete 

要么

 With ws1 .Range(.Cells(1,1),.Cells(10,2)).Delete End with ' is same as ws1.Range(ws1.Cells(1,1),ws1.CElls(10,2)).Delete 

注意.Cells之前。 这是因为您希望此范围引用sheet1的范围,而不是其他任何工作表。 离开了. 如果另一个工作表在macros中激活,可能会导致问题。

EDIT2:

毕竟以上,我已经编辑你的OP代码,以避免使用。select。 你应该可以研究这个,思考,看看我做了什么:

 Sub test() Dim ws1 As Worksheet, ws2 As Worksheet Set ws1 = Sheets("Sheet1") Set ws2 = Sheets("Sheet2") ''' RUN THE BELOW ON SHEET 2 Dim arrColOrder As Variant, ndx As Integer Dim Found As Range, counter As Integer 'Place the column headers in the end result order you want. arrColOrder = Array("*Item1*", "*Item2*", "*Item3*", "*Item4*") counter = 1 Application.ScreenUpdating = False For ndx = LBound(arrColOrder) To UBound(arrColOrder) Set Found = ws2.Rows("1:1").Find(arrColOrder(ndx), LookIn:=xlValues, LookAt:=xlWhole, _ SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) If Not Found Is Nothing Then If Found.Column <> counter Then Found.EntireColumn.Cut ws2.Columns(counter).Insert Shift:=xlToRight Application.CutCopyMode = False End If counter = counter + 1 End If Next ndx Application.ScreenUpdating = True ''' RUN THE BELOW ON SHEET1 With ws1 .Columns("P:P").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove .Range("P1").FormulaR1C1 = "Item4" .Columns("P:P").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove .Range("P1").FormulaR1C1 = "Item3" .Columns("P:P").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove .Range("P1").FormulaR1C1 = "Item2" ' .Range("P1:R1").Select 'Don't need this, since you don't do anything with it. With .Range("R1").Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 15773696 .TintAndShade = 0 .PatternTintAndShade = 0 End With .Range("P2").FormulaR1C1 = "=VLOOKUP(RC[10],Sheet1!C[-15]:C[-14],2,FALSE)" .Range("P2").AutoFill Destination:=Range("P2:P" & Cells(Rows.Count, "Z").End(xlUp).Row) .Range("Q2").FormulaR1C1 = "=VLOOKUP(RC[9],Sheet1!C[-16]:C[-14],3,FALSE)" .Range("Q2").AutoFill Destination:=Range("Q2:Q" & Cells(Rows.Count, "Z").End(xlUp).Row) .Range("R2").FormulaR1C1 = "=VLOOKUP(RC[8],Sheet1!C[-17]:C[-14],4,FALSE)" .Range("R2").AutoFill Destination:=Range("R2:R" & Cells(Rows.Count, "Z").End(xlUp).Row) End Sub 

创build一个Excel工作表对象。 你可以sorting。

 Dim ws As Excel.Worksheet Set ws = Worksheets("Sheet2") 'Then you do a sort on ws.Range("A:A").Sort