[英]VBA copy and paste data based on certain user inputed criteria from one workbook to another?
所以我有工作簿 1,其中包含我們帳戶中每個人的當月交易詳細信息(E 列的帳號列為“-xxxxx”)。 在工作簿 2 中,我為工作簿 1 中的每個唯一帳號都有一張工作表(B3 的每個工作表中的帳號,格式為“xxxxx”,沒有“-”,就像工作簿 1 中一樣)。 我需要按帳號過濾工作簿 1 中的所有交易(最好是通過工作簿 2 中的用戶選擇范圍,以便我可以單擊單元格 B3 或它自動選擇活動工作表的 B3 並且它知道按該值過濾)然后有復制並粘貼到工作簿 2 的相應工作表中的那些過濾行的 A、C 和 F 列的數據(可能再次是活動工作表,因為我將從工作簿 2 中的每個工作表運行宏)。
例如
我對編碼/vba 非常陌生,請原諒我的無知。 盡我所能去學習。
復制代碼:
Sub Copy_data()
Application.ScreenUpdating = False
Dim UserRange As Range
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim UserAcc As Range
Dim LastRow As Long
Set wb1 = Workbooks("Fresh AMEX May 24- June 22.xlsx") 'source book, can't figure out how to make this user input. inputbox?'
Set wb2 = ThisWorkbook
Set ws1 = Workbooks("Fresh AMEX May 24- June 22.xlsx").Sheets("Transaction Details") 'assuming the worksheet can just be selected and the first workbook selection can be omitted'
Set ws2 = ThisWorkbook.Sheets("Test1")
'user input to select account number for criteria'
On Error Resume Next
Set UserAcc = Application.InputBox(Prompt:="Select an ACCOUNT NUMBER", Title:="Select an ACCOUNT NUMBER", Default:=ActiveCell.Address, Type:=8)
If UserAcc Is Nothing Then Exit Sub
On Error GoTo 0
With ws1
LastRow = Cells(Rows.Count, "E").End(xlUp).Row
ws1.Range("A1:0" & LastRow).AutoFilter Field:=5, Criteria1:="UserAcc"
Intersect(.Offset(1), .Parent.Range("A:A,C:C,F:F")).Copy 'found this online that is supposed to only copy the desired columns, but can't get ti to work'
With ws2.Range("A" & LastRow(ws2) + 1) 'also get a compile error here at LastRow if I omit the abover Intersect function. I get "expected array" as the error.
.PasteSpecial xlPasteValues
End With
End With
End Sub
負數去除碼:
Sub Deleter()
Dim xRg As Range
Dim xCell As Range
Dim xTxt As String
Dim i As Long
On Error Resume Next
xTxt = ActiveWindow.RangeSelection.Address
Sel:
Set xRg = Nothing
Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
If xRg.Areas.Count > 1 Then
MsgBox "does not support multiple selections, please select again", vbInformation, "Kutools for Excel"
GoTo Sel
End If
If xRg.Columns.Count > 1 Then
MsgBox "does not support multiple columns, please select again", vbInformation, "Kutools for Excel"
GoTo Sel
End If
For i = xRg.Rows.Count To 1 Step -1
If xRg.Cells(i) < 0 Then xRg.Cells(i).EntireRow.Delete
Next
End Sub
非常感謝大家的幫助!
你大部分時間都在那里。 要讓最終用戶選擇文件,您可以使用Application.GetOpenFileName
方法。 我還糾正了其他一些事情,並對代碼進行了大量注釋,以使其更清楚它在做什么:
Sub Copy_data()
'Have end user select file using the Application.GetOpenFilename method
Dim sSrcFilePath As String
sSrcFilePath = Application.GetOpenFilename("Excel Files, *.xls*", , "Select Excel File", , False)
If sSrcFilePath = "False" Then Exit Sub 'Pressed cancel
'Delcare variables
Dim wbSrc As Workbook: Set wbSrc = Workbooks.Open(sSrcFilePath) 'Open selected workbook
Dim wsSrc As Worksheet: Set wsSrc = wbSrc.Worksheets("Transaction Details")
Dim wbDst As Workbook: Set wbDst = ThisWorkbook
Dim wsDst As Worksheet: Set wsDst = wbDst.ActiveSheet
Dim rAcctNum As Range: Set rAcctNum = wsDst.Range("B3")
Dim rDst As Range: Set rDst = wsDst.Cells(wsDst.Rows.Count, "A").End(xlUp).Offset(1)
If rDst.Row < 7 Then Set rDst = wsDst.Range("A7") 'Guarantee that paste starts at row 7, otherwise append to existing data
'Verify the account number is a valid number
If Not IsNumeric(rAcctNum.Value) Then
wbSrc.Close False 'Close the source workbook due to error
rAcctNum.Select
MsgBox "ERROR: Account number must be numeric!"
Exit Sub
End If
'Filter on column E of source worksheet
With wsSrc.Range("E1", wsSrc.Cells(wsSrc.Rows.Count, "E").End(xlUp))
.AutoFilter 1, -rAcctNum.Value 'Notice the - in front of the rAcctNum in order to filter properly
'This is because in your description, the source workbook has "-xxxxx" for the acct nums
'Copy filtered values from columns A, C, and F
'You were close here, need to make sure you specify .EntireRow so that you don't end up with an error on the Intersect
Intersect(.Offset(1).EntireRow, .Parent.Range("A:A,C:C,F:F")).Copy
rDst.PasteSpecial xlPasteValues 'Paste to destination as values
.AutoFilter 'Remove filter
.Parent.Parent.Close False 'Close source workbook, False means Don't save changes
End With
'Prep selected cells for your Deleter Sub
Intersect(Selection, wsDst.Columns("C")).Select
Call Deleter
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.