用vba中的特定条件替换字符串的特定部分

时间:2018-04-12 10:12:43

标签: regex vba access-vba

我正在尝试用空格替换字符串的特定部分。

例如,我的列的值为"I LOVE 10 IN20 records from INDIA" - 在此处,我需要将(Space)IN替换为仅‘IN’。即I LOVE 10IN20 records from INDIA。 我尝试了替换功能,它也替换了(空间)INDIA,这在我的情况下不应该发生。

正确输出I LOVE 10IN20 records from INDIA

输出错误I LOVE 10IN20 records fromINDIA

我尝试过如下的vba功能。仍然不会考虑空间(这是我到这里的问题)

Function MyReplace(AddressLine As String, FullName As String, Abbrev As String)
MyReplace = Trim(Replace(" " & AddressLine & " ", _
                                  " " & FullName & " ", _
                                  " " & Abbrev & " "))
End Function

我试图在Access中编写VBA函数,有人可以帮助模式匹配,如上例所示吗?

2 个答案:

答案 0 :(得分:1)

您的问题只询问一个具体示例:require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; class Reports extends Admin_Controller { public function index() { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Hello World !'); $spreadsheet->setActiveSheetIndex(0); $filename = 'hello world'; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"'); header('Cache-Control: max-age=0'); $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); } }

"I LOVE 10 IN20 records from INDIA"

针对该特定案例的解决方案:

Correct Output: I LOVE 10IN20 records from INDIA

Wrong Output:   I LOVE 10IN20 records fromINDIA

如果您有其他情况,您应该提供,或提供更换的确切模式

答案 1 :(得分:0)

这取决于你如何定义你的任务&业务逻辑。根据您提供的信息,LTrim()可以很好地工作,因为您在IN之后也提供了一个空格作为输入:

Public Sub TestMe()    
    Dim strInput As String
    strInput = "I LOVE 10 IN 20 records from INDIA"
    Debug.Print MyReplace(strInput, " IN ")    
End Sub

Function MyReplace(strInput As String, replaceOnly As String) As String
    MyReplace = Replace(strInput, replaceOnly, LTrim(replaceOnly))
End Function