简体   繁体   English

从一张纸上获取结果,然后将其移动到其他许多张纸上

[英]Take results from one sheet and move them into many other sheets

I have looked at similar answers to this question, but whatever I do I cannot get them to do what I need. 我已经对这个问题进行了类似的回答,但是无论我做什么,我都无法让他们做我需要的事情。

I have a daily email which has a CSV file giving call stats for our Sales team for the previous day. 我每天都有一封电子邮件,其中包含一个CSV文件,其中提供了我们销售团队前一天的通话统计信息。 What I need is to put them into Excel to give trending and historical call activity for the year. 我需要将它们放入Excel中以提供当年的趋势和历史通话活动。 Without VBA or Macros this is a very time consuming process. 没有VBA或宏,这是一个非常耗时的过程。

The stats it gives are number of calls, and average call length (that are of any importance) I have already got VBA to calculate the total outgoing with this: 它提供的统计信息是呼叫次数和平均呼叫时长(这很重要),我已经有了VBA来计算总传出量:

Dim Call_Number As Integer
Dim Call_Time As Date
Dim Call_Total As Date

Call_Number = .Cells(2, 6).Value
Call_Time = .Cells(2, 7).Value

Call_Total = Call_Number * Call_Time
.Cells(12, 7).Value = Call_Total

So what I need is to take the 3 cells for each sales member, and move them into the right place in their relative sheets, which are separated by name. 因此,我需要为每个销售成员提取3个单元格,然后将其移至相对表中正确的位置,并按名称将其分隔开。 I also need it to move into the next cell to the right if the destination cell is full, so I'm thinking I need to start the pasting process as Jan 1st and keep moving to the right until it finds blank cells. 如果目标单元格已满,我还需要它移至右侧的下一个单元格,因此我想我需要从1月1日开始粘贴过程,并继续向右移动,直到找到空白单元格。 Is there a way this can be done either in a button or automatically? 有没有一种方法可以通过按钮或自动完成?

I have the first sheet used as the data import sheet, where we just import the data into csv, and because its standard formatting, every day it will give it all in the right formatting. 我将第一个工作表用作数据导入工作表,在这里我们只是将数据导入到csv中,并且由于它是标准格式,因此每天都会以正确的格式给出所有信息。

Code I have so far. 到目前为止的代码。 It doesn't error, but doesn't do anything: 它没有错误,但是不做任何事情:

Sub Move_Data()

Dim Dean As Worksheet
Dim Chris As Worksheet
Dim Paul As Worksheet
Dim Nigel As Worksheet
Dim Calc As Worksheet
Dim Lastrow As Long
Dim J As Long
Dim i As Long


Set Dean = ThisWorkbook.Worksheets("DEAN 822")
Set Chris = ThisWorkbook.Worksheets("CHRIS 829")
Set Paul = ThisWorkbook.Worksheets("PAULP 830")
Set Nigel = ThisWorkbook.Worksheets("NIGEL 833")
Set RUSSELL = ThisWorkbook.Worksheets("RUSSELL 835")
Set Calc = ThisWorkbook.Worksheets("Calculation Sheet")

Lastrow = Range("C" & Dean.Columns.Count).End(xlToRight).Column

J = 2

For i = 0 To Lastrow

Set Rng = Dean.Range("C5").Offset(i, 0)
If Not (IsNull(Rng) Or IsEmpty(Rng)) Then
Calc.Cells(2, 4).Copy
Dean.Range("c" & J).PasteSpecial xlPasteValues
J = J + 1
End If
Next i
Application.CutCopyMode = False


End Sub

Instead of 代替

Lastrow = Range("C" & Dean.Columns.Count).End(xlToRight).Column

I think you want 我想你要

Lastrow = Range("C" & Dean.Columns.Count).End(xlUp).Row

"I also need ... in a button or automatically?" “我还需要……一个按钮还是自动?”

LastCol = WshtName.Cells(CrntRow, Columns.Count).End(xlToLeft).Column

will set LastCol to the last used column in row CrntRow. 会将LastCol设置为LastCol行中最后使用的列。


J = 2

For i = 0 To Lastrow

  Set Rng = Dean.Range("C5").Offset(i, 0)
  If Not (IsNull(Rng) Or IsEmpty(Rng)) Then
    Calc.Cells(2, 4).Copy
    Dean.Range("c" & J).PasteSpecial xlPasteValues
    J = J + 1
  End If
Next i
Application.CutCopyMode = False

I am not sure what this code is attempting. 我不确定此代码正在尝试什么。

