查找数字文本并将其替换为分数文本
Find and Replace number text to fraction text
我有两张桌子。
Droplist Table 有 2 列:
[HeaderA] 包含查找值
[HeaderA_D] 包含替换值
HeaderA
HeaderA_D
1
1/50
2
1/100
3
1/200
4
1/500
Table1 Table
- [HeaderB] 是我尝试从 Droplist[HeaderA] 中查找匹配值并替换为 Droplist[HeaderA_D]
的列
HeaderB
1
2
3
4
当前结果
HeaderB
18624
1/100
1/200
1/500
预期结果
HeaderB
1/50
1/100
1/200
1/500
问题:
我找不到“1”并将其替换为“1/50”。它不断用 18624 替换它,即 1/1/1950 的数字格式“日期”
我试过了:
我的代码在下面,我确保两者都在 .Numberformat = "Text"
'''
Sub Find_Replace()
Dim b, c As Variant
Dim j As Integer
Set b = Range("Droplist[HeaderA]")
Set c = Range("Droplist[HeaderA_D]")
For j = 1 To Application.WorksheetFunction.CountIf(Range("Droplist[HeaderA]"), "<>")
Range("Table1[HeaderB]").Select
Range("Table1[HeaderB]").NumberFormat = "@"
Application.ReplaceFormat.NumberFormat = "@"
Selection.Replace What:=b(j), Replacement:=c(j), LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=True
Next j
End Sub
'''
我又在VBA上重新执行了你的问题,当我将原始数据更改为文本1/50并继续执行VBA代码时,同样的问题发生在我的excel 再次。我认为这个问题是由excel中的find and replace
函数引起的,为了解决它,最好使用replace
函数,这里是解决方案,如果有帮助,请采纳:)
我的原始数据
具有替换功能的VBA代码:
Sub Find_Replace()
Dim b, c As Variant
Dim replaceValue As String
Dim j As Integer
Dim lastrow As Long, headerLastrow As Long, replaceTime As Long
headerLastrow = Sheet1.Range("A1").End(xlDown).Row
lastrow = Sheet1.UsedRange.Rows.Count
Set b = Sheet1.Range("A1", "A" & lastrow)
Set c = Sheet1.Range("B1", "B" & lastrow)
Sheet1.Range("E2", "E" & lastrow).NumberFormat = "@"
For j = 1 To lastrow
For replaceTime = 1 To headerLastrow
replaceValue = c(replaceTime).Text
Sheet1.Cells(j, 5).Value = Replace(Sheet1.Cells(j, 5).Value, b(replaceTime), replaceValue)
Next
Next j
End Sub
我有两张桌子。
Droplist Table 有 2 列:
[HeaderA] 包含查找值
[HeaderA_D] 包含替换值
HeaderA | HeaderA_D |
---|---|
1 | 1/50 |
2 | 1/100 |
3 | 1/200 |
4 | 1/500 |
Table1 Table
- [HeaderB] 是我尝试从 Droplist[HeaderA] 中查找匹配值并替换为 Droplist[HeaderA_D] 的列
HeaderB |
---|
1 |
2 |
3 |
4 |
当前结果
HeaderB |
---|
18624 |
1/100 |
1/200 |
1/500 |
预期结果
HeaderB |
---|
1/50 |
1/100 |
1/200 |
1/500 |
问题:
我找不到“1”并将其替换为“1/50”。它不断用 18624 替换它,即 1/1/1950 的数字格式“日期”
我试过了:
我的代码在下面,我确保两者都在 .Numberformat = "Text"
'''
Sub Find_Replace()
Dim b, c As Variant
Dim j As Integer
Set b = Range("Droplist[HeaderA]")
Set c = Range("Droplist[HeaderA_D]")
For j = 1 To Application.WorksheetFunction.CountIf(Range("Droplist[HeaderA]"), "<>")
Range("Table1[HeaderB]").Select
Range("Table1[HeaderB]").NumberFormat = "@"
Application.ReplaceFormat.NumberFormat = "@"
Selection.Replace What:=b(j), Replacement:=c(j), LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=True
Next j
End Sub
'''
我又在VBA上重新执行了你的问题,当我将原始数据更改为文本1/50并继续执行VBA代码时,同样的问题发生在我的excel 再次。我认为这个问题是由excel中的find and replace
函数引起的,为了解决它,最好使用replace
函数,这里是解决方案,如果有帮助,请采纳:)
我的原始数据
具有替换功能的VBA代码:
Sub Find_Replace()
Dim b, c As Variant
Dim replaceValue As String
Dim j As Integer
Dim lastrow As Long, headerLastrow As Long, replaceTime As Long
headerLastrow = Sheet1.Range("A1").End(xlDown).Row
lastrow = Sheet1.UsedRange.Rows.Count
Set b = Sheet1.Range("A1", "A" & lastrow)
Set c = Sheet1.Range("B1", "B" & lastrow)
Sheet1.Range("E2", "E" & lastrow).NumberFormat = "@"
For j = 1 To lastrow
For replaceTime = 1 To headerLastrow
replaceValue = c(replaceTime).Text
Sheet1.Cells(j, 5).Value = Replace(Sheet1.Cells(j, 5).Value, b(replaceTime), replaceValue)
Next
Next j
End Sub