搜索并找到一些关于VBA的线程导入已关闭工作簿的第一张,我正在尝试搜索已关闭工作簿的工作表,找到一个使用输入框输入的设置字。找到值后,将整个行拉出并粘贴到活动的第二个工作簿中。
以下是我一直致力于任何帮助的代码将不胜感激。
Dim srcWorkbook As Workbook
Dim destWorkbook As Workbook
Dim srcWorksheet As Worksheet
Dim destWorksheet As Worksheet
Dim SearchRange As Range
Dim destPath As String
Dim destname As String
Dim destsheet As String
Set srcWorkbook = ActiveWorkbook
Set srcWorksheet = ActiveSheet
Dim vnt_Input As String
vnt_Input = Application.InputBox("Please Enter Client Name", "Client Name")
destPath = "C:\test\"
destname = "Test2.xlsm"
destsheet = "Sheet1"
On Error Resume Next
Set destWorkbook = Workbooks(destname)
If Err.Number <> 0 Then
Err.Clear
Set wbTarget = Workbooks.Open(destPath & destname)
CloseIt = True
End If
For Each c In Range("A2:W100").Cells
If InStr(c, "vnt_Input") > 0 Then
c.EntireRow.Copy
destWorkbook.Activate
destWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset (1) .EntireRow.Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone,SkipBlanks:= _
False, Transpose:=False
srcWorkbook.Activate
亲切的问候,
答案 0 :(得分:0)
您需要进行一些更改。请参阅下面的完整代码我将评论这些变化:
Dim srcWorkbook As Workbook
Dim destWorkbook As Workbook
Dim srcWorksheet As Worksheet
Dim destWorksheet As Worksheet
Dim SearchRange As Range
Dim destPath As String
Dim destname As String
Dim destsheet As String
Set srcWorkbook = ActiveWorkbook
Set srcWorksheet = ActiveSheet
Dim vnt_Input As String
vnt_Input = Application.InputBox("Please Enter Client Name", "Client Name")
destPath = "C:\test\"
destname = "Quick Test.xlsm"
destsheet = "Sheet1"
On Error Resume Next
Set destWorkbook = ThisWorkbook
If Err.Number <> 0 Then
Err.Clear
Set wbTarget = Workbooks.Open(destPath & destname)
CloseIt = True
End If
For Each c In wbTarget.Sheets("Companies").Range("A2:W100") 'No need for the .Cells here
If InStr(c, vnt_Input) > 0 Then 'vnt_Input is a variable that holds a string, so you can't put quotes around it, or it will search the string for "vnt_Input"
c.EntireRow.Copy
destWorkbook.Sheets("Master").Range("A" & Rows.Count).End(xlUp).Offset(1,0).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone,SkipBlanks:= _
False, Transpose:=False 'Please don't use Select and Activate. There is almost never a need for it.
End if
Next c