It sets Rng to C5, C6, C7, C8, ... to Cn where n is Lastrow+5. 它将Rng设置为C5,C6,C7,C8,...到Cn,其中n为Lastrow + 5。 If C5, for example, if empty it copies C2 to `Calc.Cells(2, 4). 例如,如果C5为空,则将C2复制到Calc.Cells(2,4)。

Did you mean to copy column C from worksheet Dean to column B of worksheet Calc? 您是否要将C列从工作表Dean复制到工作表Calc的B列?

If the removal of empty cells is not important then this will be faster and clearer: 如果清除空单元格并不重要,那么它将更快更清晰:

Set Rng = Dean.Range(.Cells(5 ,"C"), .Cells(Lastrow ,"C"))
Rng.Copy Destination:=Calc.Cells(2, 4)

New information in response to comment 回应评论的新信息

I cannot visualise either your source data or your destination data from your description so cannot give any specific advice. 我无法根据您的描述显示您的源数据或目标数据,因此无法提供任何具体建议。

Welcome to Stack Overflow. 欢迎使用堆栈溢出。 I believe this is a good place to find previously posted information and a good place to post new questions but you must follow the site rules. 我相信这是查找以前发布的信息的好地方,也是发布新问题的好地方,但是您必须遵循网站规则。

Right of centre in the top bar is the Help button. 顶部栏中中心的右边是“帮助”按钮。 Click this and read how to use this site. 单击此并阅读如何使用此站点。 Learn how to post a question that will be classified as a good question and will be answered quickly and helpfully. 了解如何发布将被归类为好问题并能快速,有效地回答的问题。

I believe the biggest three problems with your question are: 我相信您的问题最大的三个问题是:

  • You ask too much. 你问太多了 You can ask as many good questions as you wish but there should only be one issue per question. 您可以根据自己的喜好提出许多问题,但每个问题只能有一个问题。
  • You ask for information that is already available. 您要求提供的信息。
  • You are too vague about your requirement to permit anyone to help. 您对于允许任何人提供帮助的要求过于含糊。 You say you want to move three values per staff member. 您说要移动每个职员三个值。 But you do not show how either the worksheet “Calculation Sheet” or the staff member worksheets are arranged. 但是您没有显示工作表“计算表”或工作人员工作表的排列方式。 You cannot post images until you have a higher reputation but you can use the code facility to create “drawings” of the worksheets. 在享有较高声誉之前,您不能发布图像,但可以使用代码工具来创建工作表的“工程图”。

To avoid asking too much, you must break your requirement into small steps. 为了避免提出过多要求,您必须将需求分为几个小步骤。 The following is my attempt to identify the necessary small steps based on my guess of what you seek. 以下是我根据您对所寻找内容的猜测来确定必要的小步骤的尝试。

The CSV files containing staff detail arrive as attachments to a daily email. 包含员工详细信息的CSV文件作为日常电子邮件的附件到达。 Are you manually saving those attachment? 您是否手动保存这些附件? An Outlook VBA macro to save an attachment would not be difficult to write. 用于保存附件的Outlook VBA宏将不难编写。 I suggest you leave this for later but if you search Stack Overflow for “[outlook-vba] Save attachment” you will find relevant code. 我建议您将其留待以后使用,但是如果您在Stack Overflow中搜索“ [outlook-vba]保存附件”,则会找到相关的代码。

The above shows how I search Stack Overflow. 上面显示了我如何搜索堆栈溢出。 I start with the tag for the language and follow it with some key words or a key phrase. 我从语言标签开始,然后加上一些关键词或关键词。 Sometimes it takes me a few goes to get the right search term but I rarely fail to find something interesting 有时,我需要花一些时间才能找到正确的搜索词,但我很少能找到有趣的东西

How are you importing the CSV to Excel? 您如何将CSV导入Excel? Are you doing this manually? 您是手动执行此操作吗? There are many possible VBA approaches. 有许多可能的VBA方法。 Try searching for “[excel-vba] xxxx” where xxxx describes your preferred approach. 尝试搜索“ [excel-vba] xxxx”,其中xxxx描述您的首选方法。

I assume the structure of the CSV file is pretty simple and there is no difficulty in find information in the individual rows. 我认为CSV文件的结构非常简单,并且在各个行中查找信息没有困难。 You appear to know the easiest technique for finding the last row so you should have no difficulty in creating a loop that works down the rows. 您似乎知道找到最后一行的最简单方法,因此您无需费力即可创建一个循环处理各行。

How do you relate the staff member's name in the CSV file with the name of their worksheet? 您如何将CSV文件中工作人员的姓名与工作表的姓名相关联? In your question you have worksheet names such as "DEAN 822", "CHRIS 829" and "PAULP 830". 在您的问题中,您具有工作表名称,例如“ DEAN 822”,“ CHRIS 829”和“ PAULP 830”。 Are these the names used in the CSV file? 这些是CSV文件中使用的名称吗? What happens when a new staff member joins? 新员工加入后会怎样? I doubt this happens very often but you do not want to be amending your macro when it does happen. 我怀疑这种情况经常发生,但是您不希望在发生这种情况时修改宏。

I do not understand your requirement for the new data to be added to the right of any existing data. 我不理解您对将新数据添加到任何现有数据右侧的要求。 There will be three values per day so with around 200 working days per year that gives 600 columns. 每天将有三个值,因此每年大约有200个工作日,即600列。 To me that sees an awkward arrangement. 对我来说,这很尴尬。 I would have thought one row per day would have been more convenient. 我本来以为每天一排会更方便。

How will you run the macro? 您将如何运行宏? You mention a button or automatically. 您提到一个按钮或自动。 I do not like buttons since I find the tool bars cluttered enough already. 我不喜欢按钮,因为我发现工具栏已经足够混乱了。 I prefer to use shortcut keys such as Ctrl + q . 我更喜欢使用快捷键,例如Ctrl + q I rarely have more than one macro per workbook of this type so that works well for me. 每个这种工作簿我很少有一个以上的宏,因此对我来说效果很好。 By automatically, I assume you mean the macro will run automatically when the workbook is open. 自动,我假设您的意思是打开工作簿时宏将自动运行。 I would start with the shortcut key but when you are ready look up “Events” and “Event routines”. 我将从快捷键开始,但是当您准备就绪时,请查找“事件”和“事件例程”。 You will find an explanation of how you can have a macro start automatically when the workbook opens. 您将找到有关如何在工作簿打开时自动启动宏的说明。

I hope the above is of some help. 希望以上内容对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM