如何通过列表框循环并执行重复的任务

时间:2016-01-18 01:13:42

标签: excel vba loops

我的问题是尝试在我的用户表单上的列表框中为每个条目重复我的代码。下面的代码将为一个条目执行此操作,但我需要为列表框中的每个项目执行此操作。我的ListBox看起来像这样。

'ListBox
PBB
ABH
GKB

此代码打开模板工作簿并根据列表框中的名称进行保存。然后它将数据输入到新工作簿。     选项明确     Dim DIV As String     Dim XL作为Excel.Application     Dim WBK As Excel.Workbook     昏暗的ActSheet作为工作表     昏暗的ActBook作为工作簿     Dim CurrentFile As Workbook     Dim NewFileType As String     Dim NewFile As String     Dim QTR_NUM As String     Dim MFG As String     Dim JOB As String     Dim visitdate As Variant     Dim visitdate_text As String     Dim Quote_Request As Workheet     昏暗的QTR作为工作簿     昏暗的表格作为工作簿     昏暗的DCSProgram作为工作簿     Dim ILast As Long     Dim j As Integer

Application.ScreenUpdating = False    ' Prevents screen refreshing.

Set DCSProgram = ActiveWorkbook


Set QTR = Workbooks.Open("C:\Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\1. QUOTES\3. PROGRAM FILES\QTR.xlsx")

ActiveWindow.WindowState = xlMinimized


For j = 0 To QTRList.ListCount - 1

MFG = QTRList.List(j)

visitdate = Date
visitdate_text = Format$(visitdate, "mm\-dd\-yyyy")

QTR.SaveAs FileName:="C:\QTR" & "\DCS QTR " _
& " " & MFG & " " & JobName & " " & visitdate_text & " .xlsm", _
FileFormat:=52, CreateBackup:=False, local:=True

Application.ScreenUpdating = True

Set FORM = Workbooks.Open("C:\QTR" & "\DCS QTR " _
& " " & MFG & " " & JobName & " " & visitdate_text & " .xlsm")


With FORM.Sheets(1).Range("H9")
.Value = Date
.NumberFormat = "mm/dd/yy"
End With

FORM.Sheets(1).Range("H13").Value = JobName                   'JOB NAME

With DCSProgram.Sheets("MFG_DATA")
    ILast = .Cells(.Rows.Count, 1).End(xlUp).Row
        For i = 1 To ILast
        If .Cells(i, 1).Value = MFG Then
            FORM.Sheets(1).Range("B7").Value = .Cells(i, 2).Value
            FORM.Sheets(1).Range("B8").Value = .Cells(i, 3).Value
            FORM.Sheets(1).Range("B9").Value = .Cells(i, 4).Value
            FORM.Sheets(1).Range("B12").Value = .Cells(i, 5).Value
            FORM.Sheets(1).Range("B13").Value = .Cells(i, 6).Value
            FORM.Sheets(1).Range("B14").Value = .Cells(i, 7).Value
            FORM.Sheets(1).Range("B15").Value = .Cells(i, 8).Value

   End If: Next: End With
   Next j

   End Sub

我尝试使用上面的代码,然后重复列表框,ABH和GKB中的下一个项目。如果有人能指出我正确的方向,我将不胜感激。再次感谢,杰夫

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容循环显示列表框项:

For i = 0 To QTRList.ListCount - 1
   MsgBox (QTRList.List(i))
Next