期待通过多个工作表循环Excel VBA宏?

问题描述 投票:-1回答:4

希望通过(约)125个工作表在Excel工作簿中循环以下代码,并将列出的单元格值拉到“数据库”工作表上的一个数据库条目日志中。现在它只是从其中一个标签中拉出来的。 (PO VT-0189)。想知道如何纠正。

Private Sub PopulateOrderInfo()
    Dim OrderDate As String, PONumber As String, Vendor As String, ShipTo As String, SKU As String
    Dim R As Long, LastSKURow As Long, NextDBRow As Long, OFrm As Worksheet, DB As Worksheet
    For Each OFrm In ActiveWorkbook.Worksheets
        Set OFrm = Worksheets("PO VT-0189")
        Set DB = Worksheets("Database")
        OrderDate = OFrm.Range("N4")
        PONumber = OFrm.Range("N3")
        Vendor = OFrm.Range("A13")
        ShipTo = OFrm.Range("I13")
        POTotal = OFrm.Range("P43")
        LastSKURow = OFrm.Range("A38").End(xlUp).Row
        For R = 21 To LastSKURow
            SKU = OFrm.Range("A" & R).Value
            SKUDesc = OFrm.Range("D" & R).Value
            SKUQty = OFrm.Range("K" & R).Value
            Lntotal = OFrm.Range("M" & R).Value
            NextDBRow = DB.Cells(DB.Rows.Count, "A").End(xlUp).Row + 1
            DB.Range("A" & NextDBRow).Value = OrderDate
            DB.Range("B" & NextDBRow).Value = PONumber
            DB.Range("C" & NextDBRow).Value = Vendor
            DB.Range("D" & NextDBRow).Value = ShipTo
            DB.Range("E" & NextDBRow).Value = SKU
            DB.Range("F" & NextDBRow).Value = SKUDesc
            DB.Range("G" & NextDBRow).Value = SKUQty
            DB.Range("H" & NextDBRow).Value = Lntotal
            DB.Range("I" & NextDBRow).Value = POTotal
        Next R
    Next OFrm
End Sub
vba excel-vba excel
4个回答
1
投票

我认为你也可以通过避免循环来缩短你的代码,而且大多数变量对我来说似乎都是不必要的。

Private Sub PopulateOrderInfo()

Dim R As Long, LastSKURow As Long, NextDBRow As Long, OFrm As Worksheet, DB As Worksheet

Set DB = Worksheets("Database")

For Each OFrm In ActiveWorkbook.Worksheets
    If OFrm.Name <> DB.Name Then
        LastSKURow = OFrm.Range("A38").End(xlUp).Row
        R = LastSKURow - 21 + 1
        NextDBRow = DB.Cells(DB.Rows.Count, "A").End(xlUp).Row + 1
        DB.Range("A" & NextDBRow).Resize(R).Value = OFrm.Range("N4")
        DB.Range("B" & NextDBRow).Resize(R).Value = OFrm.Range("N3")
        DB.Range("C" & NextDBRow).Resize(R).Value = OFrm.Range("A13")
        DB.Range("D" & NextDBRow).Resize(R).Value = OFrm.Range("I13")
        DB.Range("E" & NextDBRow).Resize(R).Value = OFrm.Range("A21").Resize(R).Value
        DB.Range("F" & NextDBRow).Resize(R).Value = OFrm.Range("D21").Resize(R).Value
        DB.Range("G" & NextDBRow).Resize(R).Value = OFrm.Range("K21").Resize(R).Value
        DB.Range("H" & NextDBRow).Resize(R).Value = OFrm.Range("M21").Resize(R).Value
        DB.Range("I" & NextDBRow).Resize(R).Value = OFrm.Range("P43")
    End If
Next OFrm

End Sub

0
投票

使用for循环和WorkSheets集合,如:

For I = 1 to worksheets.count
 if worksheets(i).name <> "Database" then 
  Add your code here
 end if
Next i

这将循环遍历工作簿中的每个工作表,并执行除数据库之外的所有工作表所需的操作。


0
投票

使用for each...循环

For Each ws In wb.Worksheets 
     If ws.name = "Database" Then   
'Leave blank to just skip database. Code here if you want something special on database. OR statements can be used to exclude additional sheets
 Else 
'Code here  
 End If  
    Next 

0
投票

我认为你很好地描述了这个问题。只是为了确认,你想在一个工作簿中循环遍历所有工作表,对吧。试试下面的脚本。如果您有其他问题,疑虑等反馈,谢谢。

Sub ImportAll()

Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
Dim lngCount As Long
Dim objExcel As Object, objWorkbook As Object
Dim colWorksheets As Collection
Dim strPathFile as String, strTable as String
Dim strPassword As String

' Establish an EXCEL application object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
      Set objExcel = CreateObject("Excel.Application")
      blnEXCEL = True
End If
Err.Clear
On Error GoTo 0

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False

' Replace C:\Filename.xls with the actual path and filename
strPathFile = "C:\Filename.xls"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"

' Replace passwordtext with the real password;
' if there is no password, replace it with vbNullString constant
' (e.g., strPassword = vbNullString)
strPassword = "passwordtext"

blnReadOnly = True ' open EXCEL file in read-only mode

' Open the EXCEL file and read the worksheet names into a collection
Set colWorksheets = New Collection
Set objWorkbook = objExcel.Workbooks.Open(strPathFile, , blnReadOnly, , _
      strPassword)
For lngCount = 1 To objWorkbook.Worksheets.Count
      colWorksheets.Add objWorkbook.Worksheets(lngCount).Name
Next lngCount

' Close the EXCEL file without saving the file, and clean up the EXCEL objects
objWorkbook.Close False
Set objWorkbook = Nothing
If blnEXCEL = True Then objExcel.Quit
Set objExcel = Nothing

' Import the data from each worksheet into the table
For lngCount = colWorksheets.Count To 1 Step -1
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
            strTable, strPathFile, blnHasFieldNames, colWorksheets(lngCount) & "$"
Next lngCount

' Delete the collection
Set colWorksheets = Nothing

' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.