将数据分配给单个选项卡并使用variables来跟踪VBA Excel

我会尽量保持简短,并使用实际的数据/代码,以便更好地了解我正在努力完成的任务。

我想要做的是:把一个大的数据集分成几个工人(他们的日常任务)。 这涉及到创build标签(完成),让pipe理员向每个用户input行数(macros中只有一个时间variables是我现在正在做的),然后让Excel跟踪我们的数字,所以我不必手动硬编码要复制/粘贴的单元格。

旧版:

Sheets("PropFiltered").Select Range("A1:J1250").Select 'Notice the hard code cell range. I want this to be done automatically. Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy Sheets("April").Select 'Do not want hard code named sheets. Would like it to grab from Employee1 and so on variables. Range("A1").Select ActiveSheet.Paste Call WrapText Sheets("AutoFiltered").Select Range("A1:J1250").Select Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy Sheets("Christie E.").Select Range("A1").Select ActiveSheet.Paste Call WrapText Sheets("AutoFiltered").Select Range("A1251:J2251").Select Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy Sheets("Cori").Select Range("A1").Select ActiveSheet.Paste Call WrapText Sheets("PropFiltered").Select Range("A1251:J1501").Select Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy Sheets("Cori").Select Range("A1002").Select 'When a user has both Auto and Property, this has to account for the first "paste" and I would like this to be done automatically. ActiveSheet.Paste Call WrapText 

新版本(WIP):

 Sub FillTabs(TabName As String, NumberOfAutoClaims As Integer, NumberOfPropertyClaims As Integer) 'Does it make sense to use a separate sub if I have to keep track of a changing variable (The "what cell should I copy because I've already done the first 1250" variable) Dim WhatNumberAuto As Integer Dim WhatNumberProp As Integer Dim WhatNumberAutoAdjusted As Integer Dim SettingTheACellAuto As String Dim SettingTheJCellAuto As String Dim SettingTheACellProp As String Dim SettingTheJCellProp As String Dim SettingThePasteCellAuto As String Dim SettingThePasteCellProp Dim Employee1 As String Dim Employee2 As String Dim Employee3 As String Dim Employee4 As String Employee1 = "April H.,0,1000" 'I have over 20 employees to add in with 3 variables each, is this the best method? I need to use each variable (separated by comma) Employee2 = "Christie E.,500,0" Employee3 = "Cori M.,1000,250" Employee4 = "Cody S.,1000,250" Split(Employee1,",") 'This is an example of splitting the employee1 variable to make the three pieces of data useable. Is there a better method? WhatNumberAuto = 1 'This is to keep track of "What number should I copy now" but is it even possible to have this change if I run this sub separately for each employee? WhatNumberProp = 1 SettingTheACellAuto = "A" & WhatNumberAuto SettingTheJCellAuto = "J" & NumberOfAutoClaims SettingTheACellProp = "A" & WhatNumberProp SettingTheJCellProp = "J" & NumberOfPropertyClaims 'Sheets(TabName).Select If NumberOfAutoClaims > 0 Then WhatNumberAutoAdjusted = WhatNumberAuto + NumberOfAutoClaims 'SettingThePasteCellAuto = "A" & (NumberOfAutoClaims + 1) Sheets("AutoFiltered").Select Range("SettingTheACellAuto:SettingTheJCellAuto").Select Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy Sheets(TabName).Select Range("A1").Select ActiveSheet.Paste Call WrapText Else If NumberOfAutoClaims = 0 And NumberOfPropertyClaims = 0 Then Debug.Print "This user has no Auto or Property claims assigned" End If If NumberOfPropertyClaims = 0 Then End Sub SettingThePasteCellProp = "A" & (NumberOfAutoClaims + 1) Sheets("PropFiltered").Select Range("SettingTheACellProp:SettingTheJCellProp").Select Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy Sheets(TabName).Select Range(SettingThePasteCellProp).Select ActiveSheet.Paste Call WrapText End If 

最新版本:

 HowManyTabsDoYouNeed = 21 'If you want to add or remove Tabs, you must change this number AND add/subtract from the "TabName(1)" section below. ReDim TabName(1 To HowManyTabsDoYouNeed) As String TabName(1) = "Heather,0,0" TabName(2) = "Paul,0,0" TabName(3) = "CICS,0,0" TabName(4) = "FLPIP,0,0" TabName(5) = "April H.,0,1000" TabName(6) = "Christopher H.,0,1000" ......skipping bits of code 'Begin populating employee's sheets. Dim AutoACount As Integer Dim PropACount As Integer Dim AutoAPasteCount As Integer Dim PropAPasteCount As Integer Dim AutoJCount As Integer Dim PropJCount As Integer Dim AutoRangeA As Range Dim AutoRangeJ As Range Dim PropRangeA As Range Dim PropRangeJ As Range Dim PropAPasteCountRange As Range AutoACount = 1 PropACount = 1 AutoJCount = 1 PropJCount = 1 PropAPasteCount = 1 For i = 1 To HowManyTabsDoYouNeed SplitTabName = Split(TabName(i), ",") If SplitTabName(1) <> "0" Then Set AutoRangeA = Range("A" & AutoACount) Set AutoRangeJ = Range("J" & SplitTabName(1)) Sheets("AutoFiltered").Select Range(AutoRangeA & ":" & AutoRangeJ).Select Selection.Copy Sheets("SplitTabName(0)").Select ActiveSheet.Paste AutoACount = AutoACount + SplitTabName(1) PropAPasteCount = SplitTabName(1) End If If SplitTabName(2) <> "0" Then Set PropRangeA = Range("A" & PropACount) 'MsgBox PropRangeA Set PropRangeJ = Range("J" & SplitTabName(2)) Set PropAPasteCountRange = Range("A" & PropAPasteCount) ' With Sheets("PropFiltered").Range("PropRangeA:PropRangeJ") ' .Copy Sheets("PropFiltered").Select Range(PropRangeA & ":" & PropRangeJ).Select Selection.Copy Sheets("SplitTabName(0)").Select Range(PropAPasteCountRange).Select ActiveSheet.Paste PropACount = PropACount + SplitTabName(2) End If Next i