Excel VBA编程

我是一个完整的excel初学者,今天得到一个任务,明天就可以完成。 如果有人能帮助我,我会很感激。 我有一张有下表的表格:

在这里输入图像说明

第一个表是主数据,从这里我需要获取数据,并使用marco-VBA将数据表示为单独的表格。 将不胜感激任何帮助,以实现这个使用macros。谢谢。

说主表有n列,所以我需要形成n-1个独立的表,其中每个表将有2列,第一列将永远是主表的第一列,第二列将是第(n + 1)th第n个表的主表中的列。 例子 – 第一个表将有2列(主表的第一列和主表的第二列),同样第二表将有2列(主表的第一列和主表的第三列),等等等等。 ..

在接下来的一个小时左右,我会join这个答案。 这个想法是让你从早期的代码块开始,同时开发更多的块。 编辑我现在已经完成了答案,除了你可能寻求的任何额外的解释。

我同意RBarryYoung:你不提供足够的信息让任何人为你提供一个完整的解决scheme。 另外,如果您正在尝试学习VBA,那么从长远来看,给您提供解决scheme将毫无帮助。

我通常会同意djphatic:macroslogging器是非常有用的学习与用户操作相匹配的VBA,但macroslogging器不会给你很多这个任务所需的VBA。

当你显然没有准备好时,我很好奇谁给了你这个任务。

我无法读取您的图像,所以我创build了一个名为“MasterTable”的工作表,并加载了数据,如下所示:

样本数据

您的意见意味着这张桌子的大小可能会有变化,所以首要任务是确定其尺寸。 有很多不同的方法来确定一个表的尺寸; 没有一个在任何情况下工作。 我将使用UsedRange。

将以下内容复制到模块中:

Option Explicit Sub SplitTable1() Dim UsedRng As Range With Worksheets("MasterTable") Set UsedRng = .UsedRange Debug.Print UsedRng.Address Debug.Print UsedRng.Columns.Count Debug.Print UsedRng.Rows.Count End With End Sub 

没有时间对我要展示给你的所有东西给予充分的解释,但是我会尽力解释最重要的一点。

Option Explicit意味着每个variables都必须声明。 没有这个声明,拼写错误的名字会自动声明一个新的variables。

Debug.Print值输出到立即窗口,该窗口应该在VBA编辑器屏幕的底部。 如果不存在,请单击Ctrl + G

Dim UsedRng As Range声明Dim UsedRng As Range一个variablesUsedRng 。 范围是一种对象。 当你给对象赋值的时候,你必须用Set启动语句。

运行这个macros将输出以下内容到即时窗口:

 $A$1:$H$6 8 6 

我不会使用UsedRng.AddressUsedRng.Columns.Count但我希望您了解UsedRange是什么以及如何使用它。

将这个macros添加到模块中:

 Sub SplitTable2() Dim CellValue() As Variant Dim ColCrnt As Long Dim RowCrnt As Long With Worksheets("MasterTable") CellValue = .UsedRange.Value For RowCrnt = LBound(CellValue, 1) To UBound(CellValue, 1) Debug.Print "Row " & RowCrnt & ":"; For ColCrnt = LBound(CellValue, 2) To UBound(CellValue, 2) Debug.Print " " & CellValue(RowCrnt, ColCrnt); Next Debug.Print Next End With End Sub 

Dim CellValue() As Variant声明Varianttypes的dynamic数组CellValue。 ()意味着我将在运行时声明数组的大小。

CellValue = .UsedRange.Value将数组CellValue设置为CellValue = .UsedRange.Value中的值。 此语句根据需要设置CellValue的尺寸。

CellValue成为一个二维数组。 通常,数组的第一个维度是列,第二个维度是行,但是当从一个范围加载数组时,这个数组不是真。

使用一维数组, LBound(MyArray)返回数组的LBound(MyArray)UBound(MyArray)返回上界。

使用二维数组, LBound(MyArray, 1)返回数组第一维的LBound(MyArray, 2)LBound(MyArray, 2)返回第二维的下界。

该macros将以下内容输出到立即窗口。

 Row 1: Column 1 Column 2 Column 3 Column 4 Column 5 Column 6 Column 7 Column 8 Row 2: R1C1 R1C2 R1C3 R1C4 R1C5 R1C6 R1C7 R1C8 Row 3: R2C1 R2C2 R2C3 R2C4 R2C5 R2C6 R2C7 R2C8 Row 4: R3C1 R3C2 R3C3 R3C4 R3C5 R3C6 R3C7 R3C8 Row 5: R4C1 R4C2 R4C3 R4C4 R4C5 R4C6 R4C7 R4C8 Row 6: R5C1 R5C2 R5C3 R5C4 R5C5 R5C6 R5C7 R5C8 

这第二个macros演示了我可以从工作表中加载所有的值到一个数组,然后输出它们。

将这个macros添加到模块中:

 Sub SplitTable3() Dim ColourBack As Long Dim ColourFont As Long With Worksheets("MasterTable") ColourBack = .Range("A1").Interior.Color ColourFont = .Range("A1").Font.Color Debug.Print ColourBack Debug.Print ColourFont End With End Sub 

运行这个macros,它会输出:

  16711680 16777215 

对于这个答案,这些只是幻数。 16777215将字体颜色设置为白色, 16711680将背景或内部颜色设置为蓝色。

对于最后一个macros,我创build了另一个工作表“SplitTables”。

将这个macros添加到模块中:

 Sub SplitTable4() Dim CellValue() As Variant Dim ColDestCrnt As Long Dim ColourBack As Long Dim ColourFont As Long Dim ColSrcCrnt As Long Dim RowDestCrnt As Long Dim RowDestStart As Long Dim RowSrcCrnt As Long With Worksheets("MasterTable") ' Load required values from worksheet MasterTable CellValue = .UsedRange.Value With .Cells(.UsedRange.Row, .UsedRange.Column) ' Save the values from the top left cell of the used range. ' This allows for the used range being in the middle of the worksheet. ColourBack = .Interior.Color ColourFont = .Font.Color End With End With With Worksheets("SplitTables") ' Delete any existing contents of the worksheet .Cells.EntireRow.Delete ' For this macro I need different variables for the source and destination ' columns. I do not need different variables for the source and destination ' rows but I have coded the macro as though I did. This would allow the ' UsedRange in worksheet "MasterTable" to be in the middle of the worksheet ' and would allow the destination range to be anywhere within worksheet ' "SpltTables". ' Specify the first row and column of the first sub table. You will ' probably want these both to be 1 for cell A1 but I want to show that my ' code will work if you want to start in the middle of the worksheet. ColDestCrnt = 2 RowDestStart = 3 ' I use LBound when I do not need to because I like to be absolutely ' explicit about what I am doing. An array loaded from a range will ' always have lower bounds of one. For ColSrcCrnt = LBound(CellValue, 2) + 1 To UBound(CellValue, 2) ' Create one sub table from every column after the first. 'Duplicate the colours of the header row in worksheet "MasterTable" With .Cells(RowDestStart, ColDestCrnt) .Interior.Color = ColourBack .Font.Color = ColourFont End With With .Cells(RowDestStart, ColDestCrnt + 1) .Interior.Color = ColourBack .Font.Color = ColourFont End With RowDestCrnt = RowDestStart For RowSrcCrnt = LBound(CellValue, 1) To UBound(CellValue, 1) ' For each row in CellValue, copy the values from the first and current ' columns to the sub table within worksheet "SplitTables" .Cells(RowDestCrnt, ColDestCrnt).Value = _ CellValue(RowSrcCrnt, LBound(CellValue, 2)) .Cells(RowDestCrnt, ColDestCrnt + 1).Value = _ CellValue(RowSrcCrnt, ColSrcCrnt) RowDestCrnt = RowDestCrnt + 1 Next RowSrcCrnt ColDestCrnt = ColDestCrnt + 3 ' Advance to position of next sub table Next ColSrcCrnt End With End Sub 

这是真正的macros观。 所有以前的macros已经certificate了一些东西。 这个macros做我想要的东西。

回来的问题。 但是,我不知道你在什么时区,现在是23点。 我将在大约一个小时后去睡觉。 之后,问题将在明天回答。

看看Excel中的macroslogging器。 你正在寻找的实现看起来像使用VBA来执行简单的复制和粘贴在表中的特定列。 如果打开macroslogging器并通过复制并粘贴variables并估计列来生成第一个表格,然后点击停止,则可以通过查看Visual Basic编辑器(Ctrl + F11)查看生成的代码。

您可能会发现这些链接的一些使用: http : //www.automateexcel.com/2004/08/18/excel_cut_copy_paste_from_a_macro/ http://www.techrepublic.com/blog/10things/10-ways-to-reference-excel -workbooks和 – 片-使用-VBA / 967