Excelmacros插入数据从另一个工作簿

有人可以帮助我做到以下几点:我需要复制(或使用类似的VBA函数)一系列的单元格到另一个工作表。

但是,范围可能包含空单元格,我不想包含数据,我也需要插入值(而不是粘贴),以便它们不会覆盖目标工作表上已有的东西。 .Value而不是.PasteSpecial.PasteSpecial是首选。

希望这是有道理的


编辑

这是我尝试过的一些代码,但是这仍然覆盖(即使选中的单元格为空)目标工作表中的数据。

 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 

所以基本上它只需要find数据并将其复制/插入到目标工作表中。

习惯了HTML和PHP,这对我来说是一个黑暗的跳跃:)

非常感谢大家

下面是你正在寻找什么样的代码样本。

不过,我build议在发布类似问题之前,先在VBA编程中再做一些Googlesearch。 这可以让你改进下面列出的代码。 例如, Cells.SpecialCells(xlCellTypeLastCell).Row使用函数/方法中的内build来确定你想要复制的最后一行是什么; 这可能是也可能不是你正在寻找的。

此外,这个问题非常类似于堆栈溢出的其他问题,例如: 逐个复制范围值

不过,如果您希望使用下面的代码,只需将源工作表从“Sheet1”更改为您的源数据所在的表格,并将源范围从“C1”更改为您正在使用的表格中的任一范围。 同样,你需要做的目标。

 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 

更新的代码

此代码已更新以匹配包含多个单元格的input源范围。

 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 

简单的价值来源于范围

 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 环境函数