[英]VBA Finding Numbers, Letters, and Characters in Cell and Replacing Content of Cell With Only Numbers/Letters
Having another problem. 有另一个问题。
I am creating another Excel tool at my facility and currently what I have pulls data from a purchase order tracking website and filters out superfluous data, leaving only the purchase order label itself, a single quotation mark, and an end bracket. 我正在我的工厂创建另一个Excel工具,目前我从采购订单跟踪网站提取数据并过滤掉多余的数据,只留下采购订单标签本身,单引号和结束括号。 I need to remove the quotation mark and ending bracket so I only have the PO itself, as I need this to inject into another site's URL. 我需要删除引号和结束括号,所以我只有PO本身,因为我需要这个注入到另一个站点的URL。 I have tried using wildcards with some code I wrote however the PO will get replaced with several asterisks, aka "wildcards," instead. 我已经尝试使用通配符和我编写的一些代码但是PO将被替换为几个星号,也就是“通配符”。 I am probably overlooking something obvious, but I can't figure it out. 我可能忽视了一些显而易见的事情,但我无法弄明白。
Example of data: 数据示例:
Code example: 代码示例:
Sub Filter_DockMaster_Data2()
Dim Main As Worksheet
Set Main = Worksheets("Main")
Dim ISA_List As Worksheet
Set ISA_List = Worksheets("ISA_List")
Dim ISA_Results As Worksheet
Set ISA_Results = Worksheets("ISA_Results")
Dim ISA_Raw As Worksheet
Set ISA_Raw = Worksheets("ISA_Raw")
Worksheets("ISA_Results").Select
Range("A1").Select
Do Until IsEmpty(ActiveCell)
ActiveCell.replace What:="********"" ]", Replacement:="********", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Hopefully this makes sense. 希望这是有道理的。
Quick notes, the length of the PO will vary as time goes on, so I would like to make it dynamic if possible. 快速注释,PO的长度会随着时间的推移而变化,所以我想尽可能让它变得动态。 Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。
How about the function intended for making text URL-friendly? 如何用于制作文字网址友好的功能? :) :)
=ENCODEURL(A1)
For example, this: 例如,这个:
1234ABCD']
...becomes encoded as: ...变成编码为:
1234ABCD%27%5D
...ready to insert in a query parameter (URL) string. ...准备插入查询参数(URL)字符串。 See the documentation including further examples at at the link below. 请参阅以下链接中的文档,包括更多示例。
LEFT
文字函数如LEFT
That said, there are several other ways to do this. 也就是说,还有其他几种方法可以做到这一点。
You said "replace" but it looks like you just need to cut off the last 2 characters? 你说“替换”,但看起来你只需要切断最后2个字符?
This cuts the last 2 characters of the text in A1
: 这会减少A1
本的最后2个字符:
=LEFT(A1,LEN(A1)-2)
SUBSTITUTE
Function SUBSTITUTE
功能 If you do want to "replace" text in a cell, you can use SUBSTITUTE
: 如果您确实要“替换”单元格中的文本,可以使用SUBSTITUTE
:
Example: 例:
If cell A1
contains: 如果单元格A1
包含:
1234ABCD']
...you could enter in another cell: ......你可以进入另一个细胞:
=SUBSTITUTE(A1,"]","")
...which would remove only the ]
. ......只会删除]
。 You can also nest functions. 你也可以嵌套功能。 To remove both the ]
and the '
, use this formula instead: 要删除]
和 '
,请使用此公式:
=SUBSTITUTE(SUBSTITUTE(A1,"]",""),"'","")
The SUBSTITUTE
function syntax: SUBSTITUTE
函数语法:
SUBSTITUTE(
text, old_text, new_text, [instance_num]
)
SUBSTITUTE(
text, old_text, new_text, [instance_num]
)
The SUBSTITUTE
function syntax has the following arguments: SUBSTITUTE
函数语法具有以下参数:
Text
- (Required) The text or the reference to a cell containing text for which you want to substitute characters. Text
- (必需)包含要替换字符的文本的单元格的文本或引用。 Old_text
- (Required) The text you want to replace. Old_text
- (必需)要替换的文本。 New_text
- (Required) The text you want to replace old_text
with. New_text
- (必需)要用old_text
替换old_text
的文本。 Instance_num
- (Optional) Specifies which occurrence of old_text
you want to replace with new_text
. Instance_num
- (可选)指定old_text
替换的new_text
。 If you specify instance_num
, only that instance of old_text
is replaced. 如果指定instance_num
,则仅替换old_text
该实例。 Otherwise, every occurrence of old_text
in text is changed to new_text
. 否则,文本中每次出现的old_text
都会更改为new_text
。 ( Source & More Info ) ( 来源及更多信息 )
ENCODEURL
function (Excel) Microsoft支持: ENCODEURL
函数 (Excel) MID
, LEN
, and FIND
functions to extract certain cell portions? 堆栈溢出: 使用MID
, LEN
和FIND
函数来提取某些单元格部分? SUBSTITUTE
Function Microsoft支持: SUBSTITUTE
功能 LEFT
, RIGHT
and MID
formulas in Excel JayTray: Excel中的LEFT
, RIGHT
和MID
公式 Your problem shouldn't be about repairing the the data you've extracted from some source. 您的问题不应该是修复从某些来源提取的数据。 It really should be about fixing the retrieval procedure so you do not get rogue characters. 它应该是关于修复检索程序,所以你不会得到流氓字符。
With that said, this sub procedure should quickly remove any rogue characters from column A. 话虽如此,这个子程序应该快速删除A列中的任何流氓字符。
Option Explicit
Sub posOnly()
Dim i As Long, str As String, rgx As Object
Set rgx = CreateObject("VBScript.RegExp")
'pattern for A-Z (case sensitive) or 0-9 exactly 8 characters/digits in length
rgx.Pattern = "[A-Z0-9]{8}"
With Worksheets("ISA_Results")
For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
str = .Cells(i, "A").Value2
If rgx.Test(str) Then
str = rgx.Execute(str).Item(0)
.Cells(i, "A") = str
End If
Next i
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.