簡體   English   中英

Excel VBA使用循環

[英]Excel VBA using loop

Private Sub cmdSubmit_Click()


Dim EmptyRow As Long
Dim MyRange As Range
Dim i As Integer
Dim newDate As Date


 Set MyRange = Worksheets("Submissions").Range("A:A")
 EmptyRow = Application.WorksheetFunction.CountA(MyRange) + 1

 Set MyRange2 = Worksheets("DateLoop").Range("A:A")
 EmptyRow2 = Application.WorksheetFunction.CountA(MyRange2) + 1

 Sheets("Submissions").Cells(EmptyRow, 1).Value = txtFirstName.Value
 Sheets("Submissions").Cells(EmptyRow, 2).Value = txtLastName.Value
 Sheets("Submissions").Cells(EmptyRow, 3).Value = txtNumGuests.Value
 Sheets("Submissions").Cells(EmptyRow, 4).Value = txtPhone.Value
 Sheets("Submissions").Cells(EmptyRow, 5).Value = txtEmail.Value
 Sheets("Submissions").Cells(EmptyRow, 6).Value = tbstdate.Value
 Sheets("Submissions").Cells(EmptyRow, 7).Value = tbenddate.Value
 Sheets("Submissions").Cells(EmptyRow, 8).Value = txtNumDays.Value
 Sheets("Submissions").Cells(EmptyRow, 9).Value = txtPriceNight.Value
 Sheets("Submissions").Cells(EmptyRow, 10).Value = txtEnvFee.Value
 Sheets("Submissions").Cells(EmptyRow, 11).Value = txtEntFee.Value
 Sheets("Submissions").Cells(EmptyRow, 12).Value = txtTotal.Value

Unload Me

For i = 0 To txtNumDays.Value

conDate = CDate(tbstdate.Value)
Sheets("DateLoop").Cells(EmptyRow2, 1).Value = conDate + i
Sheets("DateLoop").Cells(EmptyRow2, 2).Value = txtNumGuests.Value

Next i


End Sub

我想做的是使用底部的循環顯示所有將成為循環一部分的日期。

示例:從12/17 / 14-12 / 22/14預訂4位客人,則僅將12位客人和12/22/14的客人插入工作表。 我需要它能夠在新行中列出每個日期(所有6個日期),即使客人每次都保持不變。 目的是跟蹤一年中每天安排多少人,這就是為什么我需要每天列出

感謝您提供任何幫助。

如果需要每個日期,則需要在FOR循環中更新EmptyRow2的值。 當前,它每天都會對其進行更新,但隨后會覆蓋它,並以conDate + txtNumDays.Value值結束。 在“下一個i”行之前添加:

EmptyRow2 = EmptyRow2 + 1

您的循環將用每個循環替換Sheets("DateLoop").Cells(EmptyRow2, 1)Sheets("DateLoop").Cells(EmptyRow2, 2) 如果您希望每個日期都有一個新行,請將循環更改為以下內容:

For i = 0 To txtNumDays.Value

conDate = CDate(tbstdate.Value)
Sheets("DateLoop").Cells(EmptyRow2 + i, 1).Value = conDate + i
Sheets("DateLoop").Cells(EmptyRow2 + i, 2).Value = txtNumGuests.Value

Next i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM