vbcr,vblf和 vbcrlf之间的区别?

本文详细解析了VBCrLf、VBCr、VBLf三种换行符的区别与实际应用效果,通过实例代码对比展示了它们在字符串操作中的差异,并探讨了回车和换行在不同操作系统下的历史与使用场景。
MSDN上说:
vbCrLf
Chr(13) + Chr(10)
回车/换行组合符。

vbCr
Chr(13)
回车符。

vbLf
Chr(10)
换行符。


但是他们下面的代码的实际效果都是一样的,那请问到底有什么的区别?

MsgBox(vbCrLf & "a ")

MsgBox(vbCr & "a ")

MsgBox(vbLf & "a ")
======================
再例如:
dim a as string = "1 23 456 789 "
dim b() as string = a.split(new char(){vbcr})
dim b() as string = a.split(new char(){vblf})

同样结果没什么区别
__________________________________________________________________________
试试
dim a as string = "1 " & vbcr & vblf & "23 " & vbcr & vblf & "456 " & vbcr & vblf & "789 "
dim b() as string = a.split(new char(){vbcr})
dim c() as string = a.split(new char(){vblf})
dim d() as string = a.split(new char(){vbcr,vblf})
dim e() as string =split(a,vbcrlf)
仔细分析分割之后的字符串数组,比如长度,子串的每个字符等等,就可以发现不同之处
__________________________________________________________________________
回车和换行是两个字符,我记得历史是这样的(记不太清楚了,好像是这样):

在早期的Mac系统下用换行符来实现文字的换行,这是比较正统的做法,而在Unix系统下用回车符代表文字的换行,二者都是不可见字符,Dos出现以后,比尔盖茨为了实现Dos下的文档在Mac和Unix系统下都可读(即都实现换行效果),就采取了用二者的结合来实现换行,这样Dos文档在Mac和Unix下都不会有显示问题。这也是为什么有时候Unix的文本文件到了Win下面就所有文字都在一行里显示了...
__________________________________________________________________________
在DOS时代,这两个字符是有各自的分工。CR使光标回到行首,LF使光标下移一行。

不过在WINDOWS里面,基本只要有CR就可以了,就是说CR兼备了LF的功能。而LF则变得可有可无,只在一些终端界面例如TELNET里面才能起到作用。

转载于:https://www.cnblogs.com/ewyb/archive/2011/08/30/2159128.html

帮我检查以下脚本是不是有什么错误 Private Sub CommandButton1_Click() Dim sourceFolder As String, templatePath As String, targetFolder As String Dim sourceSheet As String, targetSheet As String Dim sourceRange As String, targetRange As String Dim fileDialog As fileDialog Dim sourceFiles As Collection Dim sourceFile As Variant Dim wbSource As Workbook, wbTarget As Workbook Dim cellValues As Variant Dim i As Integer ' 获取用户输入配置 sourceFolder = InputBox("请输入源文件夹路径:", "配置源文件夹", "E:\Tools\before") If sourceFolder = "False" Then Exit Sub ' 用户取消选择 targetFolder = InputBox("请输入目标文件夹路径:", "配置目标文件夹", "E:\Tools\after") If targetFolder = "False" Then Exit Sub ' 用户取消选择 ' 选择模板文件 Set fileDialog = Application.fileDialog(msoFileDialogFilePicker) With fileDialog .Title = "选择模板文件" .AllowMultiSelect = False .Filters.Clear .Filters.Add "Excel Files", "*.xlsx;*.xlsm;*.xls" If .Show = -1 Then templatePath = .SelectedItems(1) End With ' 获取工作表名称配置 sourceSheet = "SourceCode" targetSheet = "表紙" ' 获取单元格范围配置 sourceRange = "B1,G2" targetRange = "D8,D7" ' 获取源文件列表 Set sourceFiles = GetFileList(sourceFolder, "*.xls*") If sourceFiles.Count = 0 Then MsgBox "源文件夹中未找到Excel文件", vbExclamation Exit Sub End If Application.ScreenUpdating = False Application.DisplayAlerts = False ' 处理每个源文件 For Each sourceFile In sourceFiles ' 打开源文件 Set wbSource = Workbooks.Open(sourceFile) ' 读取源数据 cellValues = Split(sourceRange, ",") For i = LBound(cellValues) To UBound(cellValues) cellValues(i) = wbSource.Sheets(sourceSheet).Range(Trim(cellValues(i))).Value Next i ' ====== 新增功能:提取A4内容并处理 ====== Dim sourceText As String Dim textLines() As String Dim verIndex As Long Dim validLines As Collection Dim outputRow As Long Dim k As Long ' 读取A4单元格内容 sourceText = wbSource.Sheets(sourceSheet).Range("A4").Value ' 替换所有换行符为统一的 vbLf(兼容 vbCr/vbCrLf) sourceText = Replace(sourceText, vbCr, vbLf) sourceText = Replace(sourceText, vbLf & vbLf, vbLf) ' 去除连续空行 ' 分割成行数组 textLines = Split(sourceText, vbLf) ' 初始化集合存储有效行 Set validLines = New Collection ' 查找最后一个包含"Ver"的行索引(不区分大小写) verIndex = -1 For k = UBound(textLines) To 0 Step -1 If InStr(1, textLines(k), "Ver", vbTextCompare) > 0 Then verIndex = k Exit For End If Next k ' 收集 verIndex 之后的有效行(跳过空白行) For k = IIf(verIndex >= 0, verIndex + 1, 0) To UBound(textLines) If Trim(textLines(k)) <> "" Then validLines.Add textLines(k) End If Next k ' 可选:将 validLines 内容输出到工作表 outputRow = 1 For Each line In validLines ThisWorkbook.Sheets("Sheet1").Cells(outputRow, 1).Value = line outputRow = outputRow + 1 Next line ' ====== 新增功能结束 ====== wbSource.Close False ' 创建新文件 Set wbTarget = Workbooks.Open(templatePath) Dim newFileName As String newFileName = targetFolder & "\" & Split(Dir(sourceFile), ".")(0) & "_UnitTestSpec.xlsm" ' 写入目标文件 (原有功能) Dim targetCells As Variant targetCells = Split(targetRange, ",") For i = LBound(targetCells) To UBound(targetCells) wbTarget.Sheets(targetSheet).Range(Trim(targetCells(i))).Value = cellValues(i) Next i ' ====== 新增功能:写入目标工作表 ====== If validLines.Count > 0 Then With wbTarget.Sheets("ソース確認") outputRow = 5 ' 从A5开始 For k = 1 To validLines.Count .Cells(outputRow, 1).Value = validLines(k) outputRow = outputRow + 1 Next k End With End If ' ====== 新增功能结束 ====== ' 保存并关闭 wbTarget.SaveAs newFileName wbTarget.Close True Next sourceFile Application.ScreenUpdating = True Application.DisplayAlerts = True MsgBox "处理完成!共处理 " & sourceFiles.Count & " 个文件", vbInformation End Sub Function GetFileList(folderPath As String, fileFilter As String) As Collection Dim fileList As New Collection Dim fileName As String If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\" fileName = Dir(folderPath & fileFilter) Do While fileName <> "" fileList.Add folderPath & fileName fileName = Dir Loop Set GetFileList = fileList End Function
最新发布
09-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值