简体   繁体   English

需要帮助缩短此 excel 2013 宏

[英]Need help shortening this excel 2013 macro

This isn't complex by far but I'm only a novice at excel macros.到目前为止这并不复杂,但我只是 excel 宏的新手。 I've found online and edited this for my use but I know it's so long.我在网上找到并编辑了这个供我使用,但我知道它太长了。 The single ranges all refer to the same cell which is just the value of =today() .单个范围都引用同一个单元格,它只是=today()的值。 I know that can be integrated, I just don't know how.我知道可以集成,我只是不知道如何集成。 The rest copies a row and pastes it over at the bottom of specific rows, one for each employee.其余的复制一行并将其粘贴到特定行的底部,每个员工一个。 I'm sure there are even better ways to do this since the rows being copied are only there for this code and isn't the main data source.我确信有更好的方法可以做到这一点,因为被复制的行仅用于此代码,而不是主要数据源。 But one step at a time.但是一步一个脚印。 Lol哈哈

Sub LastRowDtDataTEST()
Dim wb As Workbook
Dim ws As Worksheet
Dim LastRow As Long
Set wb = ActiveWorkbook
Set ws = ThisWorkbook.Sheets("Buyer Trend Metrics")
ws.Select

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
Range("J" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B61:H61").Copy
LastRow = Cells(Rows.Count, "K").End(xlUp).Row ' get last row with data in column "K"
Range("K" & LastRow + 1).PasteSpecial Paste:=xlPasteValues ' paste values

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "S").End(xlUp).Row
Range("S" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B62:H62").Copy
LastRow = Cells(Rows.Count, "T").End(xlUp).Row
Range("T" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "AB").End(xlUp).Row
Range("AB" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B63:H63").Copy
LastRow = Cells(Rows.Count, "AC").End(xlUp).Row
Range("AC" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "AK").End(xlUp).Row
Range("AK" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B64:H64").Copy
LastRow = Cells(Rows.Count, "AL").End(xlUp).Row
Range("AL" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "AT").End(xlUp).Row
Range("AT" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B65:H65").Copy
LastRow = Cells(Rows.Count, "AU").End(xlUp).Row
Range("AU" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "BC").End(xlUp).Row
Range("BC" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B66:H66").Copy
LastRow = Cells(Rows.Count, "BD").End(xlUp).Row
Range("BD" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "BL").End(xlUp).Row
Range("BL" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B67:H67").Copy
LastRow = Cells(Rows.Count, "BM").End(xlUp).Row
Range("BM" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "BU").End(xlUp).Row
Range("BU" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B68:H68").Copy
LastRow = Cells(Rows.Count, "BV").End(xlUp).Row
Range("BV" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "CD").End(xlUp).Row
Range("CD" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B69:H69").Copy
LastRow = Cells(Rows.Count, "CE").End(xlUp).Row
Range("CE" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B58").Copy  ' Copies the value of =Today() from a cell. Would be easier to just integrate today's date instead.
LastRow = Cells(Rows.Count, "CM").End(xlUp).Row
Range("CM" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

Range("B70:H70").Copy
LastRow = Cells(Rows.Count, "CN").End(xlUp).Row
Range("CN" & LastRow + 1).PasteSpecial Paste:=xlPasteValues

End Sub

Here are some things for you to look at...这里有一些东西供您查看...

  1. ALWAYS use Option Explicit .始终使用Option Explicit See here for an explanation.请参阅此处以获取解释。
  2. When you're performing an action such as copying data, it's extremely helpful to be very clear in defining the source and destination of the data.当您执行诸如复制数据之类的操作时,非常清楚地定义数据的来源和目的地非常有帮助。 This includes defining which Workbook the data is going to.这包括定义数据将转到哪个Workbook You'll thank me later for building this habit now.稍后你会感谢我现在养成了这个习惯。

As an example:举个例子:

Dim srcWB As Workbook
Dim dstWB As Workbook
Set srcWB = ThisWorkbook
Set dstWB = ThisWorkbook

Dim srcWS As Worksheet
Dim dstWS As Worksheet
Set srcWS = srcWB.Sheets("Sheet1") ' <--- you didn't specify this in your code
Set dstWS = dstWB.Sheets("Buyer Trend Metrics")
  1. When you're performing the same (or very similar) actions over and over, it's the perfect situation to create a separate function that will perform the action for you.当您一遍又一遍地执行相同(或非常相似)的操作时,创建一个单独的函数来为您执行操作是完美的情况。 When you break out this section of code, it's called "functional isolation".当您将这部分代码分解时,它被称为“功能隔离”。 This means that if you have a problem to fix, you only have to fix it in one place instead of finding all the different spots in your code that do the same thing.这意味着,如果您有问题需要修复,您只需在一个地方修复它,而不是在代码中寻找所有不同的地方来做同样的事情。

In your case, you are performing a copy from one range of cells to another range of cells.在您的情况下,您正在执行从一个单元格范围到另一个单元格范围的复制。 So breaking that out into a separate routine looks like this:因此,将其分解成一个单独的例程如下所示:

Private Sub CopyMyData(ByRef fromData As Range, ByRef toData As Range)
    Dim lastrow As Long
    With toData.Parent
        lastrow = .Cells(.Rows.Count, toData.Column).End(xlUp).Row
    End With

    fromData.Copy
    toData.Cells(lastrow).PasteSpecial Paste:=xlPasteValues
End Sub

Notice here how I'm using variable names that describe what the code does ( fromData and toData ).注意这里我是如何使用变量名来描述代码的作用的( fromDatatoData )。 This makes it clear what's happening.这清楚地表明发生了什么。

Put it all together and your code will look something like this:将它们放在一起,您的代码将如下所示:

Option Explicit

Public Sub StartCopying()
    Dim srcWB As Workbook
    Dim dstWB As Workbook
    Set srcWB = ThisWorkbook
    Set dstWB = ThisWorkbook

    Dim srcWS As Worksheet
    Dim dstWS As Worksheet
    Set srcWS = srcWB.Sheets("Sheet1") ' <--- you didn't specify this in your code
    Set dstWS = dstWB.Sheets("Buyer Trend Metrics")

    CopyMyData fromData:=srcWS.Range("B58"), toData:=dstWS.Range("J:J")

    CopyMyData fromData:=srcWS.Range("B61:H61"), toData:=dstWS.Range("K:K")

    CopyMyData fromData:=srcWS.Range("B58"), toData:=dstWS.Range("S:S")

    CopyMyData fromData:=srcWS.Range("B61:H62"), toData:=dstWS.Range("T:T")
End Sub

Private Sub CopyMyData(ByRef fromData As Range, ByRef toData As Range)
    Dim lastrow As Long
    With toData.Parent
        lastrow = .Cells(.Rows.Count, toData.Column).End(xlUp).Row
    End With

    fromData.Copy
    toData.Cells(lastrow).PasteSpecial Paste:=xlPasteValues
End Sub
  1. Do not double space every single line .不要每行加倍空格。 You should use these as strategic separators, not the standard.您应该将这些用作战略分隔符,而不是标准。 This isn't MLA.这不是 MLA。
  2. Use a worksheet variable to quickly refer to your sheets ( ws refers to the sheet that has the cells to be copied and ds (destination sheet) refers to the sheet where the cells are to be pasted使用工作表变量快速引用您的工作表( ws指的是具有要复制的单元格的工作表, ds (目标工作表)指的是要粘贴单元格的工作表
  3. You can use a value transfer instead of a copy/paste which does not require multiple lines as well您可以使用价值转移而不是不需要多行的复制/粘贴

In general, when shortening code, you want to look for repetitiveness.通常,在缩短代码时,您希望寻找重复性。 I can see that you are constantly copying the value from Range("B58") so you can also shorten this.我可以看到您不断地从Range("B58")复制值,因此您也可以缩短它。 You have comments saying you want the value to just be today so you can just do something like您有评论说您希望价值只是今天,所以您可以做类似的事情

ds.Range("?") = Today Repeat as needed ds.Range("?") = Today根据需要重复


Option Explicit

Sub LastRowDtData()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim ds As Worksheet: Set ds = ThisWorkbook.Sheets("Buyer Trend Metrics")
Dim LR As Long

LR = ds.Range("J" & ds.Rows.Count).End(xlUp).Offset(1)
ds.Range("J" & LR).Value = ws.Range("B58").Value

LR = ds.Range("K" & ds.Rows.Count).End(xlUp).Offset(1)
ds.Range("K" & LR).Resize(1, 7).Value = ws.Range("B61:H61")

LR = ds.Range("S" & ds.Rows.Count).End(xlUp).Offset(1)
ds.Range("S" & LR).Value = ws.Range("B58").Value


'Repeat for below ranges
'------------------
Range("B62:H62").Copy
Range("B58").Copy
Range("B63:H63").Copy
Range("B58").Copy
Range("B64:H64").Copy
Range("B58").Copy
Range("B65:H65").Copy
Range("B58").Copy
Range("B66:H66").Copy
Range("B58").Copy
Range("B67:H67").Copy
Range("B58").Copy
Range("B68:H68").Copy
Range("B58").Copy
Range("B69:H69").Copy
Range("B58").Copy
Range("B70:H70").Copy


End Sub

There's a pattern to how you're copying/pasting.您复制/粘贴的方式有一种模式。

Copying every row, pasting to every 9th column after column 10.复制每一行,粘贴到第 10 列之后的每 9 列。

I've added two lines for finding the last row - either find it once and paste everything to that row, of find it before you copy each time.我添加了两行来查找最后一行 - 要么找到它一次并将所有内容粘贴到该行,要么在每次复制之前找到它。 Uncomment whichever you prefer.取消注释您喜欢的任何一个。

This will copy B61:H61 to K:P on the last row (with date in J ), then B62:H62 to T:Z with the date in R .这会将最后一行的B61:H61复制到K:P (日期在J中),然后将B62:H62复制到T:Z ,日期在R中。

The date will also appear correctly formatted rather than as a number.日期也将正确显示格式而不是数字。

Public Sub WhateverYouWantToCallIt()

    Dim x As Long, y As Long
    Dim lLastRow As Long

    With ThisWorkbook.Worksheets("Buyer Trend Metrics")

        'This will set the same last row for each copy.
        lLastRow = .Cells(.Rows.Count, 10).End(xlUp).Row + 1

        y = 10
        For x = 61 To 70

            'This will set the last row on each set of data.
            'lLastRow = .Cells(.Rows.Count, y).End(xlUp).Row + 1

            .Cells(lLastRow, y) = Date

            .Range(.Cells(lLastRow, y + 1), .Cells(lLastRow, y + 7)) = _
                .Range(.Cells(x, 2), .Cells(x, 8)).Value

            '-OR-
            '.Range(.Cells(x, 2), .Cells(x, 8)).Copy
            '.Cells(lLastRow, y + 1).PasteSpecial Paste:=xlPasteValues

            y = y + 9
        Next x
    End With

End Sub

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

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