改进代码并检查错误

时间:2013-11-22 04:18:52

标签: vba excel-vba search excel-2010 excel

这是我正在使用的代码,对于每次新搜索,它都会创建一个新工作表,新工作表将以搜索到的单词命名,但是如何改进我的代码?只是关于此代码的一些信息如果用户在单词之前和之后添加星号,它将首先进行通配符搜索,将所有内容传递给sheet2,但我已经更改了代码以在搜索到的单词上创建新的搜索库,但我也遇到了新的错误。我该如何检查错误?

  • 如果用户在文本框中留空,程序将停止 宏而不是创建一个空表。

  • 如果用户在搜索文本框中添加星号,则程序将为 为通配符搜索创建一个新工作表

       strSearch = Application.InputBox("Please enter the search string")
    
       Worksheets.Add().Name = strSearch
       Set rg = Sheets("Sheet1").Cells(1).CurrentRegion                                       
       For a = 1 To rg.Rows.Count                                                                 
       Set rgF = rg.Rows(a).Find(strSearch, , xlValues, xlWhole)
    
      If Not rgF Is Nothing Then
      rg.Rows(a).Copy Sheets(strSearch).Range("A60000").End(xlUp).Offset(1, 0)
    
      Set rgF = Nothing
      End If
      Next a
    
      Application.ScreenUpdating = True
    

1 个答案:

答案 0 :(得分:0)

对于一些无法放入工作表名称的禁用字符,您需要执行以下操作:

strSearch = Application.InputBox("Please enter the search string")

if len(strSearch) > 0 then
    'add new sheet where in name we use the followings symbols
    '(X) instead of * asterisk
    '(Q) instead of question marks
    'and other are possible...
    Worksheets.Add().Name = replace(replace(strSearch, "*", "(X)"), "?", (Q))
else
    'if there isn't any string provided by user:
    'do nothing
end if
Set rg = Sheets("Sheet1").Cells(1).CurrentRegion       
'...the rest of your code

如果用户搜索*Blah?,您的工作表名称为(X)Blah(Q)

相关问题