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

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

vba助:如何实现excel与txt之间字符串查找与单元格值的替换

VBA助:如何实现excel与txt之间字符串查找与单元格值的替换

如果你的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中文网其它相关文章!

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:Excel办公网网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号