【发布时间】:2015-02-11 11:02:08
【问题描述】:
我在一个文件夹中有 50 个 .xlsx 或 .csv 文件。我想用新数据更改特定于标题“PlantNo”的数据。
假设旧数据是“35”,现在我想用“1”替换特定列文件夹中所有 Excel 工作簿的旧数据,而不是整个数据。
我已经编写了将所有 This 更改为 That 的代码,但我希望这仅限于一列
【问题讨论】:
-
请添加您的代码
我在一个文件夹中有 50 个 .xlsx 或 .csv 文件。我想用新数据更改特定于标题“PlantNo”的数据。
假设旧数据是“35”,现在我想用“1”替换特定列文件夹中所有 Excel 工作簿的旧数据,而不是整个数据。
我已经编写了将所有 This 更改为 That 的代码,但我希望这仅限于一列
【问题讨论】:
在回答问题时,您可以使用以下内容:
Sub sbOneColumnReplace()
Application.ScreenUpdating = False 'Disable flicker on screen
Dim lLastCol As Long, lNumCol As Long, lLastRow As Long
Dim sTitleCol As String
Dim wsThis As Worksheet
'Here add/define the workbook
Set wsThis = Worksheets(1) 'Select first sheet
lLastCol = wsThis.Cells(1, Columns.Count).End(xlToLeft).Column 'Last column in a specific row
For lNumCol = 1 To lLastCol 'Loop from the first column to the last one found
sTitleCol = wsThis.Cells(1, lNumCol).Value 'I suppose the Title is in the first row, if not change the number value
If sTitleCol = "PlantNo" Then 'This search the title vlaue.If you want it, you could use also vLookup with the defined range
wsThis.Columns(lNumCol).Replace _
What:="35", Replacement:="1", _
SearchOrder:=xlByColumns, MatchCase:=True
End If
Next lNumCol
Application.ScreenUpdating = True
End Sub
未经测试 但重要的是要注意,实现它可能是更好/不同的方法,并且正如@Katz 提到的那样,添加代码或参考很重要,以表明您已经搜索了解决问题的方法并反映了您调查的问题,并加强/可能会更正该知识,如果您只发布问题而没有任何这些,有时(如果不是总是)它会被每个人跳过(相信我,我也是新手)。希望它可以帮助您朝着正确的方向前进
【讨论】: