进行search和replace时,保留Excel中单元格的数据types

一个webquery将从一个目录中的所有图像名称拉到excel成功

使用replace通过vbareplace“_lg.jpg”和“.jpg”在整个导入范围内工作正常

但是,在图像是“2496-6-4_lg.jpg”的情况下,删除“_lg.jpg”导致excel将新值解释为date6/3/2496,即使我首先将单元格格式化为文本

我已经在QueryTables.Add(Connection:=代码的一部分)内尝试了任何数量的设置更改它似乎像.PreserveFormatting = True .WebFormatting = xlWebFormattingNone .WebDisableDateRecognition = True

可能会影响最后的结果,但是改变它们中的任何一个都不会禁止将图像名称转换成当扩展名被删除时的date

另外我明确地遍历使用范围

 For Each cell In Range("E2:E" & lastPhoto) cell.Select ActiveCell = ActiveCell.FormulaR1C1 Next cell 

这强制他们的文本格式,因为每个单元格已被格式化为文本,但这些已经转换为date并没有帮助

所有这些都是为了比较从db查询输出的文本来查找缺失的图像名称

除了这些被误解的date之外,一切都很好

预先安排一个apostrohe不是一个好的解决scheme,因为它可能会打破用于比较的查找

有没有办法做一个searchreplace和保存结果值为文本,这显然是在删除扩展之前?


图像名称如何被拉入

 With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://images.mysite.com/" & mfr, Destination:=Range("$E$2")) .Name = mfr .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlAllTables .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = True .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With 

—更多信息在replace之前,我将范围格式化为文本

 Selection.NumberFormat = "@" Selection.Replace What:="_lg.jpg", Replacement:="", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False 

这似乎是ReplaceFormat:=False应该做我想要的,但它不

如果将范围格式化为Selection.NumberFormat = "'@"则修复date问题,并打破其余部分

Selection.NumberFormat之前尝试添加Selection.Value = "'" & Selection.Value – 这个行为不同于用"'@"格式化