2013-03-17 177 views
2

screeshot of the excel嗨,我需要獲取單元格的文本列作爲操作。使用vba獲取特定文本的單元格列號

我現在的代碼如下。

Option Explicit 
Private Sub Worksheet_Change(ByVal Target As Range) 
Dim rngDV As Range 
Dim oldVal As String 
Dim newVal As String 
Dim actionColName As String 
If Target.Count > 1 Then GoTo exitHandler 

On Error Resume Next 
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation) 
On Error GoTo exitHandler 

If rngDV Is Nothing Then GoTo exitHandler 

If Intersect(Target, rngDV) Is Nothing Then 
    'do nothing 
Else 
    Application.EnableEvents = False 
    newVal = Target.Value 
    Application.Undo 
    oldVal = Target.Value 
    Target.Value = newVal 
    If Target.Column = 3 Then 
    If oldVal = "" Then 
     'do nothing 
     Else 
     If newVal = "" Then 
     'do nothing 
     Else 
     Target.Value = oldVal _ 
     & "+ " & newVal 
     End If 
    End If 
    End If 
End If 

exitHandler: 
    Application.EnableEvents = True 
End Sub 

在上面的代碼有如下的條件 如果Target.Column = 3然後

而不是硬編碼與3的值我想應用此邏輯爲含有完整的列值ACTION在該列的其​​中一個單元格中。

+0

你能請張貼我已經發布了樣本文件 – brettdj 2013-03-18 11:22:37

+0

(第一)列? – user1668653 2013-03-20 05:28:34

+0

哪裏是示例文件示例文件 – brettdj 2013-03-20 06:23:00

回答

5

使用Find確定包含行動

Sub GetAction() 
Dim rng1 As Range 
Set rng1 = ActiveSheet.UsedRange.Find("Action", , xlValues, xlWhole) 
If Not rng1 Is Nothing Then 
MsgBox "Found in column " & rng1.Column 
Else 
MsgBox "Not found", vbCritical 
End If 
End Sub 
+0

感謝它的工作。請讓我知道如何編輯下拉列表中的值,而不是更新excel中的列表? – user1668653 2013-03-18 07:08:48

+0

感謝它的工作。我面臨的問題是我在特定的列上應用上述邏輯。該列的每個單元格是一個下拉列表。第一次,我將從下拉列表中選擇一個值,然後第二次如果我選擇其他值從相同的單元格下拉,然後新值將附加「+」符號與現有值。同時添加第二個值。例如,我從下拉選擇了一個值「one」。如果我想添加一個新的在「飛行」中給「下降」一些值,然後我將看到一個+一個+千個而不是一個+千個。 – user1668653 2013-03-18 07:20:53

相關問題