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
- - - -
ユーザーがドロップダウンから値を選択した後、選択した値に応じて、既存の列の両側にさらに列を追加するための2番目のマクロが必要です。
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