VBA将其他电子表格导入其中

时间:2016-07-07 18:52:30

标签: vba excel-vba excel

您好,感谢您阅读我的问题。我试图将大量文​​件导入一个工作簿。

这部分脚本在第一个工作簿中运行一次,但在第二个工作簿中遇到第三个工作簿时崩溃。

Do While Filename <> ""
    Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
    Set tmpWb = ActiveWorkbook

    For Each Sheet In tmpWb.Sheets


        Range("A2").Select
        If Range("A2").Value <> "" Then
            sFileName = tmpWb.Name
            sFileName = Replace(sFileName, ".xlsx", "")
            Sheet.Name = sFileName
            wbNew.Activate
            Sheet.Copy After:=wbNew.Sheets(1)
        Else
            'do nothing
        End If
        tmpWb.Activate

        On Error GoTo LastSheet
            Worksheets(ActiveSheet.Index + 1).Select
 LastSheet:

    Next Sheet

    Workbooks(Filename).Close
    Filename = dir()

Loop

它在工作表崩溃(ActiveSheet.Index + 1)。选择 有这个错误 运行时错误9下标超出范围

1 个答案:

答案 0 :(得分:0)

完成,花了一些时间来搞清楚,但现在这个功能很好,可能还有另外一个bug,但它可以用来将文件和表格从文件导入到一个工作簿中

# Sample size
N <- 10000

# Creating the df
var1 <- sample(c(0,1),N,replace = T)
var2 <- sample(c(0),N,replace = T)
var3 <- as.factor(sample(c("2", "9", "5", "5", "5"),N,replace = T))
var4 <- as.factor(sample(c("EA", "EA", "EA", "EA", "JP"),N,replace = T))
dtf <- data.frame(var1, var2, var3, var4)

# Loading the package
require(rpart)
require(caret)

# Hot Enconding - Creating dummy variables
dummies <- dummyVars(~ ., data = dtf)
dtf2 <- as.data.frame(predict(dummies, newdata = dtf))

# Fitting the model
fit <- rpart(var1 ~. - var4.EA,data=dtf2, method="class", minbucket=25)