【问题标题】:Searching for keywords & Entering Text搜索关键字并输入文本
【发布时间】:2020-02-23 01:40:54
【问题描述】:

对于 Excel 的 VBA,我非常陌生。但是,我在 VBA 上阅读了尽可能多的内容,但仍然无法理解如何编写此代码。我遇到的问题之一是语法本身。

目前,我有一个电子表格,其中的信息不是恒定的,我希望能够在此电子表格中搜索特定单词,然后 Excel 将一个单元格向右移动并输入一些文本。如果特定单词不存在,那么我希望 VBA 什么都不做,继续搜索下一个单词。

所以,这就是我的想法,我只是不知道如何用正确的语法写出来:

Option Explicit
Sub SortAndLabel()

'Here is where I get lost as far as syntax goes so I will just type in what I'm wanting it to do...

Search the current worksheet for "12345 Total"

If "12345 Total" is not found then Do Nothing

Else

'Move 1 cell to the right
ActiveCell.Offset(0,1)

'Then enter in the following text (I don't know the proper syntax for this)...
Enter text "Electric"

End If

如果您能提供任何帮助,我将不胜感激。

【问题讨论】:

  • 开始使用宏记录器生成代码。

标签: excel vba subroutine


【解决方案1】:

试试下面的代码。

Sub FindSub()
Dim fng As Range
Dim fText As String

fText = "12345 Total"

    Set fng = Sheet1.Cells.Find(fText)

        If Not fng Is Nothing Then
            fng.Offset(0, 1) = "Electric"
        Else
            MsgBox "No match found."
        End If
Set fng = Nothing
End Sub

【讨论】:

  • 感谢 Harun24HR 的帮助。这解决了我的问题。我确实有一个问题......我怎么能有 VBA“什么都不做”,而不是显示一个消息框,说明“未找到匹配项”。我以前在网上读过的东西说我应该只写“什么都不做”,但这对我来说似乎总是出错。
  • 然后只使用 if 语句。删除其他部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多