我试图根据单个行和列中的信息将某些单元格变为红色。我的算法应该做的是通过单列搜索并找到匹配的字符串并保存列号,然后对该行执行相同的操作。然后脚本选择单元格并将其变成红色。根据表头和单列更改单元格值
我搜索的所有密钥都来自我在网上找到并修改的一段代码,以适合我的需求。它完美的作品。问题是我无法让搜索正常工作。
Option Explicit
Sub Blahbot()
Dim xRow As Long
Dim x As Long, y As Long
Dim xDirect$, xFname$, InitialFoldr$, xFF$
InitialFoldr$ = "G:\" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7) '<<< Where the search terms come from
Do While xFname$ <> ""
y = Application.WorksheetFunction.Match(Mid(xFname$, 11, 4), Range("D2:KD2"), 0) '<<< Find a matching string in table header
x = Application.WorksheetFunction.Match(Mid(xFname$, 16, 4), Range("B3:B141"), 0) '<<< Find matching string in column B
Cells(x, y).Select '<<<Select the cell and turn it red
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
xFname$ = Dir
Loop
End If
End With
End Sub
代码的功能是它读取文件夹,获取文件名并分割它们。该名称始终为@@@@ _ ####(其中@ =大写字母,####是24小时格式的时间)。
Mid函数将该名称分成4个字母和时间。
如果你明白我想要做什么,你能建议一个更好的搜索算法,或看看我的代码做错了什么?
只是想重申你的问题,以确保它是明确的。您正在寻找特定范围内的所有单元格,这些单元格具有与用户在开始此过程时选择的所有文件名中的4个字符的字母代码和4个数字时间代码相匹配的特定字符集? – psubsee2003 2012-07-30 13:32:18
如果超过1个单元格匹配,该怎么办?你只想要第一个吗?或者你想匹配所有? – psubsee2003 2012-07-30 13:32:51
并且您电子表格中的一些示例数据也可能会有所帮助。 – psubsee2003 2012-07-30 13:33:48