使用macrosreplaceExcel文件中的数据
我有一个Excel文件,包含一些二维数组中的数据。
我想要做的是创build一个macros,它可以用表(toto,或tata,或titi)的标题来代替星号'*'。
只使用工作表工具(无VBA):
Ctrl-F
- find什么=〜*
-
Find All
-
Ctrl-A
select所有查找结果 -
Close
查找对话框 - 假设你的头在第二行,并假设光标在某处C列(我的两倍,YMMV),键入公式
=C$2
- 按下
Ctrl-Enter
喜欢这个?
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
这是我想出的一个简单的方法。
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表示列。
一个更精确的范围select可以用来代替这个基本的代码来select合适的范围。
要在Excel中实现,只需打开代码窗口并将其粘贴到所需的macros/子程序中即可。