作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个工作簿上创建了一个按钮,可以打开另一个无宏工作簿。 transposeDataMatrices
是将在打开的工作簿中的工作表上运行的子程序:
Private Sub CommandButton21_Click()
Set BEEBook = ThisWorkbook
FileSelect = Application.GetOpenFilename(FileFilter:="Excel Files (*.xl*), *.xl*", Title:="Please select the report to import")
If FileSelect = "False" Then Exit Sub
Set ReportBook = Workbooks.Open(FileSelect)
transposeDataMatrices
End Sub
transposeDataMatrices
内是以下严重截断的代码(
ReportBook
是
trasposeDataMatrices
中正在处理的工作簿的全局变量;
cArray
是全局数组):
Public Sub transposeDataMatrices()
ReportBook.Activate
rowCounter = finWkst.UsedRange.Rows.Count
For ii = 1 To wsCount
Worksheets(ii).Activate
pullModelData (ii) ' just pulls some data, off ii worksheet in ReportBook
ReDim indexIDArray(0 To 504) As Integer
Dim j as Integer: j = 0
For Each catName In cArray
Dim totRange As Range
Set catTitle = Worksheets(ii).UsedRange.Find(catName)
If Not catTitle Is Nothing Then
catTitle.Offset(2, 0).Offset(0, 1).Select
ReportBook.Worksheets(ii).Range(Selection, Selection.End(xlToRight).Offset(0, -1)).Select
ReportBook.Worksheets(ii).Range(Selection, Selection.End(xlDown)).Select
Set totRange = Selection
Else
Set totRange = Cells("A1")
EndIf
indexIDArray(j) = j
Next
equipModelVersion rowCounter, indexIDArray
rowCounter = finWkst.UsedRange.Rows.Count
Next ii
End Sub
Sub equipModelVersion(rowCounterDummy As Integer, indexArrayDummy() As String)
ReportBook.Activate
finWkst.Activate
iCol = 1:
Set indexRange = Range(Cells(rowCounterDummy + 1, iCol), Cells(rowCounterDummy + 1 + UBound(valueArrayDummy, 1), iCol))
Dim j As Integer: j = 0
For I = rowCounterDummy + 1 To rowCounterDummy + 1 + UBound(valueArrayDummy, 1)
Cells(I, iCol) = indexArrayDummy(j)
j = j + 1
Next
End Sub
Sub initializeWorkspace()
ReportBook.Activate
finWkst.Activate
Range("A1").Value = "IndexID"
Range("B1").Value = "ModelID"
Range("C1").Value = "UserVersion"
Range("D1").Value = "Equipment"
Range("E1").Value = "Date"
For ii = LBound(cArray) To UBound(cArray)
Cells(1, ii + 5).Value = cArray(ii)
Next
End Sub
equipModelVersion
运行时,它将值存储在按钮所在的工作表上的工作簿单元格中,而不是在为存储值而创建的工作表中,而不是打开的工作簿中。
transposeDataMatrices
并单独使用,以下行有效:
Set totRange = Range(Range(Selection, Selection.End(xlToRight).Offset(0, -1)), Selection.End(xlDown))
ReportBook.Worksheets(ii).Range(Selection, Selection.End(xlToRight).Offset(0, -1)).Select
ReportBook.Worksheets(ii).Range(Selection, Selection.End(xlDown)).Select
Set totRange = Selection
最佳答案
VBA 并不聪明,它只会做你告诉它做的事情,而不是你认为你告诉它做的事情。
我的猜测是ReportBook.Worksheets(ii)
正在失去焦点。
使用对象时始终声明父对象是一种很好的做法。简单的方法是将父对象声明为变量。
在这种情况下:
Dim ws as worksheet
set ws = ReportBook.Worksheets(ii)
with
声明
with ws
Set totRange =.Range(Selection, Selection.End(xlDown))
end with
.select
命令。见
here有关这方面的重要信息。使用它只会弄脏代码并使查找错误变得更加困难。
关于vba - 在一个工作簿中创建适用于另一个工作簿的子例程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33044508/
我是一名优秀的程序员,十分优秀!