【问题标题】:If statement from excel scores to change text in word with VBA如果来自 excel 的语句得分以使用 VBA 更改 word 中的文本
【发布时间】:2015-03-04 04:51:14
【问题描述】:

我在直接从具有某些值的数据库中提取的 Excel 表中有不同的分数。我想从 excel 中提取这些分数,并能够使用 if 语句更改 word 文档中的文本(例如,如果单元格中的分数(2、2)> 60(在 excel 中)=删除/替换某个文本(在 word 中))用VBA。有谁知道我将如何编译这个宏?

我试过的代码:

Private Sub CommandButton8_Click()

Dim objExcel As New Excel.Application

Dim exWb As Excel.Workbook

Set exWb = objExcel.Workbooks.Open("C:\Desktop\Testdoc.xlsx")

If score.Cells(2, 2) >= 60 Then

Function FnOpeneWordDoc()

   Dim objWord

   Dim objDoc

   Set objWord = CreateObject("Word.Application")

   Set objDoc = objWord.Documents.Open("C:\Desktop\TestDoc.docm")

   objWord.Visible = True

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting

    With Selection.Find
    .Text = "text here"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

End Function

End If

exWb.Close

Set exWb = Nothing

End Function

编辑:新代码:

Private Sub CommandButton7_Click()

Dim objExcel As New Excel.Application

Dim exWb As Excel.Workbook

Set exWb = objExcel.Workbooks.Open("C:\UsersDesktop\Testdoc.xlsx")

If exWb.Sheets(1).Cells(2, 2).Value >= 60 Then Run [BLA]

If Not exWb.Sheets(1).Cells(2, 2).Value >= 60 Then Run [BLA2]

End If

exWb.Close

Set exWb = Nothing

End Sub

Function BLA()
With Selection.Find
.ClearFormatting
.Text = "Old text"
.Replacement.ClearFormatting
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll, Forward:=True, _
    Wrap:=wdFindContinue

End With

End Function

Function BLA2()
With Selection.Find
.ClearFormatting
.Text = "Old text"
.Replacement.ClearFormatting
.Replacement.Text = "Old text"
.Execute Replace:=wdReplaceAll, Forward:=True, _
    Wrap:=wdFindContinue

End With

End Function

【问题讨论】:

  • 向我们展示您的一些代码!你试过什么?
  • 学习VBA的最佳方法是什么?
  • 我希望这能让它更清楚一点...我不知道该怎么做。
  • 请解释什么不工作,什么和在哪里?任何错误(编号和描述)?
  • 另外,使用“F8”或其他调试选项(如断点)逐行运行代码应该可以帮助您了解正在发生的事情以及错误的确切位置。

标签: vba excel ms-word


【解决方案1】:

您的问题很难理解,因为您的代码没有意义。首先,您在 sub 中定义了一个函数。那永远不会编译。您需要将函数或子函数分开。

然后您似乎以End Function 结束子 - 也会导致错误。顺便说一句,函数返回值,如果您不想返回某些内容,请使用 Subs。

最后,您还没有提到您将如何以及在何处运行代码。 FnOpeneWordDoc 首先打开 Word 打开 Word 文档,CommandButton8_Click 首先打开 Excel 打开 Excel xlsx。要么你从 Excel 运行它,然后你只需要打开 Word,因为 Excel 已经打开,或者相反。从 Excel 中尝试以下操作。我不知道你想在 Word 中做什么,所以我注释掉了无法编译的代码。我希望它能让你朝着正确的方向前进。

Option Explicit

Private Sub CommandButton8_Click()
    Dim exWb As Workbook

    Set exWb = Workbooks.Open("C:\Desktop\Testdoc.xlsx")

    If exWb.Sheets(1).Cells(2, 2).Value >= 60 Then
        Call FnOpeneWordDoc
    End If

    exWb.Close

    Set exWb = Nothing
End Sub


Sub FnOpeneWordDoc()
    Dim objWord

    Dim objDoc

    Set objWord = CreateObject("Word.Application")

    Set objDoc = objWord.Documents.Open("C:\Desktop\TestDoc.docm")

    objWord.Visible = True


    'Selection.Find.ClearFormatting

    'Selection.Find.Replacement.ClearFormatting

    'With Selection.Find
    '    .Text = "text here"
    '    .Replacement.Text = ""
    '    .Forward = True
    '    .Wrap = wdFindContinue
    '    .Format = False
    '    .MatchCase = False
    '    .MatchWholeWord = False
    '    .MatchWildcards = False
    '    .MatchSoundsLike = False
    '    .MatchAllWordForms = False
    'End With

    Set objDoc = Nothing
    Set objWord = Nothing

End Sub

编辑:回复评论 - 当然,这就是If exWb.Sheets(1).Cells(2, 2).Value >= 60 Then 行的作用。如果 B2 中的值等于或大于 60,则运行FnOpeneWordDoc

额外提示:您可以在 VBA 环境中使用即时窗口 (CTRL+G) 来评估条件,方法是输入问号 ?,然后条件:

?exWb.Sheets(1).Cells(2, 2).Value >= 60

将返回 TrueFalse

编辑#2: 您有两个 If 语句,其中一个更好。此外,第二个 If 语句不正确。你要么有If True Then [Command],要么在下面一行的[Command]后面加上End If - 你把两者混合起来。

尝试以下基本代码,稍后再添加详细信息 - 如果可能,请删除其他所有内容。

Option Explicit

Private Sub CommandButton8_Click()
    '***** Check to see if something
    '***** is bigger than something else
    If 61 >= 60 Then
        '***** Run sub FnOpeneWordDoc
        Call FnOpeneWordDoc
    Else
        '***** Do nothing
    End If
End Sub


Sub FnOpeneWordDoc()
    '***** Do something cool
End Sub

编辑#3: 如果您使用正确的语法,您的 If 语句实际上可以工作:

'***** Use either one line without "End If"...
If exWb.Sheets(1).Cells(2, 2).Value >= 60 Then Run [BLA]

'***** ...or this syntax, where you must use "End If"
If Not exWb.Sheets(1).Cells(2, 2).Value >= 60 Then
    Run [BLA2]
    '***** You can add more lines here if you want
End If

【讨论】:

  • 感谢您的评论。我理解你的困惑,对此我很抱歉。我意识到 sub 内的功能不起作用,我试图展示我想要做的事情(显然失败了)。我从 Word VBA 运行代码,所以你是对的,我不必再打开文档了..对不起这个错误。我会再试一次。
  • @user3219697 没问题!
  • 好吧,我现在感觉自己像个彻头彻尾的白痴。是否可以运行条件函数?我的意思是:如果值 > 60 则运行函数 X,否则不运行函数 X。
  • 我尝试了多种方式,但没有出现错误,“if”仍然不是我想要的。我似乎无法确定 VBA 执行哪个功能。我错过了什么吗? (新代码见编辑问题)
  • 试试我添加的非常简单的代码示例并尝试让它运行。当 If Else 会更好时,您的新代码中有两个 If 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
相关资源
最近更新 更多