2021-07-21 VB窗体求范围质数(Excel复制工作簿)
Imports System.IO.Ports'Imports System.IO
'Imports ADOXPublic Class Form1Dim 列表 As New DataGridViewDim wb1 As New TextBoxDim wb2 As New TextBoxDim aj As New ButtonDim xs As New TextBoxDim 串行端口 = New SerialPortPrivate Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadMe.Text = "VB解答专用窗体"wb1.Parent = Mewb1.Text = "3"wb2.Parent = Mewb2.Text = "47"xs.Parent = Mexs.Text = "质数:"aj.Parent = Meaj.Text = "查找"wb2.Location = New Point(wb1.Width + 7)aj.Location = New Point(wb1.Width + wb2.Width + 7 * 2)xs.Location = New Point(wb1.Width + wb2.Width + aj.Width + 7 * 3)xs.Width = Me.Width - (wb1.Width + wb2.Width + aj.Width + 7 * 3)xs.WordWrap = Truexs.Height = Me.Heightxs.Multiline = TrueAddHandler aj.Click, AddressOf 按键单击End SubPrivate Sub 按键单击(sender As Object, e As EventArgs)'缘由https://ask.csdn.net/questions/7477534?spm=1005.2025.3001.5141xs.Text = "质数:"Dim a = Integer.Parse(wb1.Text.Trim())Dim b = Integer.Parse(wb2.Text.Trim())Dim c As Integer = 3While a <= b And a > 1If a = 2 Or a = 3 Thenxs.AppendText(a.ToString("0 "))a = IIf(a = 2, a + 1, a + 2)ElseIf c <= a / c ThenIf (a Mod c = 0) Thena = a + 2c = 3Elsec = c + 2End IfElsexs.AppendText(a.ToString("0 "))a = a + 2c = 3End IfEnd WhileEnd SubPrivate Sub Excel复制工作簿()Dim myExcelApp As New Microsoft.Office.Interop.Excel.ApplicationmyExcelApp.Workbooks.Open(System.Environment.CurrentDirectory + "\\测试用例.xlsx", Type.Missing)myExcelApp.Workbooks.Open(System.Environment.CurrentDirectory + "\\复制用例.xlsx", Type.Missing)Dim worksheet1 As Microsoft.Office.Interop.Excel.Worksheet = CType(myExcelApp.Workbooks(1).Worksheets(1), _Microsoft.Office.Interop.Excel.Worksheet)Dim worksheet2 As Microsoft.Office.Interop.Excel.Worksheet = CType(myExcelApp.Workbooks(2).Worksheets(1), _Microsoft.Office.Interop.Excel.Worksheet)worksheet1.Copy(After:=worksheet2)myExcelApp.Workbooks(2).Save()myExcelApp.Quit()myExcelApp = NothingEnd SubEnd Class
缘由VB求素数问题,提示:须使用双分支选择结构。_软件工程-CSDN问答
Private Sub 按键单击(sender As Object, e As EventArgs)'缘由https://ask.csdn.net/questions/7477534?spm=1005.2025.3001.5141xs.Text = "质数:"Dim a = Integer.Parse(wb1.Text.Trim())If a < 30 ThenMessageBox.Show("数据无效可重新输入!", "友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)wb1.Focus()End IfDim b = Integer.Parse(wb2.Text.Trim())Dim c As Integer = 3, h As Integer = 0, q = 2While q <= a And q > 1If q = 2 Or q = 3 Thenxs.AppendText(q.ToString("0 "))h = h + qq = IIf(q = 2, q + 1, q + 2)ElseIf c <= q / c ThenIf (q Mod c = 0) Thenq = q + 2c = 3Elsec = c + 2End IfElsexs.AppendText(q.ToString("0 "))h = h + qq = q + 2c = 3End IfEnd Whilewb2.Text = hEnd Sub