Vba 将excel文件分配给脚本,以便运行下面的代码

Vba 将excel文件分配给脚本,以便运行下面的代码,vba,excel,Vba,Excel,我有一个类似于下面的场景 Sub SliceNDice() Dim objRegex As Object Dim X Dim Y Dim lngRow As Long Dim lngCnt As Long Dim tempArr() As String Dim strArr Set objRegex = CreateObject("vbscript.regexp") objRegex.Pattern = "^\s+(.+?)$" 'Define the range to b

我有一个类似于下面的场景

Sub SliceNDice() 
Dim objRegex As Object 
Dim X 
Dim Y 
Dim lngRow As Long 
Dim lngCnt As Long 
Dim tempArr() As String 
Dim strArr 

Set objRegex = CreateObject("vbscript.regexp") 
objRegex.Pattern = "^\s+(.+?)$" 
'Define the range to be analysed
X = Range([a1], Cells(Rows.Count, "b").End(xlUp)).Value2 
Redim Y(1 To 2, 1 To 1000) 
For lngRow = 1 To UBound(X, 1) 
'Split each string by ","
   tempArr = Split(X(lngRow, 2), ",") 
For Each strArr In tempArr 
      lngCnt = lngCnt + 1 
     'Add another 1000 records to resorted array every 1000 records
      If lngCnt Mod 1000 = 0 Then Redim Preserve Y(1 To 2, 1 To lngCnt + 1000) 
           Y(1, lngCnt) = X(lngRow, 1) 
           Y(2, lngCnt) = objRegex.Replace(strArr, "$1") 
     Next 
Next lngRow
'Dump the re-ordered range to columns C:D
[c1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y) 

End Sub 

但是为了实现上面的代码,我需要将其引用到excel文件中。我该怎么做?例如,我的文件存储在位置C:\SI。我如何引用它,以便使用上面的代码处理该文件。期待您的光临。

打开工作簿,将其存储为变量,然后使用该变量

Sub OpenWorkbookFileAndRunAgainstThat()
Dim wb as Workbook
Set wb = Workbooks.Open("C:\SI\workbook.xlsx")

With wb
    .Worksheets("yourSheet").Range("A1").value = "something" 'This is instead of Range in the sample above.
End with
End Sub

你能把你的文章正确地格式化吗?把它引用到excel文件是什么意思?如果将代码转储到Excel文件中的模块中,则在代码运行时,它将引用处于活动状态的Excel文件。范围[a1]、CellsRows.Count、b.EndxlUp始终引用活动工作簿中的活动工作表,因为您尚未指定特定的工作表/文件。