通过使用带有 excel vba 的 do while 循环进行乘法 table
making multiplication table by using do while loop with excel vba
我正在尝试使用 "do while loop" 进行二维动态乘法 table 但我只能使用此代码获得 1 行有人可以帮忙吗?我需要使用 do while 循环而不是另一个循环。
Sub multp_doWhileLoop()
Dim i As Long
Dim j As Long
Dim k As Long
Dim l As Long
k = Range("b1").Value
l = Range("d1").Value
ReDim multp_tab(k, l) As Long 'defining ixj matrix
i = 1
j = 1
Range("a2:zz100000").Clear
Cells(2, 1) = "mult."
Do While i < k + 1
Do While j < l + 1
Cells(i + 2, 1).Value = i 'for frame
Cells(2, j + 1).Value = j '
multp_tab(i, j) = i * j
j = j + 1
Loop
i = i + 1
Loop
i = 1
j = 1
Do While i < k + 1
Do While j < l + 1
Cells(i + 2, j + 1).Value = multp_tab(i, j)
j = j + 1
Loop
i = i + 1
Loop
End Sub
请告诉我,我没有帮助你考试作弊。
Sub multp_doWhileLoop()
' 09 Apr 2017
Dim Mtbl As Variant ' Multiplication table
Dim R As Long ' row
Dim C As Long ' column
Dim Rmax As Long ' try to give meaning to the names
Dim Cmax As Long ' you give to your variables
With ActiveSheet
Rmax = .Cells(1, "B").Value
Cmax = .Cells(1, "D").Value
If Rmax < 1 Or Cmax < 1 Then
MsgBox "Please enter valid parameters in row 1.", _
vbExclamation, "Invalid matrix parameters"
Exit Sub
End If
ReDim Mtbl(1 To Rmax, 1 To Cmax) 'defining ixj matrix
Do
R = R + 1
C = 0
Do
C = C + 1
Mtbl(R, C) = R * C
Loop While C < Cmax
Loop While R < Rmax
With .UsedRange
.Range(.Cells(2, 1), .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
.Cells(2, 1).Resize(Rmax, Cmax).Value = Mtbl
End With
End Sub
这是一个简短的解决方案:
Sub multp()
For c = 1 To 10
For r = 1 To 10
ThisWorkbook.Sheets("sheet1").Cells(r, c).Value = r * c
Next r
Next c
End Sub
我正在尝试使用 "do while loop" 进行二维动态乘法 table 但我只能使用此代码获得 1 行有人可以帮忙吗?我需要使用 do while 循环而不是另一个循环。
Sub multp_doWhileLoop()
Dim i As Long
Dim j As Long
Dim k As Long
Dim l As Long
k = Range("b1").Value
l = Range("d1").Value
ReDim multp_tab(k, l) As Long 'defining ixj matrix
i = 1
j = 1
Range("a2:zz100000").Clear
Cells(2, 1) = "mult."
Do While i < k + 1
Do While j < l + 1
Cells(i + 2, 1).Value = i 'for frame
Cells(2, j + 1).Value = j '
multp_tab(i, j) = i * j
j = j + 1
Loop
i = i + 1
Loop
i = 1
j = 1
Do While i < k + 1
Do While j < l + 1
Cells(i + 2, j + 1).Value = multp_tab(i, j)
j = j + 1
Loop
i = i + 1
Loop
End Sub
请告诉我,我没有帮助你考试作弊。
Sub multp_doWhileLoop()
' 09 Apr 2017
Dim Mtbl As Variant ' Multiplication table
Dim R As Long ' row
Dim C As Long ' column
Dim Rmax As Long ' try to give meaning to the names
Dim Cmax As Long ' you give to your variables
With ActiveSheet
Rmax = .Cells(1, "B").Value
Cmax = .Cells(1, "D").Value
If Rmax < 1 Or Cmax < 1 Then
MsgBox "Please enter valid parameters in row 1.", _
vbExclamation, "Invalid matrix parameters"
Exit Sub
End If
ReDim Mtbl(1 To Rmax, 1 To Cmax) 'defining ixj matrix
Do
R = R + 1
C = 0
Do
C = C + 1
Mtbl(R, C) = R * C
Loop While C < Cmax
Loop While R < Rmax
With .UsedRange
.Range(.Cells(2, 1), .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
.Cells(2, 1).Resize(Rmax, Cmax).Value = Mtbl
End With
End Sub
这是一个简短的解决方案:
Sub multp()
For c = 1 To 10
For r = 1 To 10
ThisWorkbook.Sheets("sheet1").Cells(r, c).Value = r * c
Next r
Next c
End Sub