2017-08-15 52 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

相关问题