我創建了以下Do
循環:VBA Do循環不工作
Sub Test2()
Set sht = Worksheets("Calculations")
LastRow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row + 1
LastCol = (LastRow - 2) * 3
i = 1
r = 3
c = 5
Do Until r > LastRow
sht.Cells(r, c).Select
RangeName = "Airline_" & i
Set Cell = sht.Cells(r, 5)
sht.Names.Add Name:=RangeName, RefersTo:=Cell
Do Until c > LastCol
RangeName = "Airline_" & i
Set Cell = Application.Union(Range("Airline_" & i), sht.Cells(r, c))
sht.Names.Add Name:=RangeName, RefersTo:=Cell
c = c + 3
Loop
i = i + 1
r = r + 1
Loop
End Sub
乍一看,一切都似乎是好的。但是當r
變爲4時,看起來Do until c > LastCol
不起作用。
下面是一些截圖,爲解釋:
你可能需要開始前設置爲'c = 5'你'做,直到C> LastCol'循環,否則'C'仍然有它曾在以前的循環結束的值。 (我不確定你的'sht.Cells(r,c).Select'的目的是什麼 - 你可能實際上需要在該行之前設置'c = 5',但是移動它可能會更好)。 – YowE3K