我有一个如下文本文件,我需要使用excel
宏进行修改。
这是它的样子(空格分隔)。但是,并非所有文本文件都以空格分隔。
"W2" "S4" SEC "W1" PF22 0.7 PRM22 0.7 PI "P2"
"W2" "S3" SEC "W1" PF22 0.7 PM22 0.7 PI "P2"
"W2" "S2" SEC "W1" PF22 0.7 PM22 0.7 PI "P2"
"W2" "S1" SEC "W1" PF22 0.7 PM22 0.7 PI "P2"
我想检查每一行,如果该行包含" Sn"和" Pm"等于我在excel文件中的列表对,将0.7s值更改为0.5。
S1 P2
S2 P1
S5 P1
... ...
我试图修改this线程中的代码并且没有成功。
我该怎么办?
答案 0 :(得分:0)
请参阅以下代码。它可能不完美,但它的工作基于OP中的数据样本。它基于chr(34)
,"
的代码。
Sub RepStr()
Dim lastrow As Long
Dim srchList As Worksheet
Dim mainList As Worksheet
Dim sStart As Long
Dim sStop As Long
Dim sValue As String
Dim pStart As Long
Dim pSttop As Long
Dim pValue As String
Set srchList = Sheets("Sheet8") '<- Sn Pn list
Set mainList = Sheets("Sheet7") '<- String List
lastrowMain = mainList.Range("A" & Rows.Count).End(xlUp).Row
lastrowsrch = srchList.Range("A" & Rows.Count).End(xlUp).Row
i = 1
While i <= lastrowMain
'Code based on your string is located at Column A of mainList
sStart = InStr(5, mainList.Range("A" & i).Value, Chr(34)) + 1
sStop = InStr(sStart + 1, mainList.Range("A" & i).Value, Chr(34))
sValue = Mid(mainList.Range("A" & i).Value, sStart, sStop - sStart)
pStart = InStr(InStr(1, mainList.Range("A" & i).Value, "PI"), mainList.Range("A" & i).Value, Chr(34)) + 1
pStop = InStr(pStart + 1, mainList.Range("A" & i).Value, Chr(34))
pValue = Mid(mainList.Range("A" & i).Value, pStart, pStop - pStart)
'Code based on your matching values are located at srchList Column A (S values), Column B (P values)
For j = 1 To lastrowsrch
If srchList.Range("A" & j).Value = sValue And srchList.Range("B" & j).Value = pValue Then
mainList.Range("A" & i).Value = Replace(mainList.Range("A" & i).Value, 0.7, 0.5)
End If
Next j
i = i + 1
Wend
End Sub