返回列表 上一主題 發帖

[發問] 求助~關於vba的程式!

回復 4# candy516
最主要是看不懂你所謂除權息的標是要填在哪裡?
請試舉例來說明,你資料要怎麼填法
學海無涯_不恥下問

TOP

本帖最後由 Hsieh 於 2011-1-17 16:20 編輯

回復 6# candy516

我不懂的是,Sheet1記載是2010/9/7  是除權日
但妳敘述時又是變成2010/9/8
若條件沒成立是往前找或是往後找
找到後要在C85寫入什麼值?
萬一除權後都沒回升又該如何?
學海無涯_不恥下問

TOP

回復 8# candy516
  1. Sub ex()
  2. Set d = CreateObject("Scripting.Dictionary")
  3. Application.ScreenUpdating = False
  4. With Sheet1
  5.    For Each a In .Range(.[A2], .[A2].End(xlDown))
  6.       d(a & Year(a.Offset(, 1))) = Array(a.Offset(, 1), a.Offset(, 2))
  7.    Next
  8. End With
  9. For y = 2001 To 2010
  10.   With Sheets(CStr(y))
  11.     If Application.CountBlank(.Range(.[B1], .[IV1].End(xlToLeft))) > 0 Then .Range(.[B1], .[IV1].End(xlToLeft)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  12.     .Range(.[IV1].End(xlToLeft).Offset(, 1), .[IV1]).EntireColumn.Clear
  13.     k = 2
  14.     Do Until .Cells(1, k) = ""
  15.     .Columns(k + 1).Insert
  16.        mystr = .Cells(1, k) & y
  17.        Set a = .Columns("A").Find(d(mystr)(0))
  18.        If Not a Is Nothing Then
  19.        cnt = 0
  20.          r = a.Row
  21.          test = .Cells(r - 1, k)
  22.          r = r + 1
  23.          cnt = cnt + 1
  24.          Do Until .Cells(r, k) >= test Or .Cells(r, k) = ""
  25.          cnt = cnt + 1
  26.          r = r + 1
  27.          Loop
  28.          If .Cells(r, 1) = "" Then
  29.          .Cells(a.Row, k + 1) = "無填權"
  30.          Else
  31.          .Cells(a.Row, k + 1) = cnt
  32.          End If
  33.        End If
  34.        k = k + 2
  35.     Loop
  36.   End With
  37. Next
  38. Application.ScreenUpdating = True
  39. End Sub
複製代碼
學海無涯_不恥下問

TOP

回復 10# candy516
  1. Sub ex()
  2. Set d = CreateObject("Scripting.Dictionary")
  3. Application.ScreenUpdating = False
  4. With Sheet1
  5.    For Each a In .Range(.[A2], .[A2].End(xlDown))
  6.       d(a & Year(a.Offset(, 1))) = a.Offset(, 1).Value
  7.    Next
  8. End With
  9. For y = 2001 To 2010
  10.   With Sheets(CStr(y))
  11.     If Application.CountBlank(.Range(.[B1], .[IV1].End(xlToLeft))) > 0 Then .Range(.[B1], .[IV1].End(xlToLeft)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  12.     .Range(.[IV1].End(xlToLeft).Offset(, 1), .[IV1]).EntireColumn.Clear
  13.     k = 2
  14.     Do Until .Cells(1, k) = ""
  15.     .Columns(k + 1).Insert
  16.        mystr = .Cells(1, k) & y
  17.        Set a = .Columns("A").Find(d(mystr))
  18.        If Not a Is Nothing Then
  19.        cnt = 0
  20.          r = a.Row
  21.          test = .Cells(r + 1, k)
  22.          cnt = cnt + 1
  23.          Do Until .Cells(r, k) >= test Or .Cells(r, k) = ""
  24.          cnt = cnt + 1
  25.          r = r - 1
  26.          Loop
  27.          If r <= 2 Then
  28.          .Cells(a.Row, k + 1) = "無填權"
  29.          Else
  30.          .Cells(a.Row, k + 1) = cnt
  31.          End If
  32.        End If
  33.        k = k + 2
  34.     Loop
  35.   End With
  36. Next
  37. Application.ScreenUpdating = True
  38. End Sub
複製代碼
學海無涯_不恥下問

TOP

回復 12# candy516

因為欄數過多必須使用2007版本
  1. Sub ex()
  2. Set d = CreateObject("Scripting.Dictionary")
  3. Application.ScreenUpdating = False
  4. With Sheet1
  5.    For Each a In .Range(.[A2], .[A2].End(xlDown))
  6.       d(a & Year(a.Offset(, 1))) = a.Offset(, 1).Value
  7.    Next
  8. End With
  9. For y = 2001 To 2004
  10.   With Sheets(CStr(y))
  11.     If Application.CountBlank(.Range(.[B1], .[XFD1].End(xlToLeft))) > 0 Then .Range(.[B1], .[XFD1].End(xlToLeft)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  12.     .Range(.[XFD1].End(xlToLeft).Offset(, 1), .[XFD1]).EntireColumn.Clear
  13.     k = 2
  14.     Do Until .Cells(1, k) = ""
  15.     .Columns(k + 1).Insert
  16.        mystr = .Cells(1, k) & y
  17.        Set a = .Columns("A").Find(d(mystr))
  18.        If Not a Is Nothing Then
  19.        cnt = 0
  20.          r = a.Row
  21.          test = .Cells(r + 1, k)
  22.          cnt = cnt + 1
  23.          Do Until .Cells(r, k) >= test Or .Cells(r, k) = ""
  24.          cnt = cnt + 1
  25.          r = r - 1
  26.          Loop
  27.          If r <= 2 Then
  28.          .Cells(a.Row, k + 1) = "無填權"
  29.          Else
  30.          .Cells(a.Row, k + 1) = cnt
  31.          End If
  32.        End If
  33.        k = k + 2
  34.     Loop
  35.   End With
  36. Next
  37. Application.ScreenUpdating = True
  38. End Sub
複製代碼
學海無涯_不恥下問

TOP

回復 15# candy516

那要看你的格式是否跟其他年度一樣
學海無涯_不恥下問

TOP

回復 19# candy516
比較好的方式是把所有年度資料都整理在同一工作表中
跨年跨月的問題就容易解決
學海無涯_不恥下問

TOP

回復 22# candy516
這樣可能麻煩一點
程式將填入填權日數,並將10日內填權名單列出
請將程式碼置於Sheet1模組
  1. Sub 填權()
  2. Dim A As Range, Ar()
  3. Set dic = CreateObject("Scripting.Dictionary")
  4. Set myday = CreateObject("Scripting.Dictionary")
  5.           ReDim Preserve Ar(z)
  6.           Ar(z) = Array("公司", "年度", "填權日數")
  7.           z = z + 1

  8. Dim Sh As Worksheet
  9. For j = Sheets.Count To 1 Step -1
  10. Set Sh = Sheets(j)
  11.   With Sh
  12.      If .Name <> Me.Name Then
  13.      Set rng = .Range(.[B1], .Cells(1, .Columns.Count).End(xlToLeft))
  14.         If Application.CountBlank(rng) > 0 Then rng.SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  15.         For r = .Cells(.Rows.Count, 1).End(xlUp).Row To 3 Step -1
  16.         Set A = .Cells(r, 1)
  17.            myday(A.Value) = temp
  18.            temp = A.Value
  19.         Next
  20.         Else
  21.         For Each A In .Range(.[A2], .Cells(.Rows.Count, 1).End(3))
  22.         dic(A & Year(A.Offset(, 1))) = A.Offset(, 1)
  23.         Next
  24.     End If
  25.   End With
  26. Next
  27. For Each Sh In Sheets
  28.   With Sh
  29.   If .Name < Me.Name Then
  30.   k = 2: dn = .Name
  31.     Do Until .Cells(1, k) = ""
  32.        .Columns(k + 1).Insert
  33.        f = .Cells(1, k)
  34.        Set A = .Columns("A").Find(dic(f & dn))
  35.        If A Is Nothing Then GoTo 20
  36.        d = myday(A.Value)
  37.        With Sheets(CStr(Year(d)))
  38.           Set b = .Columns("A").Find(d)
  39.           Set c = .Rows(1).Find(f)
  40.           If b Is Nothing Or c Is Nothing Then cnt = "無填權": GoTo 10
  41.           x = b.Row
  42.           y = c.Column
  43.           test = .Cells(x, y)
  44.        End With
  45.        r = A.Row: cnt = 1
  46.        Do Until r = 2 Or .Cells(r, k) >= test
  47.        r = r - 1
  48.        cnt = cnt + 1
  49.        Loop
  50.        If r = 2 Then cnt = "無填權"
  51. 10
  52.        .Cells(A.Row, k + 1) = cnt
  53.        If IsNumeric(cnt) And cnt <= 10 Then
  54.           ReDim Preserve Ar(z)
  55.           Ar(z) = Array(f, dn, cnt)
  56.           z = z + 1
  57.        End If
  58.       
  59. 20
  60.        k = k + 2
  61.     Loop
  62.   End If
  63.   End With
  64. Next
  65. Set dic = Nothing
  66. Set myday = Nothing
  67. With Worksheets.Add
  68. .[A1].Resize(z, 3) = Application.Transpose(Application.Transpose(Ar))
  69. .Move
  70. End With
  71. End Sub
複製代碼
學海無涯_不恥下問

TOP

        靜思自在 : 謊言像一朵盛開的鮮花,外表美麗,生命短暫。
返回列表 上一主題