返回列表 上一主題 發帖

[發問] 原有工作表中不同欄位資料,轉移到新產生工作表中,並重新安排位置(已解決)

回復 3# jesscc
  1. Sub SourceData_S()
  2. Dim Ay()
  3. With Worksheets("資料來源")
  4.     Set Rng = .Range("A3:B3")
  5.     fs = False
  6.     If .Range("B3").Value = "" Then
  7.     MsgBox "無法取得股票名稱,請確定股票名稱已填入B3儲存格", 32, "資料錯誤!"
  8.     Exit Sub
  9.     End If
  10.     For Each sh In Sheets '檢查工作表名稱是否存在
  11.        If sh.Name = .[B3].Text Then fs = True: Exit For
  12.     Next
  13.     If fs = False Then Sheets.Add.Name = .[B3].Text '如果工作表不存在就新增工作表
  14.     ar = Array("A", "C", "I", "P") '需要提取的欄位
  15.     ReDim Preserve Ay(s) ',將標題列存入陣列的第一筆並擴大陣列
  16.     Ay(s) = Array(.Cells(4, ar(0)).Value, .Cells(4, ar(1)).Value, .Cells(4, ar(2)).Value, .Cells(4, ar(3)).Value)
  17.     s = s + 1
  18.     For i = 5 To .Cells(.Rows.Count, 1).End(xlUp).Row '進入資料迴圈
  19.        If Weekday(.Cells(i, ar(0)), vbMonday) < 5 Then '判斷日期為星期幾,星期5以前執行
  20.           ReDim Preserve Ay(s) '將資料存入陣列
  21.           Ay(s) = Array(.Cells(i, ar(0)).Text, .Cells(i, ar(1)).Value, .Cells(i, ar(2)).Value, .Cells(i, ar(3)).Value)
  22.           s = s + 1
  23.           Else '星期五執行
  24.           ReDim Preserve Ay(s) '將資料存入陣列
  25.           Ay(s) = Array(.Cells(i, ar(0)).Text, .Cells(i, ar(1)).Value, .Cells(i, ar(2)).Value, .Cells(i, ar(3)).Value)
  26.           s = s + 1
  27.           ReDim Preserve Ay(s) '儲存一個空白列到陣列
  28.           Ay(s) = Array("", "", "", "")
  29.           s = s + 1
  30.         End If
  31.     Next
  32.     With Sheets(Sheets("資料來源").[B3].Text)
  33.     Rng.Copy .[A1] '股票名稱
  34.     With .Range(.[A3], .Cells(.Rows.Count, 6))
  35.        .ClearContents '清除原來資料
  36.        .Columns(1).NumberFormat = "yyyy/mm/dd" '設定A欄為日期格式
  37.     End With
  38.     .[A2].Resize(s, 4) = Application.Transpose(Application.Transpose(Ay)) '將陣列值寫入工作表
  39.     .Columns("A").AutoFit 'A欄自動欄寬
  40.     End With
  41.     End With
  42. End Sub
複製代碼
學海無涯_不恥下問

TOP

愛死你了 Hsieh 大大
怎麼那麼厲害,才沒幾分鐘的時間,就完成了。
我想要的結果都出來了,可是看不太懂程式的運作,實在太高深了。可以麻煩大大講解一下重點嗎?
Jess

TOP

回復 1# jesscc
  1. Sub SourceData_S()
  2. Dim Ay()
  3. With Worksheets("資料來源")
  4.     Set Rng = .Range("A3:B3")
  5.     fs = False
  6.     If .Range("B3").Value = "" Then
  7.     MsgBox "無法取得股票名稱,請確定股票名稱已填入B3儲存格", 32, "資料錯誤!"
  8.     Exit Sub
  9.     End If
  10.     For Each sh In Sheets
  11.        If sh.Name = .[B3].Text Then fs = True: Exit For
  12.     Next
  13.     If fs = False Then Sheets.Add.Name = .[B3].Text
  14.     ar = Array("A", "C", "I", "P")
  15.     ReDim Preserve Ay(s)
  16.     Ay(s) = Array(.Cells(4, ar(0)).Value, .Cells(4, ar(1)).Value, .Cells(4, ar(2)).Value, .Cells(4, ar(3)).Value)
  17.     s = s + 1
  18.     For i = 5 To .Cells(.Rows.Count, 1).End(xlUp).Row
  19.        If Weekday(.Cells(i, ar(0)), vbMonday) < 5 Then
  20.           ReDim Preserve Ay(s)
  21.           Ay(s) = Array(.Cells(i, ar(0)).Text, .Cells(i, ar(1)).Value, .Cells(i, ar(2)).Value, .Cells(i, ar(3)).Value)
  22.           s = s + 1
  23.           Else
  24.           ReDim Preserve Ay(s)
  25.           Ay(s) = Array(.Cells(i, ar(0)).Text, .Cells(i, ar(1)).Value, .Cells(i, ar(2)).Value, .Cells(i, ar(3)).Value)
  26.           s = s + 1
  27.           ReDim Preserve Ay(s)
  28.           Ay(s) = Array("", "", "", "")
  29.           s = s + 1
  30.         End If
  31.     Next
  32.     With Sheets(Sheets("資料來源").[B3].Text)
  33.     Rng.Copy .[A1]
  34.     With .Range(.[A3], .Cells(.Rows.Count, 6))
  35.        .ClearContents
  36.        .Columns(1).NumberFormat = "yyyy/mm/dd"
  37.     End With
  38.     .[A2].Resize(s, 4) = Application.Transpose(Application.Transpose(Ay))
  39.     .Columns("A").AutoFit
  40.     End With
  41.     End With
  42. End Sub
複製代碼
學海無涯_不恥下問

TOP

        靜思自在 : 做該做的事是智慧,做不該做的事是愚癡。
返回列表 上一主題