简体   繁体   English

Excel宏插入另一个工作簿中的数据

[英]Excel Macro Insert Data From another workbook

Could someone help me to do the following: I need to copy (or using a similar VBA function) a range of cells to another worksheet. 有人可以帮助我执行以下操作:我需要将一系列单元格复制(或使用类似的VBA函数)到另一个工作表。

However, the range will probably include empty cells that I don't want to include with the data, and I also need to insert the values (rather than Paste) so they don't override stuff already on the destination sheet. 但是,该范围可能包含我不想包含在数据中的空单元格,并且我还需要插入值(而不是粘贴),以便它们不会覆盖目标工作表中已有的内容。 .Value rather than .Paste or .PasteSpecial is preferred. .Value而非.Paste.PasteSpecial是优选的。

Hope this makes sense 希望这有意义


EDIT 编辑

Here is some code I have tried, but this still overrides (even if the selected cells were empty) the data in the destination sheet. 这是我尝试过的一些代码,但这仍然会覆盖目标工作表中的数据(即使所选单元格为空)。

Sub MakeQuote()
Application.ScreenUpdating = False
  Sheets("Code Input Sheet").Range("A9:C800").Copy
  Workbooks.Open("C:\Users\j\Documents\Trial.xltm").Activate
  Sheets("Special Quote").Range("A4").PasteSpecial xlPasteValues, skipBlanks:=False
  Application.CutCopyMode = False
Err_Execute:
    If Err.Number = 0 Then MsgBox "Copying Successful :)" Else _
    MsgBox Err.Description
    Application.ScreenUpdating = True
End Sub

So basically it needs to find just the data and copy/insert it into the destination sheet. 因此,基本上它只需要查找数据并将其复制/插入到目标表中即可。

Being used to HTML and PHP this is a bit of a jump in the dark for me :) 对于HTML和PHP来说,这对我来说是个飞跃:)

Many thanks everyone 非常感谢大家

A sample of what kind of code you are looking for is below. 您在寻找什么样的代码示例如下。

However I would recommend doing a few more Google searches on VBA programming before posting similar questions. 但是,我建议在发布类似问题之前再对VBA编程进行一些Google搜索。 This will let you improve upon code listed below. 这可以让您改进下面列出的代码。 For example, Cells.SpecialCells(xlCellTypeLastCell).Row uses a build in function/method to figure out what is the last row you wish to copy; 例如, Cells.SpecialCells(xlCellTypeLastCell).Row使用内置函数/方法来找出您要复制的最后一行; which may or may not be what you are looking for. 可能不是您要找的东西。

Further this question highly resembles other questions on Stack Overflow for example: Copy range values one by one 此外,此问题与“堆栈溢出”中的其他问题高度相似,例如: 逐一复制范围值

Nevertheless if you wish to use the code below just change the source worksheet from "Sheet1" to whichever sheet your source data is from, and the source range from "C1" to whichever range on that sheet you are using. 但是,如果您希望使用下面的代码,只需将源工作表从“ Sheet1”更改为源数据来自哪个工作表,并将源范围从“ C1”更改为您正在使用的工作表中的任何范围。 Similarly you'll need to do with the target. 同样,您将需要处理目标。

Sub rangeCopy()
    Dim sourceRange As Range
    Dim targetRange As Range
    Dim lastRow As Long
    Dim sourceCounter As Long
    Dim targetCounter As Long
    Dim outString As String

    'Change the source and target sheet as needed.
    Set sourceRange = Sheets("Sheet1").Range("C1")
    Set targetRange = Sheets("Sheet2").Range("A1")

    lastRow = sourceRange.Parent.Cells.SpecialCells(xlCellTypeLastCell).Row

    For sourceCounter = 0 To lastRow - 1

        'Copy the cell you want to transfer from the source apge
        outString = Trim(sourceRange.Offset(sourceCounter).Value)

        'Find the next empty cell in the target worksheet
        While (Trim(targetRange.Offset(targetCounter).Value) <> "")
            targetCounter = targetCounter + 1
        Wend

        targetRange.Offset(targetCounter).Value = outString
    Next

End Sub

Updated Code 更新的代码

This code was updated to match an input source range that contains multiple cells. 此代码已更新,以匹配包含多个单元格的输入源范围。

sub rangeCopy()
    Dim sourceRange As Range, loopRange As Range
    Dim targetRange As Range
    Dim lastRow As Long
    Dim sourceCounter As Long
    Dim targetCounter As Long
    Dim outString As String
    Dim startRow As Long
    Dim startCol As Long
    Dim endCol As Long
    Dim colCounter As Long

    'Change the source and target sheet as needed.
    Set sourceRange = Sheets("Sheet1").Range("B2:C34")
    Set targetRange = Sheets("Sheet2").Range("A1")

    startRow = sourceRange.Row
    lastRow = sourceRange.Rows.Count
    startCol = sourceRange.Column
    endCol = sourceRange.Columns.Count - 1
    Set loopRange = sourceRange.Parent.Cells(startRow, startCol)


    For colCounter = 0 To endCol
        targetCounter = 0
        For sourceCounter = 0 To lastRow - 1

            'Copy the cell you want to transfer from the source apge
            outString = Trim(loopRange.Offset(sourceCounter, colCounter).Value)

            'Find the next empty cell in the target worksheet
            While (Trim(targetRange.Offset(targetCounter, colCounter).Value) <> "")
                targetCounter = targetCounter + 1
            Wend

            targetRange.Offset(targetCounter, colCounter).Value = outString
        Next
    Next

End Sub

Simple Value to Value From Range 简单值到范围内的值

Example

Option Explicit
'// Value to Value
Sub Value_To_Value()

    Dim FilePath As String

    '// Workbook1 File Path
    FilePath = Environ("USERPROFILE") & "\Documents\Temp\" '<- Change Path

    With Range("A1:A10")
        .Value = "='" & FilePath & "[Workbook1.xlsm]Sheet1'!B1:B10"
    End With

End Sub

MSDN Environ Function MSDN 环境功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM