Excel宏生成单个随机输出

时间:2013-12-20 15:16:41

标签: excel vba excel-vba

我录制了一个宏,它贯穿了一堆格式化和拼接的数据。它基本上格式化数据页面,然后将其拼接数月。数据大致如下:

Column A    ...  Column D ... Column H    Column I    Jan    Feb ...  Nov     Dec
   -              00645           -        budget    45.2    32.8    -15.0   100.00

基本上它填写所有A列的条目(所有条目都是“LINE”),重命名B,C,D,H和I列的标题,然后复制列AJ并将其移动到专为此创建的选项卡特定月份,然后删除列J,以便下个月始终在列J中并继续重复,直到解析出所有月份。它是一个非常简单的宏,大多是录制的,但它表现得很奇怪。

问题是,一旦它运行,它会将应该在A列(“LINE”)中的值随机输入到文件中其他位置的单个单元格。它似乎是半 - 随机哪个单元格。如果您从文件A中输入数据,无论您运行多少次宏,“LINE”都会显示在同一个单元格中。但它将是文件B,C,D等的不同单元格。与其他单元(相同的条目,相同的格式等)相比,它在执行此操作的单元格方面基本上没有什么不同。我不知道为什么这样做。以下是我的代码:

Sub Format_Upload()

Format_Upload Macro



ActiveCell.FormulaR1C1 = "LINE"
LastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("A2").Copy Range("A2").Resize(LastRow - 1)
Range("B1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Dept ID"
Range("C1").Select
ActiveCell.FormulaR1C1 = "MSUPLOAD1"
Range("D1").Select
ActiveCell.FormulaR1C1 = "01/01/14"
Range("I1").Select
ActiveCell.FormulaR1C1 = "01/01/14"
Range("D1").Select
Selection.NumberFormat = "mm/dd/yy;@"
Range("I1").Select
Selection.NumberFormat = "mm/dd/yy;@"
Cells.Select
With Selection
    .HorizontalAlignment = xlRight
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
Columns("J:U").Select
With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
With Selection
    .HorizontalAlignment = xlRight
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Selection.NumberFormat = "0.00"
Columns("A:J").Select
Selection.Copy
Sheets("JAN").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Data").Select
Columns("J:J").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("FEB").Select
Range("A1").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "2/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "2/1/2014"
Range("H1").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("MAR").Select
Range("A1").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "3/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "3/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("APR").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "4/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "4/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Range("J1").Activate
Selection.Copy
Sheets("MAY").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "5/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "5/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("JUN").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "6/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "6/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("JUL").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "7/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "7/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("AUG").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "8/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "8/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Copy
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("SEP").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "9/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "9/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Selection.Copy
Sheets("OCT").Select
Sheets("Data").Select
Columns("A:J").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("OCT").Select
Range("A1").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "10/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "10/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("NOV").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "11/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "11/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("A:J").Select
Selection.Copy
Sheets("DEC").Select
ActiveSheet.Paste
Columns("D:D").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "12/1/2014"
Range("I1").Select
ActiveCell.FormulaR1C1 = "12/1/2014"
Range("I2").Select
Sheets("Data").Select
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft

Sheets("FEB").Select
ActiveCell.FormulaR1C1 = "MSFEB"
Sheets("MAR").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSMAR"
Sheets("APR").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSAPR"
Sheets("MAY").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSMAY"
Sheets("JUN").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSJUN"
Sheets("JUL").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSJUL"
Sheets("AUG").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSAUG"
Sheets("SEP").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSSEP"
Sheets("OCT").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSOCT"
Sheets("NOV").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSNOV"
Sheets("DEC").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "MSDEC"
Range("H2").Select

End Sub

我在代码中没有看到解释此异常的任何内容。关于是什么导致这个问题的任何想法? “

编辑:奇怪的单元格总是出现在第一个标签上(名为“JAN”),但其位置会有所不同。

1 个答案:

答案 0 :(得分:2)

ActiveCell.FormulaR1C1 = "LINE"

这是在运行之前将“LINE”放在您选择的任何一个单元格中。

将其更改为其他内容