我在一家银行工作,我们经常收到Nasdaq Smart关于市场滥用的季度数据。我被分配了创建宏来汇总这些数据的任务,以便能够在更长的时间线内可视化市场滥用。
到目前为止,我能够手动选择我感兴趣的工作簿,然后自动选择要导入的工作簿部分。
我遇到的问题是它不会将所选数据输入到新文件中。如果我可以信任编译器,那么问题是在这种情况下不支持范围函数。
Private Sub CommandButton23_Click()
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim rngSourceRange As Range
Dim rngDestination As Range
Dim bottomCell As Range
Dim rngTemp As Range
Set wkbCrntWorkBook = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel 97-2003", "*.xlsx; *.xlsm; *.xlsa , *.xls"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
Workbooks.Open .SelectedItems(1)
Set wkbSourceBook = ActiveWorkbook
Set bottomCell = wkbSourceBook.Sheets("par_ACCOUNT").Cells.Find(what:="Account by Type")
Set rngTemp = wkbSourceBook.Sheets("par_ACCOUNT").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If Not rngTemp Is Nothing Then
wkbSourceBook.Sheets("par_ACCOUNT").Range(bottomCell, rngTemp.Offset(0, 65)).Select
End If
wkbCrntWorkBook.Activate
Set rngDestination = wkbCrntWorkBook.Range("A1")
Selection.Copy rngDestination
rngDestination.CurrentRegion.EntireColumn.AutoFit
wkbSourceBook.Close False
End If
End With
End Sub
提前感谢大家的帮助。这是我第一次使用VBA和Stackoverflow,所以欢迎任何形式的帮助。
答案 0 :(得分:1)
我认为问题可能是由于在激活不同的工作簿之前选择了一个范围,导致选择失败。
If Not rngTemp Is Nothing Then
wkbSourceBook.Sheets("par_ACCOUNT").Range(bottomCell, rngTemp.Offset(0, 65)).Select
End If
wkbCrntWorkBook.Activate
Set rngDestination = wkbCrntWorkBook.Range("A1")
Selection.Copy rngDestination
我会完全避免使用select
,这可以使用一个简单的范围对象(已经声明)来实现。
If Not rngTemp Is Nothing Then
set rngSourceRange = wkbSourceBook.Sheets("par_ACCOUNT").Range(bottomCell, rngTemp.Offset(0, 65))
End If
Set rngDestination = wkbCrntWorkBook.sheets(???).Range("A1")
rngSource.Copy rngDestination
答案 1 :(得分:0)
以下是处理类似项目的任何人的工作代码。
Private Sub CommandButton23_Click()
func testViewControllerShouldHaveNavigationController() {
let viewController = UIViewController()
let navigation = UINavigationController()
navigation.viewControllers = [viewController]
XCTAssertNotNil(viewController.navigationController)
}
End Sub