vba - 按行连接所有数据并删除数字
问题描述
我需要帮助为 excel 编写代码,该代码将所有活动数据放在一行中并将其连接到一个单元格中,然后让这个循环遍历所有活动行。
我遇到的问题是列和行的数量是完全随机的,所以我只需要它来考虑所有现有数据。
最后,我希望串联只包含字母,所以没有数字或“,”或其他任何东西。
如果你有能力请帮忙。
这是之前和之后的图像,如果有帮助的话。
前
后
解决方案
这可能有效 - 宏避免了每个特殊字符和数字。将该代码复制并粘贴到 VBA 编辑器中的新模块中,然后运行子“CreateString”。
Sub CreateString()
Dim LastRow%: LastRow = ActiveSheet.UsedRange.Rows.Count
Dim strConcatenate$, i%, j%
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.ActiveSheet
ws.Range("A:A").Clear
For i = 1 To LastRow
For j = 2 To LastColumn(i) 'Calls function "LastColumn" to get the last column of each row
strConcatenate = strConcatenate & FindReplace(ws.Cells(i, j))
Next j
ws.Cells(i, 1) = strConcatenate 'This will past the finished string into column [A] in the specific row
strConcatenate = "" 'blanks the string, so the next string in the next row is fresh
Next i
End Sub
Function LastColumn(row%) As Integer
Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
LastColumn = ws.Cells(row, ws.Columns.Count).End(xlToLeft).Column
End Function
Function FindReplace(CellValue$)
Dim strPattern$: strPattern = "[^A-Za-z]+" 'Pattern to only take care of letters
Dim strReplace$: strReplace = "" 'Replace everything else with blank
Dim regex As Object
Set regex = CreateObject("vbscript.regexp")
With regex
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
FindReplace = regex.Replace(CellValue, strReplace) 'RegEx Function replaces the pattern with blank
End Function
推荐阅读
- javascript - TypeScript + Rollup 无法识别 Firebase 导入
- javascript - 在 laravel 刀片中将 HTML 元素转换为同名的数组
- python - 将相同的用户事件聚合到两个不同的窗口中
- python - `docker-compose up`:postgres 连接被拒绝
- npm-install - 如何在 termux 上安装 npm
- c# - wpf netframeworkcore 3.1 mvvm 可见性
- python - 尝试使用 ploty-dash 应用程序连接到 mysql db 时出现错误
- angular - Angular 和 RxJS - catchError() 不会捕获请求错误
- python-3.x - 通过迭代访问嵌入列表
- yarnpkg - 如何使用 yarn 安装最新版本的 jsPDF?