,如果我有在Excel中以下內容:檢查細胞在Excel中有數據,然後向列添加細胞向左
A B C (columns)
a b c (data)
d e f (data)
g h i (data)
- - - (empty)
及以下驗證下拉:
With rng.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="1,2"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
首先,我需要使用VBA來檢查,看看如果單元格的數據,如果是在驗證下拉菜單添加到左邊的新列/單元格如下:
A B C D
1,2 a b c
1,2 d e f
1,2 g h i
- - - -
繼用戶從下拉菜單中選擇一個值,我需要一個第二宏,以進一步添加列選擇取決於值的現有列的任一側:
A B C D E F G
1 a 1 b 1 c 1 (if 1 selected from dropdown)
2 d 2 e 2 f 2 (if 2 selected from dropdown)
2 g 2 h 2 i 2 (if 2 selected from dropdown)
我在VBA是一個真正的初學者所以任何非常感謝幫助。
=======編輯================================
我有摸索出的這第一部分,其餘部分仍然證明是一個痛苦:
Sub changeClass()
Dim rng As Range
Dim r As Range
Set rng = Range(Cells(6, 2), Cells(6, 2).End(xlDown))
Dim rCell As Range
For Each rCell In rng.Cells
rCell.Offset(0, -1).Value = "Data"
Next rCell
For Each rCell In rng.Cells
With rng.Offset(0, -1).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$A$1:$A$3"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next rCell
End Sub
以及如何插入新列,但不能插入新的數據:
Sub newColumn()
Dim rng As Range
Dim crng As Range
Dim r As Range
With ActiveSheet
LastCol = .Cells(5, .Columns.Count).End(xlToLeft).Column
End With
Set rng = Range(Cells(6, 1), Cells(6, 1).End(xlDown))
Set crng = Range(Cells(5, 1), Cells(5, LastCol))
Set drng = Range(Cells(4, 1), Cells(4, LastCol))
Dim rCell As Range
Dim cCell As Range
Dim dCell As Range
For Each rCell In rng.Cells
For Each cCell In crng.Cells
cCell.Offset(-1, 0).Value = "columnMark"
Next cCell
Next rCell
For Each dCell In drng.Cells
If dCell.Value = "columnMark" Then
dCell.EntireColumn.Offset(0, 1).Insert
End If
dCell.Value = ""
Next dCell
End Sub
你試過了什麼? – 2013-03-26 16:55:07
我嘗試了各種各樣的東西; xlLeft是我以各種組合的形式出現的,偏移量(0,-1)是另一個,但我無法弄清楚如何將它應用到具有數據單元格的整列。沒有什麼似乎爲我工作。 – Alex 2013-03-26 17:01:16
在你看來,這會是一件大事嗎?謝謝。 – Alex 2013-03-26 17:06:55