返回列表 上一主題 發帖

[發問] 求高手幫忙簡化程式碼

回復 1# s13030029
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Rng(1 To 2) As Range, xRow As Integer, AR, i As Integer, xI As Integer
  4.     With Sheets("工作表1")
  5.         xRow = .[A9].End(xlDown).Row
  6.         With .Range("$E$9:$G" & xRow)
  7.             .AutoFilter
  8.             .AutoFilter Field:=1, Criteria1:="=目視", Operator:=xlOr, Criteria2:="=游標卡尺"
  9.             '***篩選後A欄到G欄的資料區域******
  10.             Set Rng(1) = .Parent.Range("A10:G" & xRow).SpecialCells(xlCellTypeVisible)
  11.             '*********************************
  12.             Set Rng(2) = Sheets("工作表3").[A13]
  13.             For i = 1 To Rng(1).Areas.Count                 '**篩選後資料 不一定是連續的
  14.                 For xI = 1 To Rng(1).Areas(i).Rows.Count    ''**篩選後資料的 Areas 集合,此集合代表多區域選取範圍中的所有範圍。
  15.                     AR = Application.Transpose(Application.Transpose(Rng(1).Areas(i).Rows(xI)))
  16.                     'Rng(1).Areas(i).Rows(xI) 轉換成一維陣列
  17.                     
  18.                     '***處理 篩選後A欄到G欄的資料區域  中有沒有資料的欄位******
  19.                     AR = Join(AR, ",")  '陣列以","連結轉為文字
  20.                     Do While InStr(AR, ",,")
  21.                         AR = Replace(AR, ",,", ",")  ' ** 消除多餘的","  '
  22.                     Loop
  23.                     AR = Split(Mid(AR, 1, Len(AR) - 1), ",")  '文字以","轉為陣列
  24.                     With Rng(2)
  25.                         .Cells(1) = AR(0)
  26.                         .Cells(1, 2) = AR(1) & vbLf & AR(2)
  27.                         EX_格式 .Cells(1).Resize(5)
  28.                         EX_格式 .Cells(1, 2).Resize(5)
  29.                     End With
  30.                     Set Rng(2) = Rng(2).Offset(1)
  31.                 Next
  32.            Next
  33.         End With
  34.     End With
  35. End Sub
  36. Sub EX_格式(ByVal Target As Range)
  37.     With Target
  38.         .HorizontalAlignment = xlCenter
  39.         .VerticalAlignment = xlCenter
  40.         .WrapText = False
  41.         .Orientation = 0
  42.         .AddIndent = False
  43.         .IndentLevel = 0
  44.         .ShrinkToFit = False
  45.         .ReadingOrder = xlContext
  46.         '.MergeCells = True
  47.          .Merge
  48.     End With
  49. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 願要大、志要堅、氣要柔、心要細。
返回列表 上一主題