我有一个Excel文件,其中包含2d数组中的一些数据。
我想要做的是创建一个宏,它可以用表格的标题(toto,或tata或titi)替换星号'*'。
答案 0 :(得分:5)
喜欢这个吗?
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
答案 1 :(得分:3)
仅使用工作表工具(无VBA):
Ctrl-F
Find All
Ctrl-A
选择所有查找结果Close
“查找”对话框=C$2
Ctrl-Enter
答案 2 :(得分:0)
这是我提出的一种简单方法。
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
单元格(x,y):x表示行,y表示列。
可以使用更精确的范围选择而不是此基本选择来使代码选择适当的范围。
要在excel中实现,只需打开代码窗口并将此代码粘贴到所需的宏/子例程中。