首页 > 软件教程 > 办公软件 > VBA教程:在Excel和文本文件之间进行字符串搜索和单元格值替换的方法

VBA教程:在Excel和文本文件之间进行字符串搜索和单元格值替换的方法

WBOY
发布: 2024-01-23 23:45:31
转载
623 人浏览过

VBA教程:在Excel和文本文件之间进行字符串搜索和单元格值替换的方法

VBA教程:在Excel和文本文件之间进行字符串搜索和单元格值替换的方法

如果你的txt文件使用tab分隔符,那么以下代码可以符合你的要

Sub ChangeTxt()

Dim FileN, TxtWb As Workbook, ToFindData As String

Dim ToSubData As String, c As Range, FirstAdr As String

If MsgBox("是否已经选中待查找的单元格?", vbYesNo) = vbNo Then Exit Sub

ToFindData = Selection.Cells(1).Value

ToSubData = Selection.Cells(1).Offset(, 1).Value

FileN = Application.GetOpenFilename("Txt文件,*.txt", , "选择txt文件")

If TypeName(FileN) = "Boolean" Then Exit Sub

Workbooks.OpenText Filename:=FileN, consecutivedelimiter:=False, _

Tab:=True, Space:=False

Set TxtWb = ActiveWorkbook

Set c = TxtWb.Sheets(1).UsedRange.Find(What:=ToFindData, _

LookAt:=xlPart, MatchCase:=False, matchbyte:=False, SearchFormat:=False)

If Not c Is Nothing Then

FirstAdr = c.Address

Do

c.Offset(, 4) = ToSubData

Set c = TxtWb.Sheets(1).UsedRange.FindNext(c)

Loop Until c.Address = FirstAdr

TxtWb.Close savechanges:=True

MsgBox "替换完毕"

Else

TxtWb.Close False

MsgBox "未找到,请选中要查找的单元格。"

End If

Set c = Nothing

Set TxtWb = Nothing

End Sub

excel vba内容替换

Sub s()

Dim c As Range

n = Cells(Rows.Count, "t").End(3).Row

Set rg = Range("b6:t" & n)

rg.HorizontalAlignment = xlCenter

t = InputBox("输入要查找的数字")

For Each c In rg

If c "" And c "√" And c "X" Then

If InStr(c, t) >0 Then

c = "√"

Else

c = "X"

End If

End If

Next

For Each c In rg

If c = "√" Then

c.Font.Bold = True

c.Font.Color = vbBlue

ElseIf c = "X" Then

c.Font.Bold = True

c.Font.Color = vbRed

End If

Next

End Sub

遍历工作表查找字符串并替换 VBA

Range("A1").Select

Selection.Copy

Cells.Find(What:="*照明*", After:=ActiveCell, LookIn:=xlFormulas, LookAt _

:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _

False, MatchByte:=False, SearchFormat:=False).Activate

Cells.FindNext(After:=ActiveCell).Activate

Cells.FindNext(After:=ActiveCell).Activate

Cells.FindNext(After:=ActiveCell).Activate

Cells.FindNext(After:=ActiveCell).Activate

Cells.FindNext(After:=ActiveCell).Activate

Cells.FindNext(After:=ActiveCell).Activate

ActiveCell.Replace What:="*照明*", Replacement:="马尼公司照明*30倍", LookAt:= _

xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _

ReplaceFormat:=False

Cells.FindNext(After:=ActiveCell).Activate

ActiveCell.Replace What:="*照明*", Replacement:="马尼公司照明*30倍", LookAt:= _

xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _

ReplaceFormat:=False

Cells.FindNext(After:=ActiveCell).Activate

ActiveCell.Replace What:="*照明*", Replacement:="马尼公司照明*30倍", LookAt:= _

xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _

ReplaceFormat:=False

Cells.FindNext(After:=ActiveCell).Activate

ActiveCell.Replace What:="*照明*", Replacement:="马尼公司照明*30倍", LookAt:= _

xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _

ReplaceFormat:=False

Cells.FindNext(After:=ActiveCell).Activate

ActiveCell.Replace What:="*照明*", Replacement:="马尼公司照明*30倍", LookAt:= _

xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _

ReplaceFormat:=False

Cells.FindNext(After:=ActiveCell).Activate

以上是VBA教程:在Excel和文本文件之间进行字符串搜索和单元格值替换的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:docexcel.net
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板