所以我通过试用,错误和搜索来学习VBA。现在我有这样的事情:
For i = 1 To NewEntries
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(tnd, example, 0) + 1) = ReportA.Cells(2, 3).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Date, example, 0) + 1) = ReportA.Cells(2, 5).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Code, example, 0) + 1) = ReportA.Cells(4 + i, 2).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Desc, example, 0) + 1) = ReportA.Cells(4 + i, 3).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Price, example, 0) + 1) = ReportA.Cells(4 + i, 4).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Sprice, example, 0) + 1) = ReportA.Cells(4 + i, 5).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Disc, example, 0) + 1) = ReportA.Cells(4 + i, 6).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Payment, example, 0) + 1) = ReportA.Cells(4 + i, 7).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Bank, example, 0) + 1) = ReportA.Cells(4 + i, 8).Value
Next
如何将FOR循环放在通过ReportZ循环ReportA的内容中,而不是为每个报告复制此代码块?
答案 0 :(得分:2)
将Chr
与ascii代码一起使用。
dim a as long
For i = 1 To NewEntries
for a = 65 to 90
with worksheets("report" & chr(a))
debug.print .Cells(2, 3).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(tnd, example, 0) + 1) = .Cells(2, 3).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Date, example, 0) + 1) = .Cells(2, 5).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Code, example, 0) + 1) = .Cells(4 + i, 2).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Desc, example, 0) + 1) = .Cells(4 + i, 3).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Price, example, 0) + 1) = .Cells(4 + i, 4).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Sprice, example, 0) + 1) = .Cells(4 + i, 5).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Disc, example, 0) + 1) = .Cells(4 + i, 6).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Payment, example, 0) + 1) = .Cells(4 + i, 7).Value
MH.Cells(i + LastRow2, Application.WorksheetFunction.Match(Bank, example, 0) + 1) = .Cells(4 + i, 8).Value
end with
next a
next i