我必須能夠在一個范圍內給兩個輸入一個開始和一個結束數字。我必須能夠推斷出介于兩者之間的所有值,包括開始和結束數字。我已經制作了一個卡住的回圈:) 我還在一些輸入框中定義了起始編號和結束編號,并成功地將起始編號放入 A1。
在我的例子中,數字總是未知的,一個人總能分辨出開始和結束的數字是什么,但它們總是會改變。
Sub FindNum()
Dim iLowVal As Integer
Dim iHighVal As Integer
iLowVal = InputBox("Beginning Range")
iHighVal = InputBox("Ending Range")
Range("A1").Value = iLowVal
Do Until iLowVal = iHighVal
Range("A1" & i) = iLowVal 1
Loop
End Sub
uj5u.com熱心網友回復:
現在就開始學習如何使用變體陣列。使用它們并批量分配到作業表比回圈作業表更快。
使用陣列時,使用 For 回圈會更快。
Sub FindNum()
Dim iLowVal As Long
Dim iHighVal As Long
iLowVal = InputBox("Beginning Range")
iHighVal = InputBox("Ending Range")
Dim itNum As Long
itNum = iHighVal - iLowVal 1
Dim arr As Variant
ReDim arr(1 To itNum)
Dim k As Long
k = iLowVal
Dim i As Long
For i = 1 To itNum
arr(i) = k
k = k 1
Next i
ActiveSheet.Range("A1").Resize(itNum).Value = Application.Transpose(arr)
End Sub
uj5u.com熱心網友回復:
寫一個整數陣列 ( For...Next)
- 這只是一個帶有更多選項的基本代碼。玩弄它(即修改
"A1", "A", (1, 0))以查看它的行為,這樣你就可以通過谷歌搜索、SOing、提出另一個問題等來改進它。
Option Explicit
Sub FindNumForNext()
' Input data.
Dim nStart As Long: nStart = InputBox(Prompt:="Start Value", _
Title:="Write an Array of Integers", Default:=1)
Dim nEnd As Long: nEnd = InputBox("End Value")
' Check out 'Application.InputBox' as a better way to input data.
' Determine the order (asc or desc).
Dim nStep As Long
If nStart <= nEnd Then ' ascending
nStep = 1
Else ' descending
nStep = -1
End If
' Create a reference to the destination worksheet.
Dim dws As Worksheet: Set dws = ActiveSheet ' the one you're looking at
' Instead of the previous line, it is safer (better) to determine
' the exact worksheet where this will be used, e.g.:
'Dim dwb As Workbook: Set dwb = ThisWorkbook ' workbook containing this code
'Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet1") ' tab name
' Create a reference to the first destination cell.
Dim dCell As Range: Set dCell = dws.Range("A1")
' Clear possible previous data.
dws.Columns("A").Clear
Dim n As Long ' Values (Numbers) Counter (For Next Control Variable)
' Loop from start to end...
For n = nStart To nEnd Step nStep
' Write the current number to the current destination cell.
dCell.Value = n
' Create a reference to the next destination cell.
Set dCell = dCell.Offset(1, 0)
' 1 means one cell down
' 0 means zero cells to the right
Next n ' next value (number)
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/348750.html
上一篇:將多行合并在一張紙中
