如何使用 VS 2008 VB 将一个单元格或一系列单元格从一个 Excel 电子表格复制到另一个 Excel 电子表格
我在 VS 2008 VB 中创建了一个 GUI(简单的部分),它有两个文本框(一个用于选择目标 Excel 文件,另一个用于选择目标 Excel 文件)。它们都附带一个浏览按钮(使用 OpenDialog 浏览文件选择 - 只读)。此外,我还有两个文本框,我已将其大小调整为侏儒,以容纳行和列(如果用户想要复制单元格 1-6 和列 A -D,他们只需输入 1-6在第一个文本框中,AD 在第二个文本框中)。最后,我在 GUI 底部有一个复制按钮,当所有字段都有相关数据时按下该按钮,以复制选定的单元格。我对如何通过提示用户输入数字和列来复制单元格或单元格范围以及如何实际实现复制单元格的逻辑感到困惑。我看过一些例子,但我还没有成功地将它们应用到我的特定应用程序中。感谢任何帮助...谢谢!
VS 2008 VB 代码:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
strm.Close()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selectedFile As String = String.Empty
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
selectedFile = OpenFileDialog1.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim selectedFile As String = String.Empty
If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
selectedFile = OpenFileDialog2.FileName
End If
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub OpenFileDialog2_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog2.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog2.OpenFile()
TextBox2.Text = OpenFileDialog2.FileName.ToString()
If Not (strm Is Nothing) Then
strm.Close()
End If
End Sub
Private Sub CopyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Copy_Btn.Click
End Sub
End Class
I have created a GUI (the easy part) in VS 2008 VB that has two text boxes (one to select the target Excel file, and the other to select the destination Excel file). They both are accompanied by a browse button (uses OpenDialog to browse files select - ReadOnly). Additionally, I have two more text boxes which I have re-sized to midgets, to accommodate row/s and column/s (if a user wanted to copy cells 1-6 and columns A -D, they would simply enter 1-6 in the first text box, and A-D in the second text box). Lastly, I have a copy button at the bottom of the GUI, to be pressed once all fields have relevant data, as to copy the selected cell/s. I'm befuddled on how to copy a cell or a range of cells, by prompting the user for a number and a column and how to actually implement the logic of copying the cells. I've seen examples, but I have not successfully been able to apply them to my particular application. Any help is appreciated ... Thanks!
VS 2008 VB Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
strm.Close()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selectedFile As String = String.Empty
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
selectedFile = OpenFileDialog1.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim selectedFile As String = String.Empty
If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
selectedFile = OpenFileDialog2.FileName
End If
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub OpenFileDialog2_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog2.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog2.OpenFile()
TextBox2.Text = OpenFileDialog2.FileName.ToString()
If Not (strm Is Nothing) Then
strm.Close()
End If
End Sub
Private Sub CopyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Copy_Btn.Click
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您最感兴趣的是如何获取行范围输入(例如“1-5”)和列范围输入(例如“AC”)并将这些输入定义的范围复制到另一个工作表(或工作簿) ?)。我整理了一些 VBA 来展示如何做到这一点。我没有花时间构建错误处理或输入验证,但对于格式正确的输入,此方法将起作用。请注意,我已将此示例代码设置为从活动工作簿的工作表 1 复制并粘贴到活动工作簿的工作表 2 中。
If I understand correctly, you're mostly interested in how to take the row range input (e.g. "1-5") and the column range input (e.g. "A-C") and copy the range those inputs define into another sheet (or workbook?). I pulled together some VBA to show how you can do this. I didn't take the time to build in error handling or input validation, but for properly formatted input, this approach will work. Beware that I have this sample code set up to copy from sheet 1 of your active workbook and paste into sheet 2 of the active workbook.