返回列表 上一主題 發帖

[發問] 篩選資料並且放到新的Sheet裡

回復 1# candy516
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Rng As Range, 除息日 As Date, 股票 As Range, R As Range, Ar(), i As Integer
  4.     With Sheets("Sheet1")
  5.         .Activate
  6.         Set Rng = .Range("A2:A" & .Range("A2").End(xlDown).Row)  '設定Sheet1股票範圍
  7.         If Application.Intersect(Rng, ActiveCell) Is Nothing Then  '沒有選擇到股票
  8.             MsgBox "股票代號: 有誤"
  9.             Exit Sub
  10.         End If
  11.         Set Rng = ActiveCell
  12.         'Rng(1, 2) = Rng.Cells(1, 2)
  13.         除息日 = Mid(Rng(1, 2), 1, 4) & "/" & Mid(Rng(1, 2), 5, 2) & "/" & Mid(Rng(1, 2), 7, 2)
  14.     End With
  15.     ReDim Ar(1, 0)
  16.     With Sheets(Mid(Rng(1, 2), 1, 4))     '除息年度工作表
  17.         Set 股票 = .Rows(1).Find(Rng, LOOKAT:=xlPart, LookIn:=xlValues)  '找到股票代號名稱 日報酬率欄位
  18.         For Each R In .Range("A3:A" & .Range("A3").End(xlDown).Row)     '
  19.             If R >= 除息日 And R <= 除息日 + 14 Then
  20.                 Ar(0, i) = R
  21.                 Ar(1, i) = R.Cells(1, 股票.Column)
  22.                 i = i + 1
  23.                 ReDim Preserve Ar(1, i)  '增加陣列的維數
  24.             End If
  25.         Next
  26.     End With
  27.     If i = 0 Then
  28.         MsgBox "找不到" & 股票 & "日報酬率"
  29.         Exit Sub
  30.     End If
  31.     With Sheets("Sheet2").Range("IV1").End(xlToLeft).Offset(, 1)  'Range("IV1")往左有資料的第一個儲存格->Offset(, 1) 向右移動一欄
  32.         .Cells(1, 2) = 股票
  33.         .Cells(2, 1) = "年月日"
  34.         .Cells(2, 2) = "日報酬率"
  35.         .Cells(3, 1).Resize(i, 2) = Application.WorksheetFunction.Transpose(Ar)
  36.     End With
  37.     End
  38. End Sub
複製代碼

TOP

回復 3# candy516
傳檔來看看

TOP

回復 9# candy516
股票代號: 有誤
表示在Sheets("Sheet1") 沒有用滑鼠選定股票代號
你的檔案Sheets("Sheet1")是選在 A1  "證券代碼"
請在Sheets("Sheet1")的A欄  選定一家的 證券代碼 再試試

TOP

回復 13# candy516
你1樓的訴求不是這樣?
還有其他想法說說看

TOP

回復 15# candy516
試試看是否一樣
  1. Sub Ex()
  2.     Dim 除息日 As Date, 股票 As Range, R As Range, Ar(), E As Integer, i As Integer, ii As Integer
  3.     Sheets("Sheet2").Cells.Clear
  4.     For E = 2 To Sheets("Sheet1").UsedRange.Rows.Count
  5.         Set 股票 = Sheets("Sheet1").UsedRange.Rows(E).Cells(1)
  6.         除息日 = Format(Sheets("Sheet1").UsedRange.Rows(E).Cells(2), "0000/00/00")
  7.         ReDim Ar(1, 0)
  8.         i = 0
  9.         ii = 1
  10.         With Sheets(Year(除息日) & "")
  11.             Set 股票 = .Rows(1).Find(股票, LOOKAT:=xlPart, LookIn:=xlValues)  '找到股票代號名稱 日報酬率欄位
  12.             For Each R In .Range("A3:A" & .Range("A3").End(xlDown).Row)     '
  13.                 If R >= 除息日 And R <= 除息日 + 14 Then
  14.                     Ar(0, i) = R
  15.                     Ar(1, i) = R.Cells(1, 股票.Column)
  16.                     i = i + 1
  17.                     ReDim Preserve Ar(1, i)  '增加陣列的維數
  18.                 End If
  19.             Next
  20.             If i > 0 Then
  21.                 If Sheets("Sheet2").Range("IV" & ii).End(xlToLeft).Offset(, 1).Column >= Columns.Count - 1 Then ii = ii + 14
  22.                 With Sheets("Sheet2").Range("IV" & ii).End(xlToLeft).Offset(, 1) 'Range("IV1")往左有資料的第一個儲存格->Offset(, 1) 向右移動一欄
  23.                     .Cells(1, 2) = 股票
  24.                     .Cells(2, 1) = "年月日"
  25.                     .Cells(2, 2) = "日報酬率"
  26.                     .Cells(3, 1).Resize(i, 2) = Application.WorksheetFunction.Transpose(Ar)
  27.                 End With
  28.             End If
  29.         End With
  30.     Next
  31.     With Sheets("Sheet2")
  32.         .Columns(1).Delete
  33.         .Cells.EntireColumn.AutoFit
  34.         .Cells.EntireRow.AutoFit
  35.     End With
  36. End Sub
複製代碼

TOP

        靜思自在 : 人生不一定球球是好球,但是有歷練的強打者,隨時都可以揮棒。
返回列表 上一主題