[英]Need a jumpstart with Excel VBA object model
我不太會使用Excel,也永遠不會作為真正的電子表格/計算器使用。 我得到了列表文本數據,並根據一組規則對其進行了格式化。 到現在為止,這已經可以手動進行管理了,但是變得笨拙了,所以我想我應該嘗試使其自動化。 然后我查看了Excel對象模型,然后……哇。
我毫不費力地列出了過程的邏輯,但是插入正確的對象,方法等是一場噩夢。 在這方面的任何幫助,我將不勝感激。 這是邏輯/偽代碼:
for each cell "x" in a selected range (in a single column)
if "x" is not blank
for each cell "y" in selection after current "x"
if text in "y" = text in "x"
change format of "y" to right, red ("is a repeat")
if FG color of "x" is blue
change format of "x" to left, black ("is repeated")
end if
end if
end if
next "y"
next "x"
我什至在模型中找不到名為“ cell”的對象。
'針對選定范圍內的每個單元格“ x”(在單列中)
Dim x as Range
For each x in selection
如果“ x”不為空
if x <> "" then
對於當前“ x”之后選擇的每個單元格“ y”
Dim y as range
for each y in range(y.address & ":" & cells(selection.row.count,y.column).address)
編輯:發現錯誤! 那應該是Range(X.Offset(1,0).address等中的每個y
如果“ y”中的文本=“ x”中的文本
If x.text = y.text then
將“ y”的格式更改為右側,紅色(“重復”)'不要問太多!
y.NumberFormat = "[Red]" & Chr$(34) & "Is a Repeat" & Chr$(34) & ";[Red]" & Chr$(34) & "Is a Repeat" & Chr$(34) & ";[Red]" & Chr$(34) & "Is a Repeat" & Chr$(34) & ";[Red]" & Chr$(34) & "Is a Repeat" & Chr$(34)
End Sub y.HorizontalAlignment = xlright
如果“ x”的FG顏色為藍色
if x.interior.color = vbblue then'or use rgb function
將“ x”的格式更改為左側,黑色(“重復”)
x.NumberFormat = Chr$(34) & "Is a Repeat" & Chr$(34) & ";" & Chr$(34) & "Is a Repeat" & Chr$(34) & ";" & Chr$(34) & "Is a Repeat" & Chr$(34) & ";" & Chr$(34) & "Is a Repeat" & Chr$(34)
x.horizontalalignment = xlright
end if
end if
end if
next y
next x
多虧了這里提供的線索,我才得以做到這一點-對方法沒有深入的了解。 ( - :
我敢肯定,有更好的方法來定義“所選內容中當前單元格以下”的每個單元格的范圍”,但這確實可行(刪除了一些多余的內容)。
批評受到歡迎-這就是我的學習方式。
Sub FormatColors()
Dim Outer As Range
Dim Inner As Range
Dim txtColumn As String
Dim RangeEnd As String
Dim txtInner As String
With Selection
txtColumn = Chr(.Column + 64) 'limited to A-Z
RangeEnd = ":" & txtColumn & Trim(Str(.Row + .Rows.Count))
.Font.Color = vbBlue
.Cells.HorizontalAlignment = xlCenter
End With
For Each Outer In Selection
If Not IsEmpty(Outer) Then
txtInner = txtColumn & Trim(Str(Outer.Row + 1)) & RangeEnd
For Each Inner In Range(txtInner)
If Inner.Text = Outer.Text Then
Inner.HorizontalAlignment = xlRight
Inner.Font.Color = vbRed
If Outer.Font.Color = vbBlue Then
Outer.HorizontalAlignment = xlLeft
Outer.Font.Color = vbBlack
End If
End If
Next
End If
Next
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.