如何使用 VS 2008 VB 将一个单元格或一系列单元格从一个 Excel 电子表格复制到另一个 Excel 电子表格

发布于 2024-12-09 17:24:08 字数 2365 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-12-16 17:24:08

如果我理解正确,您最感兴趣的是如何获取行范围输入(例如“1-5”)和列范围输入(例如“AC”)并将这些输入定义的范围复制到另一个工作表(或工作簿) ?)。我整理了一些 VBA 来展示如何做到这一点。我没有花时间构建错误处理或输入验证,但对于格式正确的输入,此方法将起作用。请注意,我已将此示例代码设置为从活动工作簿的工作表 1 复制并粘贴到活动工作簿的工作表 2 中。

Sub promptncopy()

Dim rowStr As String, colStr As String, tmpAdd As String
Dim hypr As Integer, hypc As Integer
Dim sIn As Worksheet, sOut As Worksheet
Dim rIn As Range, rOut As Range

'Input boxes for row and column ranges.  It sounds like you already have some code like this.
rowStr = InputBox("Enter row numbers you want to copy as range (e.g. 1-5).", "Enter Rows")
If rowStr = "" Then
    Exit Sub
End If
colStr = InputBox("Enter column letters you want to copy as range (e.g. A-E).", "Enter Columns")
If colStr = "" Then
    Exit Sub
End If

'Records position of hyphen in input strings.
hypr = InStr(rowStr, "-")
hypc = InStr(colStr, "-")

'Combines colStr and rowStr to form appropriate address for input range.
'Handles cases where colStr and/or rowStr are single values rather than ranges.
If hypc <> 0 Then
    If hypr <> 0 Then
        tmpAdd = Left(colStr, hypc - 1) & Left(rowStr, hypr - 1) & ":" & Right(colStr, Len(colStr) - hypc) & Right(rowStr, Len(rowStr) - hypr)
    Else
        tmpAdd = Left(colStr, hypc - 1) & rowStr & ":" & Right(colStr, Len(colStr) - hypc) & rowStr
    End If
Else
    If hypr <> 0 Then
        tmpAdd = colStr & Left(rowStr, hypr - 1) & ":" & colStr & Right(rowStr, Len(rowStr) - hypr)
    Else
        tmpAdd = colStr & rowStr
    End If
End If

'Sets In and Out sheets to first and second sheets in workbook, respectively.
Set sIn = Sheets(1)
Set sOut = Sheets(2)
Set rIn = sIn.Range(tmpAdd)
Set rOut = sOut.Range("A1").Resize(rIn.Rows.Count, rIn.Columns.Count)
'Prints values (not formulas!) from input range to output range.
rOut = rIn.Value

End Sub

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.

Sub promptncopy()

Dim rowStr As String, colStr As String, tmpAdd As String
Dim hypr As Integer, hypc As Integer
Dim sIn As Worksheet, sOut As Worksheet
Dim rIn As Range, rOut As Range

'Input boxes for row and column ranges.  It sounds like you already have some code like this.
rowStr = InputBox("Enter row numbers you want to copy as range (e.g. 1-5).", "Enter Rows")
If rowStr = "" Then
    Exit Sub
End If
colStr = InputBox("Enter column letters you want to copy as range (e.g. A-E).", "Enter Columns")
If colStr = "" Then
    Exit Sub
End If

'Records position of hyphen in input strings.
hypr = InStr(rowStr, "-")
hypc = InStr(colStr, "-")

'Combines colStr and rowStr to form appropriate address for input range.
'Handles cases where colStr and/or rowStr are single values rather than ranges.
If hypc <> 0 Then
    If hypr <> 0 Then
        tmpAdd = Left(colStr, hypc - 1) & Left(rowStr, hypr - 1) & ":" & Right(colStr, Len(colStr) - hypc) & Right(rowStr, Len(rowStr) - hypr)
    Else
        tmpAdd = Left(colStr, hypc - 1) & rowStr & ":" & Right(colStr, Len(colStr) - hypc) & rowStr
    End If
Else
    If hypr <> 0 Then
        tmpAdd = colStr & Left(rowStr, hypr - 1) & ":" & colStr & Right(rowStr, Len(rowStr) - hypr)
    Else
        tmpAdd = colStr & rowStr
    End If
End If

'Sets In and Out sheets to first and second sheets in workbook, respectively.
Set sIn = Sheets(1)
Set sOut = Sheets(2)
Set rIn = sIn.Range(tmpAdd)
Set rOut = sOut.Range("A1").Resize(rIn.Rows.Count, rIn.Columns.Count)
'Prints values (not formulas!) from input range to output range.
rOut = rIn.Value

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文