標題:
[發問]
求高手幫忙簡化程式碼
[打印本頁]
作者:
s13030029
時間:
2019-5-27 09:45
標題:
求高手幫忙簡化程式碼
程式內容是將sheet1篩選後的資料複製到sheet2的合併儲存格
目前錄製的方法是複製到sheet2後,再合併儲存格
sheet1的資料是變動的
sheet2的合併儲存格是以5列合併為準
希望能用迴圈或其他方式簡化程式碼
Sub 巨集2()
Range("E9:G15").Select
Selection.AutoFilter
ActiveSheet.Range("$E$9:$G$15").AutoFilter Field:=1, Criteria1:="=目視", _
Operator:=xlOr, Criteria2:="=游標卡尺"
Range("A9").Select
Selection.Copy
Sheets("工作表3").Select
Range("A13").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("工作表1").Select
Range("A11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("工作表3").Select
Range("A18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("工作表1").Select
Range("A14").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("工作表3").Select
Range("A23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("工作表1").Select
Range("A15").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("工作表3").Select
Range("A28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("工作表1").Select
Range("B9:D9").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("工作表3").Select
Range("B13").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("工作表1").Select
Range("B11:D11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("工作表3").Select
Range("B18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("工作表1").Select
Range("B14:D14").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("工作表3").Select
Range("B23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("工作表1").Select
Range("B15:D15").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("工作表3").Select
Range("B28").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("工作表1").Select
Range("E9:G9").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "目視"
Sheets("工作表3").Select
Range("B13").Select
ActiveCell.FormulaR1C1 = "SIPCSS002-00目視"
Range("B14").Select
Sheets("工作表1").Select
Range("E11:G11").Select
ActiveCell.FormulaR1C1 = "游標卡尺"
Sheets("工作表3").Select
Range("B18").Select
ActiveCell.FormulaR1C1 = "φ19.93-20.07 游標卡尺"
Range("B19").Select
Sheets("工作表1").Select
Range("E14:G14").Select
ActiveCell.FormulaR1C1 = "游標卡尺"
Sheets("工作表3").Select
Range("B23").Select
ActiveCell.FormulaR1C1 = "14.90-15.10 游標卡尺"
Range("B24").Select
Sheets("工作表1").Select
Range("E15:G15").Select
ActiveCell.FormulaR1C1 = "目視"
Sheets("工作表3").Select
Range("B28").Select
ActiveCell.FormulaR1C1 = "袋裝 目視"
Range("A13:A17").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("B13:B17").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
ActiveCell.FormulaR1C1 = "SIPCSS002-00 目視"
Range("B13:B17").Select
ActiveCell.FormulaR1C1 = "SIPCSS002-00 目視"
Range("A18:A22").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("B18:B22").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Range("B18:B22").Select
ActiveCell.FormulaR1C1 = "φ19.93-20.07 游標卡尺"
Range("A23:A27").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("B23:B27").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
ActiveCell.FormulaR1C1 = "14.90-15.10 游標卡尺"
Range("A28:A32").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("B28:B32").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
ActiveCell.FormulaR1C1 = "袋裝 目視"
Range("B33").Select
Sheets("工作表1").Select
Selection.AutoFilter
End Sub
複製代碼
[attach]30673[/attach]
作者:
kim223824
時間:
2019-5-30 17:45
回復
1#
s13030029
Sub test_20190530()
ROW1 = Cells(Rows.Count, "E").End(3).Row
Range("E9:G" & ROW1).Select
Selection.AutoFilter
ActiveSheet.Range("$E$9:$G$" & ROW1).AutoFilter Field:=1, Criteria1:="=目視", _
Operator:=xlOr, Criteria2:="=游標卡尺"
Sheets("¤u§@ªí3").Cells.Clear
Range("A9:G" & ROW1).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("¤u§@ªí3").Select
Range("A1").Select
ActiveSheet.Paste
ROW2 = Cells(Rows.Count, "A").End(3).Row
Range("H1:H" & ROW2) = "=B1 & "" "" & E1"
Range("H1:H" & ROW2).Value = Range("H1:H" & ROW2).Value
Columns("B:G").Delete Shift:=xlToLeft
For i = ROW2 To 1 Step -1
Range("A" & i & ":A" & i + 4).Merge
Range("B" & i & ":B" & i + 4).Merge
Range("B" & i & ":B" & i + 4).WrapText = True
Range("B" & i & ":B" & i + 4).HorizontalAlignment = xlCenter
Rows(i & ":" & i + 3).Insert Shift:=xlDown
Next
ROW2 = Cells(Rows.Count, "A").End(3).Row
Range("A5:B" & ROW2).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("A5").Select
Columns("B:B").ColumnWidth = 12
Sheets(1).Select
Selection.AutoFilter
Range("A9").Select
End Sub
[attach]30730[/attach]
作者:
GBKEE
時間:
2019-5-31 10:36
回復
1#
s13030029
Option Explicit
Sub Ex()
Dim Rng(1 To 2) As Range, xRow As Integer, AR, i As Integer, xI As Integer
With Sheets("工作表1")
xRow = .[A9].End(xlDown).Row
With .Range("$E$9:$G" & xRow)
.AutoFilter
.AutoFilter Field:=1, Criteria1:="=目視", Operator:=xlOr, Criteria2:="=游標卡尺"
'***篩選後A欄到G欄的資料區域******
Set Rng(1) = .Parent.Range("A10:G" & xRow).SpecialCells(xlCellTypeVisible)
'*********************************
Set Rng(2) = Sheets("工作表3").[A13]
For i = 1 To Rng(1).Areas.Count '**篩選後資料 不一定是連續的
For xI = 1 To Rng(1).Areas(i).Rows.Count ''**篩選後資料的 Areas 集合,此集合代表多區域選取範圍中的所有範圍。
AR = Application.Transpose(Application.Transpose(Rng(1).Areas(i).Rows(xI)))
'Rng(1).Areas(i).Rows(xI) 轉換成一維陣列
'***處理 篩選後A欄到G欄的資料區域 中有沒有資料的欄位******
AR = Join(AR, ",") '陣列以","連結轉為文字
Do While InStr(AR, ",,")
AR = Replace(AR, ",,", ",") ' ** 消除多餘的"," '
Loop
AR = Split(Mid(AR, 1, Len(AR) - 1), ",") '文字以","轉為陣列
With Rng(2)
.Cells(1) = AR(0)
.Cells(1, 2) = AR(1) & vbLf & AR(2)
EX_格式 .Cells(1).Resize(5)
EX_格式 .Cells(1, 2).Resize(5)
End With
Set Rng(2) = Rng(2).Offset(1)
Next
Next
End With
End With
End Sub
Sub EX_格式(ByVal Target As Range)
With Target
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
'.MergeCells = True
.Merge
End With
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)