gpt4 book ai didi

vba - 使用宏替换 Excel 文件中的数据

转载 作者:行者123 更新时间:2023-12-03 00:03:54 24 4
gpt4 key购买 nike

我有一个 Excel 文件,其中包含二维数组中的一些数据。

Excel document

我想要做的是创建一个宏,它可以用表的列标题(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 - 使用宏替换 Excel 文件中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10722451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com