2次元配列のデータを含むExcelファイルがあります。
私がやりたいのは、アスタリスク「*」をテーブルの列のヘッダー(toto、tata、またはtiti)で置き換えることができるマクロを作成することです。
このような?
Option Explicit
Sub Sample()
Dim oRange As Range, aCell As Range, bCell As Range
Dim ws As Worksheet
Dim ExitLoop As Boolean
On Error GoTo Whoa
'~~> Change this to the relevant sheet name
Set ws = Worksheets("Sheet1")
Set oRange = ws.Cells
Set aCell = oRange.Find(What:="~*", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
'~~> Assuming that the headers are in row 2
aCell.Value = Cells(2, aCell.Column)
Do While ExitLoop = False
Set aCell = oRange.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
'~~> Assuming that the headers are in row 2
aCell.Value = Cells(2, aCell.Column)
Else
ExitLoop = True
End If
Loop
End If
Exit Sub
Whoa:
MsgBox Err.Description
End Sub
ワークシートツールのみを使用する(VBAなし):
Ctrl-F
Find All
Ctrl-A
すべての検索結果を選択するにはClose
検索ダイアログ=C$2
Ctrl-Enter
これが私が思いついた簡単な方法です。
i = 3
While Cells(2, i).Value <> ""
Range(Cells(3, i), Cells(6, i)).Select
Selection.Replace What:="~*", Replacement:=Cells(2, i).Value, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
i = i + 1
Wend
Cells(x、y):xは行を示し、yは列を示します。
この基本的な範囲の代わりに、より洗練された範囲選択を使用して、コードに適切な範囲を選択させることができます。
Excelで実装するには、コードウィンドウを開き、このコードを目的のマクロ/サブルーチンに貼り付けるだけです。