English 中文(简体)
• 如何复制一个电池或一系列电池,从一个Excel电子表格到另一个Excel Spreadsheet与VS 2008 VB
原标题:How to copy a cell or a range of cells from one Excel spreadsheet to another Excel Spreadsheet with VS 2008 VB
  • 时间:2011-10-12 12:32:17
  •  标签:
  • excel
  • vba

我在2008年VSB创建了一个全球倡议(容易部分),有两箱案文(一个是选择目标Excel文档,另一个是选择目的地Excel文档)。 这两份文件都有一个浏览量纽吨(用于选择浏览文档——阅读)。 此外,我还有另外两箱文字箱,再用到中点,以容纳浏览量和栏目(如果用户想要复制61-6和A-D栏,他们就会在第一盒中进入1-6,而A-D在第二盒中。) 最后, 我在德国马克底部有一顿影印件,一旦所有领域都拥有相关数据,就将印刷选定的电池。 关于如何复制一个囚室或一系列囚室,通过将用户带入一个号码和一栏,以及如何实际执行复制这些囚室的逻辑,对Im进行了辩驳。 我看到了一些例子,但我未能成功地将其应用于我的特别申请。 任何帮助都值得赞赏。 感谢!

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
问题回答

如果我理解正确的话,你对如何将浏览范围投入(例如“1-5”)和“一栏范围投入”(例如“A-C”)和将这些投入的范围确定为另一个表格(或工作手册)表示兴趣。 我把一些美国律师协会聚集在一起,以显示你如何能够做到这一点。 我没有时间在错误处理或输入验证方面加以发展,但为了适当格式的投入,这种办法将发挥作用。 我知道,我制定了这份样本守则,以复制你积极工作手册的表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




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签