【发布时间】:2021-11-06 18:57:07
【问题描述】:
我正在尝试从另一个应用程序在 excel 中运行此代码。代码运行没有问题,但是 rngNumber.Copy wsData.Range("A2") 没有被复制。我已经直接在excel中测试了相同的代码,它被完美地复制了。我认为当代码从另一个应用程序运行时,rngNumber 可能没有正确设置。但是,我不明白确切的原因。任何建议将不胜感激,谢谢。
Sub TEST()
' Try to connect to a running instance of Excel.
Dim excelApp As Excel.Application
On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err Then
Err.Clear
' Couldn't connect so start Excel. It's started invisibly.
Set excelApp = CreateObject("Excel.Application")
If Err Then
MsgBox "Cannot access excel."
Exit Sub
End If
End If
' You can make it visible if you want. This is especially
' helpful when debugging.
excelApp.Visible = True
'Open the excel file (through dialog)
Dim ExcelFilePath As Variant
ExcelFilePath = excelApp.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*")
If ExcelFilePath <> False Then
Set wb = excelApp.Workbooks.Open(ExcelFilePath)
End If
' Open the excel file
Dim wb as Workbook
Set wb = excelApp.ActiveWorkbook
Dim ws as Worksheet
Set ws = wb.Worksheets(1)
ws.Activate
'Set Worksheet
Dim wsData As WorkSheet
Set wsData = wb.Worksheets(2)
'Write column titles
With wsData
.Cells(1, "A").Value = "Number"
End With
'Get column letter for each column whose first row starts with an specific string
ws.Activate
Dim sNumber as String
sNumber= Find_Column("Number")
'Define variables
Dim rngNumber As Range
' Copy and paste data from "Number" column to Column "A" in Worksheets "Data"
ws.Activate
'Find which is the last row with data in "Number" column and set range
With ws.Columns(sNumber)
Set rngNumber = Range(.Cells(2), .Cells(.Rows.Count).End(xlUp))
End With
'Copy and paste data from "Number" column
rngNumber.Copy wsData.Range("A2")
End Sub
Private Function Find_Column(Name As String) As String
Dim rngName As Range
Dim Column As String
With ws.Rows(1)
On Error Resume Next
Set rngName = .Find(Name, .Cells(.Cells.Count), xlValues, xlWhole)
' Calculate Name Column Letter.
Find_Column = Split(rngName.Address, "$")(1)
End With
End Function
【问题讨论】:
-
@EEM 这是关于我问你的问题的完整问题。
-
试试
Dim rngNumber As excelApp.Range -
@CDP1802 我试过了,但出现编译错误“未定义用户定义的类型”。无论如何,发布的代码只是较长代码的第一部分。在其余代码中,我设置了其他范围,它们工作得很好。只是在列内定义范围时:``` With ws.Columns(sNumber)```
-
问题是
Range(.Cells(2), .Cells(.Rows.Count).End(xlUp))。这只是选择 1 个单元格,我认为您想要一个范围内的多个单元格,如果是这样,它就像.Range(.Cells(1,1), .Cells(1,1000))。您也错过了范围开头的.。 -
您的代码中存在一些问题... 1. 在不工作的代码中使用
On Error Resume Next,而不是On Error GoTo 0,这是一个坏主意。当出现错误时(在以下所有代码中),它不仅不会让 VBA 发出警告。 2 您在Sub级别将ws声明为Worksheet,并尝试在Find_Column函数中使用它。 VBA 不知道它是什么,如果完全无用的On Error Resume Next不应该存在,它会发出警告。 3. 您应该在模块顶部使用Option explicit并声明所有使用的变量。