【问题标题】:Using VBA If then statement to copy and paste data使用 VBA If then 语句复制和粘贴数据
【发布时间】:2016-07-19 16:29:41
【问题描述】:

我是一个全新的 VBA 用户,尝试根据日期范围复制和粘贴数据。在第一列我有日期,在第二列我有我想复制和粘贴的数据。 CurYear 是指我正在寻找的范围内的结束日期,而 StatDate 是指我正在寻找的范围内的开始日期。当我运行此代码时,它会使 Excel 崩溃。请帮助我很迷茫

Worksheets("Weekly").Select

Dim nRows As Integer
Dim CurYear As Date
Dim StartDate As Date

nRows=Range("A1").CurrentRegions.Count.Rows
CurYear=Range("I265").Value
StartDate=Range("M5").Value

Do While Cells(nRows,1)<>""

if Cells(nRows,1).Value< CurYear & Cells(nRows,1)> StartDate Then

Cells(nRows,1).Offset(0,1).Copy
Worksheets("Weekly").Range("H41").Paste

Loop
End If

【问题讨论】:

  • 我只要让 Cells(nRows,1).Offset(0,1).value = Worksheets("Weekly").Range("H41").value,不需要复制粘贴
  • 另外,结束 if 应该在循环之前,我得到 currentregions.count.rows 的错误。我可能会通过将变量设置为 A1 并将另一个变量设置为最后使用的单元格来创建适当的范围 (Worksheets("Sheet1").Activate ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Address)
  • 在调试器中按F8,哪一行崩溃了?
  • Lowpar 我对如何在 do while 循环中调用单元格范围感到有些困惑。这就是我所拥有的,我应该如何填写空白 CurYear = Range("I265").Value StartDate = Range("M5").Value x = Range("A1") y = Range("A1").End (xlDown) 在单元格中做_____ ""
  • @ScottCraner - 或者可能是nRows = nRows - 1? (从nRows=Range("A1").CurrentRegions.Count.Rows 有点难以分辨,但如果它是nRows=Range("A1").CurrentRegion.Rows.Count,那么 nRows 从最后开始,所以需要向下工作。)

标签: vba excel


【解决方案1】:

将“选项显式”放在代码顶部(子代码之前),它会告诉您要修复的问题。这样做将修复您的错误部分,即您的 end if 在循环之外而不是在循环内部,但它不会发现您没有更改循环计数器。试试这个代码。它实际上与您所做的一些小改动几乎相同。

Option Explicit
Sub test()
Dim sht As Worksheet, i As Long, l As Long, j

Dim nRows As Integer
Dim CurYear As Date
Dim StartDate As Date

Set sht = Worksheets("Test1") ' set the sheet as object isntead of selecting it for faster code and avoiding other issues

nRows = Cells(sht.Rows.Count, "B").End(xlUp).Row 'Last used row in column B - current region lastrow gets twitchy in some circumstances and should be avoided unless there is a reason to use it
l = 41

CurYear = range("I265").Value
StartDate = range("M5").Value

For i = 1 To nRows
  If Cells(i, 1).Value < CurYear And Cells(i, 1).Value > StartDate Then 'for If statements you use "and" not "&"
  Cells(l, 15) = Cells(i, 2) 'you will want something like this line and the next if you don't want to overwrite H41 if there is more than one match
  l = l + 1
  End If
Next i

End Sub

另外,为了帮助调试,打开本地窗口(在 VBE 中查看)。使用 F8 单步执行您的代码,在 locals 窗口中查看您的变量,以确保它们是您在脚本中该步骤所期望的。

如果您对代码执行此操作,您会发现您的循环变量缺少计数器更改。所以它一直在寻找 nRow 最终成为“”,但它保持在它设置的任何位置。无限循环。我将其更改为 for next 格式,但 6 of 1 和半打另一种用于您的代码。

欢迎使用 VBA。不要戳你的眼睛。 :-)

【讨论】:

  • 仅供参考 - 如果您对代码的其他部分有疑问,并且无法使用 F8 单步执行并查看本地窗口进行排序,请务必搜索新答案并在适当的情况下提出新问题.
  • 罗杰,我还是有点麻烦。我不再崩溃 excel,这太棒了!但是,上面的代码不是复制和粘贴值。在我运行代码“H41”后保持空白。我认为我的日期可能有错误,但是我的所有日​​期都输入正确。 PS 我没有更改上面代码中的任何内容,除了它也引用了哪个表
  • 我编辑了上面的代码并添加了 cmets。试一试。哦,如果您想要 H 列而不是 O 列,则单元格 (l,15) 行将需要将 15 更改为 8。我错过了将其更改回来。如果这对您有所帮助,请不要忘记按向上箭头的“接受答案”。 :)
  • 谢谢!在这短短的时间内,我学会了一个关于 vba 的按钮以及如何使用它。谢谢!
【解决方案2】:

与其使用使用大量内存并使程序运行缓慢的复制/粘贴,您可能需要考虑以下代码,它与您的代码或罗杰的代码具有相同的目的,但比使用Select 和复制/粘贴语法。

Sub Test()
Dim nRows As Long, LastRow As Long   'Declare as Long instead of Integer to avoid overflow
Dim CurYear As Date, StartDate As Date

LastRow = Cells(Rows.Count, 1).End(xlUp).Row 'Count the last used row in column 1 where you put the first data (dates)
nRows = 2    'Set the starting point of row where you put the first data (dates). In this example I use 2
CurYear = Range("I265").Value
StartDate = Range("M5").Value

Do
    If Cells(nRows, 1).Value < CurYear And Cells(nRows, 1) > StartDate Then 'Use And not &
        Cells(nRows, 5).Value = Cells(nRows, 2).Value   'This is essentially a "copy/ paste" syntax. Change the value (5) to the column you want to paste the value in column 2
    End If
    nRows = nRows + 1   'Set an increment value so each looping the nRows will increase by 1
Loop Until nRows = LastRow + 1   'Added by 1 so that the data in LastRow will keep being processed

End Sub

【讨论】:

  • 这太棒了!谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-04-08
  • 2016-01-10
  • 1970-01-01
  • 2017-07-26
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多