返回列表 上一主題 發帖

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

本帖最後由 FAlonso 於 2011-1-19 14:33 編輯

自己做了一個,給其他會員看看,excel文件稍為做了一些修改,把空column給刪掉
不能下載的話,請看下列程式
  1. Sub chooseyellowcell2()
  2. Dim i As Integer, myfinalrow As Integer
  3. Dim mycell As Range, mycolumn As Range, mycell2 As Range, mytarget As Range, mybaseline As Range, mycheck As Range
  4. Dim checkstatus As Boolean

  5. ThisWorkbook.Activate

  6. For i = 2 To Worksheets.Count
  7.     Sheets(i).Activate
  8.     myfinalrow = ActiveSheet.Range("A2").End(xlDown).Offset(1).Row
  9.         For Each mycell In ActiveSheet.Range("A1", Range("IV1").End(xlToLeft))
  10.             If mycell.Value <> "" Then
  11.             Set mycolumn = Range(mycell, mycell.End(xlDown))
  12.                 For Each mycell2 In mycolumn
  13.                     If mycell2.Interior.ColorIndex = 6 Then
  14.                         checkstatus = False
  15.                         Set mybaseline = mycell2.Offset(1)
  16.                         Set mycheck = mycell2
  17.                             If mybaseline.Value = "" Then
  18.                             mybaseline.Value = "無法計算"
  19.                             Exit For
  20.                             Else
  21.                             Do
  22.                                 If mycheck.Value >= mybaseline.Value Then
  23.                                 ActiveSheet.Cells(myfinalrow, mycheck.Column) = mybaseline.Row - mycheck.Row
  24.                                 checkstatus = True
  25.                                 Exit Do
  26.                                 Else
  27.                                 Set mycheck = mycheck.Offset(-1)
  28.                                 End If
  29.                             Loop While checkstatus = False And IsNumeric(mycheck) = True
  30.                                 If checkstatus = False Then
  31.                                 mycheck.End(xlDown).Offset(1).Value = "無填權"
  32.                                 End If
  33.                             End If
  34.                     End If
  35.                 Next
  36.             End If
  37.         Next
  38. Next

  39. End Sub
複製代碼
計算除權日.rar (289.3 KB)
80 字節以內
不支持自定義 Discuz! 代碼

TOP

回復 20# Hsieh


你好~
但是每個年度有除權的公司不太一樣~
所以要把它合併到同一張工作表中似乎有一點困難!
謝謝!

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

回復 23# Hsieh
謝謝你的幫忙!
我目前是分兩個程式執行:
第一:先將所有股價的欄位都刪除
Sub delcol()
Dim i%, c As Range
Set c = Columns(2)
For i = 4 To [iv1].End(1).Column Step 2
Set c = Union(c, Columns(i))
Next
c.Delete
End Sub
第二:再將空格刪除,把資料往上補,在後再將其結果轉置。
Sub ex1()
Columns("B:CF").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
End Sub
因為我後來想說,我先不要設定所謂有完成填權的天數,
這樣彈性比較大!而我到時候想看"幾天"完成填權的公司,
再用EXCEL篩選功能即可!
真的很謝謝你耐心的幫忙!
^^

TOP

        靜思自在 : 口說好話、心想好意、身行好事。
返回列表 上一主題