我正在嘗試為用戶輸入數字和偶數在陣列中重復的位置制作一個宏。我已經得到了從 0-n 重復數字的代碼(n 是輸入的數字)。但是,我不知道如何將偶數重復兩次。
Sub Macro3()
For n = 1 To Worksheets("Sheet1").Cells(1, 2) 1
Cells(2, 1 n).Select
ActiveCell.FormulaR1C1 = (n - 1)
Next
End Sub
下面是輸出 當前代碼與我真正想要的
uj5u.com熱心網友回復:
寫一個整數陣列
- 將 0 和單元格中指定值之間的整數陣列寫入
B1從 開始的行范圍B2。偶數寫兩次。
Option Explicit
Sub WriteArrayOfIntegers()
Const ProcTitle As String = "Write Array of Integers"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
' Create a reference to the source cell.
Dim sCell As Range: Set sCell = ws.Range("B1")
' Write the value of the source cell to a variable.
Dim sValue As Variant: sValue = sCell.Value
Dim Last As Long
' Validate the source cell value.
If IsNumeric(sValue) Then ' is a number
Last = Abs(CLng(sValue)) ' positive ('Abs'), whole ('CLng')
Else ' is not a number
MsgBox "The value in cell '" & sCell.Address(0, 0) & "' ('" _
& sValue & "' is not a number.", vbCritical, ProcTitle
Exit Sub
End If
' Create a reference to the first destination cell.
Dim dCell As Range: Set dCell = ws.Range("B2"): dCell.Value = 0
Dim Size As Long: Size = 1
Dim n As Long
' Loop through the numbers and apply alternating row size (1 or 2)
' and column offset (2 or 1) before writing.
For n = 1 To Last
Set dCell = dCell.Offset(, Size) ' define next first cell
Size = 2 - n Mod 2 ' calculate the size (Odd = 1, Even = 2)
dCell.Resize(, Size).Value = n ' write to the resized row range
Next n
' Clear the range to the right of the last cell to remove any previous data.
Dim crrg As Range
With dCell.Offset(, Size) ' define next first cell
' Define the range from the next first to the last worksheet cell
' in the row.
Set crrg = .Resize(, ws.Columns.Count - .Column 1)
End With
crrg.Clear ' or crrg.ClearContents
MsgBox "Array of numbers written.", vbInformation, ProcTitle
End Sub
uj5u.com熱心網友回復:
你的代碼真的沒問題,只需添加問題是偶數和一個變數來看看寫在哪里。也只需將 n 回圈從 0 更改為:
Sub Macro3()
For n = 0 To Worksheets("Sheet1").Cells(1, 2)
a = a 1
Cells(2, 2 a).Select
ActiveCell.FormulaR1C1 = n
'check if number is even and check if a > 1 because we don't whant to repeat 0
If n Mod 2 = 0 And a > 1 Then
a = a 1
Cells(2, 2 a).Select
ActiveCell.FormulaR1C1 = n
End If
Next
End Sub
uj5u.com熱心網友回復:
通過 ArrayList 快速替代
使用ArrayList (順便說一下.Sort,處理, .Remove, .Insert,等方法.Reverse)可能是一種以非常易讀的方式操作陣列資料的便捷方式。它不是 VBA 的一部分,但可以通過后期系結輕松訪問(參考 .Net 庫 mscorlib.dll)。
Option Explicit ' code module head
Sub DoubleEvenNumbersGreaterOne()
'a) define upper limit
Dim ws As Worksheet
Set ws = Sheet1 ' << change to project's sheet Code(Name)
Dim Limit As Long
Limit = ws.Range("B1")
'b) declare ArrayList
Dim arr As Object ' late bind .Net mscorlib.dll
Set arr = CreateObject("System.Collections.ArrayList")
'c) populate list array
arr.Add 0 ' start adding with zero
Dim i As Long
For i = 1 To Limit ' loop through sequence 1:Limit
arr.Add i ' add current number
If i Mod 2 = 0 Then arr.Add i ' additional even number
Next
'd) get array
Dim a As Variant: a = arr.ToArray ' change ArrayList object to VBA array
'Debug.Print Join(a, "|") ' optional check in VB Editor's immediate window
'e) write 0-based 1-dim array to ws (here: Sheet1) or declare another target worksheet (e.g. ws2)
With ws.Range("B2")
.EntireRow = vbNullString ' empty target row
.Resize(1, UBound(a) 1) = a ' write values into correct number of cells
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/342811.html
