2017-08-15 55 views
-1

我有此代碼,它將數據從「發票」工作表發送到「銷售工作簿」工作表,但經過考慮後認爲將數據完全發送到不同的工作簿會是有益的。我將如何使用下面的代碼來實現這個(因爲它花了我很長時間纔得到這個!)。這是代碼-這是原始問題。現在已經完全解決,並在下面更新 -Excel VBA-修改代碼,以便將數據從「發票」工作表傳輸至「銷售工作簿」工作表,然後保存至其他工作簿

下面的代碼現在可用。要解決的最後一個問題是,複製的數據也複製到空項目行上。我找到了一個簡單的解決方案,我將這裏的圖片下面的代碼複製。它基本上是一個自動運行的vba代碼,如果某個單元格中沒有數據,它將刪除一行。謝謝您的幫助。我感覺無敵!

Sub sendtosales() 
Dim WB As Workbook '''! 
Dim CurrentWB As Workbook '''! 
Dim WBLoc As String '''! 
Dim rng As Range 
Dim i As Long 
Dim a As Long 
Dim rng_dest As Range 
Application.ScreenUpdating = False 
WBLoc = "C:\Salestracker.xlsm" '''! Location of the workbook, trimmed down for public view 
Set CurrentWB = Excel.ThisWorkbook '''! 
Set WB = Workbooks.Open(WBLoc) '''! Opens the workbook 
i = 1 
Set rng_dest = WB.Sheets("Salestracker").Range("D:F") '''! Change Sheets() to whichever sheet you want to use 

' Find first empty row in columns D:F on sheet Sales Book 
Do Until WorksheetFunction.CountA(rng_dest.Rows(i)) = 0 
    i = i + 1 
Loop 

'Copy range A23:D27 on sheet Invoice to Variant array 
Set rng = CurrentWB.Sheets("Invoice").Range("A23:D27") '''! 

' Copy rows containing values to sheet Sales Book 
For a = 1 To rng.Rows.Count 
If WorksheetFunction.CountA(rng.Rows(a)) <> 0 Then 
    rng_dest.Rows(i).Value = rng.Rows(a).Value 
    With WB.Sheets("Salestracker") '''! Change Sheets() to whichever sheet you want to use 
    'Copy Invoice number 
    .Range("B" & i).Value = CurrentWB.Sheets("Invoice").Range("C18").Value '''! 
    'Copy Date 
    .Range("A" & i).Value = CurrentWB.Sheets("Invoice").Range("C15").Value '''! 
    'Copy Company name 
    .Range("C" & i).Value = CurrentWB.Sheets("Invoice").Range("A7").Value '''! 
    End With '''! 
    i = i + 1 
End If 
Next a 

WB.Close savechanges:=True '''! This wil close the Workbook and save changes 

Set WB = Nothing '''! Cleaning memory 
Set CurrentWB = Nothing '''! Cleaning memory 
Application.ScreenUpdating = True 

End Sub 

Image of Invoice and Salestracker with comments in red, and the problem bit on saletracker are greyed-out

下面是刪除那些在某一小區中沒有數據行的代碼,F在我區分

Sub killemptyF() 
On Error Resume Next 
Columns("F").SpecialCells(xlCellTypeBlanks).EntireRow.Delete 
End Sub 

這裏是代碼自動運行此模塊無論何時打開工作簿 -

Sub Auto_Run() 
Run ("killemptyF") 
End Sub 
+2

你想要它創建一個新的工作簿還是你已經有一個工作簿,你希望數據被複制到? – Slaqr

+1

避免循環,使用'nmbRowsD = ThisWorkbook.Sheets(「Sales Book」)。Range(「D」&Rows.Count).End(xlUp).Row + 1'找到第D列的第一個空行(可能是錯誤的,你可能需要D和F都是空的,如果是這樣,用nmbRowsD和nbmRowsF來使用函數** MAX **)。如果您已經有工作簿,請確保先打開它並將其中的範圍稱爲Workbook(「WorkbookName」)。Worksheet(「SheetName」)。Range() – AntiDrondert

+0

@slaqr我已經創建了一個工作簿。我想要將發送到「銷售預訂」表的數據發送到「Salestracker」工作簿(該書的第一張稱爲salestracker)。銷售手冊和銷售手冊完全相同,只是在不同的書中 – Peter

回答

1

像這樣的東西應該工作。我已添加/編輯的所有內容均標有'''!

Sub sendtosales() 
Dim WB as Workbook '''! 
Dim CurrentWB as Workbook '''! 
Dim WBLoc as String '''! 
Dim rng As Range 
Dim i As Long 
Dim a As Long 
Dim rng_dest As Range 
Application.ScreenUpdating = False 
WBLoc = "C:\Documents\Salestracker.xlsm" '''! Location of the workbook 
Set CurrentWB = Excel.ThisWorkbook '''! 
Set WB = Workbooks.Open(WBLoc) '''! Opens the workbook 
i = 1 
Set rng_dest = WB.Sheets(1).Range("D:F") '''! Change Sheets() to whichever sheet you want to use 

' Find first empty row in columns D:F on sheet Sales Book 
Do Until WorksheetFunction.CountA(rng_dest.Rows(i)) = 0 
    i = i + 1 
Loop 

'Copy range A23:D27 on sheet Invoice to Variant array 
Set rng = CurrentWB.Sheets("Invoice").Range("A23:D27") '''! 

' Copy rows containing values to sheet Sales Book 
For a = 1 To rng.Rows.Count 
If WorksheetFunction.CountA(rng.Rows(a)) <> 0 Then 
    rng_dest.Rows(i).Value = rng.Rows(a).Value 
    With WB.Sheets(1) '''! Change Sheets() to whichever sheet you want to use 
    'Copy Invoice number 
    .Range("B" & i).Value = CurrentWB.Sheets("Invoice").Range("C18").Value '''! 
    'Copy Date 
    .Range("A" & i).Value = CurrentWB.Sheets("Invoice").Range("C15").Value '''! 
    'Copy Company name 
    .Range("C" & i).Value = CurrentWB.Sheets("Invoice").Range("A7").Value '''! 
    End With '''! 
    i = i + 1 
End If 
Next a 

WB.Close savechanges:=True '''! This wil close the Workbook and save changes 

Set WB = Nothing '''! Cleaning memory 
Set CurrentWB = Nothing '''! Cleaning memory 
Application.ScreenUpdating = True 

End Sub 
+0

編輯:添加當前工作簿的聲明以避免混淆使用哪個工作簿。 – Slaqr

相關問題