VBA:在两个打开的工作表之间导入数据(无路径)

时间:2016-10-03 19:29:21

标签: excel vba excel-vba

我打开两个属于3D CAD文件的工作簿,但没有真正的路径。

无论如何,在我手动打开它之后,我希望VBA将工作簿B中的A1移动到工作簿A.

为了让VBA知道源名称,我说工作簿B的名称存储在工作簿A的B1中

如果我有路径,我知道如何移动数据但是因为有“无”,我该怎么做?谢谢!

2 个答案:

答案 0 :(得分:1)

希望得到这个帮助。

Dim FrmWorkbook As Workbook
Dim iCount As Integer

'Capture the source workbook
For i = 1 To ThisWorkbook.Application.Workbooks.Count
    'change the name of your source Workbook below
    If ThisWorkbook.Application.Workbooks(i).Name = "Book1.xlsx" Then
        Set FrmWorkbook = ThisWorkbook.Application.Workbooks(i)
        Exit For
    End If
Next

'option 1: row by row or column by colum.  (Column C)
Dim lRow As Long

For lRow = 1 To 100
    ThisWorkbook.Sheets(1).Range("C" & lRow).Value = FrmWorkbook.Sheets(1).Range("C" & lRow).Value
Next

'option 2: copy the entire sheet (assuming Sheet1)
With FrmWorkbook.Sheets(1)
    .Activate
    .Select
    .Copy before:=ThisWorkbook.Sheets(1)
End With

Set FrmWorkbook = Nothing

MsgBox "DONE"

答案 1 :(得分:0)

来回走后我发现这也有效:

Private Sub import_Click()

Dim wb1 As Workbook
Dim wb2 As Workbook

Dim ws1 As Worksheet
Dim ws2 As Worksheet

Set wb2 = ActiveWorkbook
Set ws2 = wb2.Sheets("sheet1")

Set wb1 = Workbooks(ActiveSheet.Range("n2").Value & ".xls")
Set ws1 = wb1.Sheets("input")


ws2.Range("a1") = ws1.Range("f90").Value
End Sub

感谢您提出的好主意