【问题标题】:Excel replace function in access VBAAccess VBA中的Excel替换功能
【发布时间】:2015-11-26 14:14:25
【问题描述】:

我正在尝试在 access VBA 中运行一些 Excel VBA 代码。它几乎可以工作,但我的替换功能有问题。

Sub test2()
    Dim lngColumn As Long
    Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim blnEXCEL As Boolean

    blnEXCEL = False

    ' Establish an EXCEL application object
    On Error Resume Next
    Set xlx = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
        Set xlx = CreateObject("Excel.Application")
        blnEXCEL = True
    End If
    Err.Clear
    On Error GoTo 0
    xlx.Visible = True
            
    ' Replace C:\Filename.xls with the actual path and filename
    ' of the EXCEL file from which you will read the data
    Set xlw = xlx.Workbooks.Open("Q:\21 Projekty\FlowControl\Flow.xlsx", , True) ' opens in read-only mode

    ' Replace WorksheetName with the actual name of the worksheet
    ' in the EXCEL file
    Set xls = xlw.Worksheets("Flow")
            
    ' Replace A1 with the cell reference from which the first data value
    ' (non-header information) is to be read
    Set xlc = xls.range("A1") ' this is the first cell that contains data
            
    With xls
        .Columns(2).Insert Shift:=xlToRight
        SelectedColumn = SelectedColumn + 1
        
        .Columns(3).Copy _
            Destination:=.Columns(1)
            
        .Columns(2).Replace What:=" - *", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False 
    'Error there  Subscript out of range (Error 9) 
        
        .Columns(3).Replace What:="* - ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        
        .Columns(2).AutoFit
        .Columns(3).AutoFit
    Errorhandling:
        'Application.ScreenUpdating = True
        'End Sub
    End With
End Sub

【问题讨论】:

  • 检查 xlToRight、xlByRows、xlPart 等内容 - Access 无法通过后期绑定理解它们。在 Excel 中,在“立即”窗口中输入 ?xlToRight,它将返回 -4161。使用它给你的数字。
  • 如果您的代码按原样工作 - 看起来您已设置对 Excel 的引用,但在代码中使用后期绑定 - 您将 xlx 定义为对象而不是 Excel.Application。
  • 非常感谢,这是一个真正的问题,所以我在这部分的代码现在看起来像:.columns(2).Replace What:="* - ", Replacement:="", LookAt: =2, _ SearchOrder:=1, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False

标签: excel vba ms-access


【解决方案1】:

如前所述,您需要处理 Excel VBA 常量,例如 xlToRight。

一种方法是用常量替换它们的值,另一种方法是在代码顶部将它们声明为常量。

Const xlToRight = -4161
Const xlPart = 2
Const xlByRows = 1

【讨论】:

    【解决方案2】:

    问题是 excel vba 和访问 vba 中的 替换 函数有点不同。现在我只使用:

    .columns(2).Replace What:=" - *", Replacement:=""

    问题解决了。

    编辑:
    应该是

    .columns(2).Replace What:="* - ", Replacement:="", LookAt:=2, _
            SearchOrder:=1, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      相关资源
      最近更新 更多