Excel VBA声明变量请提供帮助
我有一个主电子表格,我想用团队成员提供的各种数据集填充它。 我以前做过这件事,效果很好,但我也意识到,在某个时候,我会遇到臭名昭著的运行时错误(-2147319767(80028029)) 过去一位同事告诉我,避免这种情况的方法是将文件和工作表声明为变量DIM、SET等 我从来都不能完全理解这些,所以有人能帮我写代码吗 代码只是打开文件、取消隐藏工作表、复制数据并将其粘贴到主报告中:Excel VBA声明变量请提供帮助,excel,vba,variables,Excel,Vba,Variables,我有一个主电子表格,我想用团队成员提供的各种数据集填充它。 我以前做过这件事,效果很好,但我也意识到,在某个时候,我会遇到臭名昭著的运行时错误(-2147319767(80028029)) 过去一位同事告诉我,避免这种情况的方法是将文件和工作表声明为变量DIM、SET等 我从来都不能完全理解这些,所以有人能帮我写代码吗 代码只是打开文件、取消隐藏工作表、复制数据并将其粘贴到主报告中: Workbooks.Open Filename:="S:\International\Inte
Workbooks.Open Filename:="S:\International\Interview Scoresheet (Justin).xlsm""
Sheets("Scores").Visible = True
Sheets("Scores").Select
Windows("Interview Scoresheet (Justin).xlsm").Activate
Sheets("Scores").Select
Range("A2:Z" & Range("A" & Rows.Count).End(xlUp).Row).Select
Selection.Copy
Windows("Interview Checks.xlsm").Activate
Sheets("Scores").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Interview Scoresheet (Justin).xlsm").Activate
ActiveWindow.Close savechanges:=False
Workbooks.Open Filename:="S:\International\Interview Scoresheet (Soyuncu).xlsm""
Sheets("Scores").Visible = True
Sheets("Scores").Select
Windows("Interview Scoresheet (Soyuncu).xlsm").Activate
Sheets("Scores").Select
Range("A2:Z" & Range("A" & Rows.Count).End(xlUp).Row).Select
Selection.Copy
Windows("Interview Checks.xlsm").Activate
Sheets("Scores").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Interview Scoresheet (Soyuncu).xlsm").Activate
ActiveWindow.Close savechanges:=False
大多数人将开始使用vba,就像他们在Excel中使用的那样。虽然这是可能的,但代码的性能和稳定性将非常糟糕。因此,首先要学习的是使用“数组”,允许vba在再次与工作表交互之前处理内存中的所有魔法 <>你会发现很多关于“VBA数组”的很好的教程,但是作为一个KICKESTORT考虑如下:
Sub Arrays()
Dim arr
arr = Sheet1.Range("A1").CurrentRegion.Value2 'get all data in memory
Dim j As Long, i As Long 'initiate our counter vars
Dim arr2: ReDim arr2(1 To UBound(arr), 1 To UBound(arr, 2)) '=> setup new array to modify source data
For j = 1 To UBound(arr) 'traverse rows
For i = 1 To UBound(arr, 2) 'traverse columns
'here we can access each cell by referencing our array(<rowCounter>, <columnCounter>
'e.g. arr(j,i) => if j = 1 and i = 1 we'll have the values of Cell A1
'we can dump these values anywhere in the activesheet, other sheet, other workbook, .. but to limit the number of interactions with our sheet object we can also use our intermediant arrays
Next i
Next j
'when we are ready with our data we dumb to the sheet
With Sheet1 'the with allows us the re-use the sheet name without typing it again
.Range(.Cells(1, 1), .Cells(UBound(arr2), UBound(arr2, 2))).Value2 = arr2 'the ubound function allows us to size the "range" to the same size as our array, once that's done we can just dumb it to the sheet
End With
End Sub
最后但并非最不重要的一点是,如果您想学习:
在代码顶部(First sub上方)添加“option explicit”,这将迫使您声明(隐藏)所有变量,这将使您更好地了解正在发生的情况,并避免意外/不明确的错误。如果您没有声明特定的数据类型,那么默认值是“variant”,这就是为什么您会注意到我在示例中没有为数组添加这个值
使用“F8”一步一步地浏览代码,右键单击VAR、阵列上的“添加监视”以查看发生了什么。。
Sub multiWorkbook()
'dim vars to specific datatype
Dim wb As Workbook, wb2 As Workbook, sh As Worksheet, arr
Set wb = ThisWorkbook
Set sh = wb.Sheets("sheet 1")
Dim wb2 As Workbook, sh2, FilesInPath As String
MyPath = ThisWorkbook.Path & "\SubFolder\"
FilesInPath = "test.xlsx"
Set wb2 = Workbooks.Open(MyPath & FilesInPath)
Set sh2 = wb2.Sheets("sheet 1")
End Sub