【问题标题】:Macro in Excel Stops in one cellExcel中的宏在一个单元格中停止
【发布时间】:2022-01-28 03:53:55
【问题描述】:

我是 VBA 新手,遇到了一个小问题。这是我的代码:

Sub my_first_macro()
    Range("D4") = "My text Here"
    Range("I4") = "1"
    Range("D5").Select
End Sub

这个宏的想法是有一个与之关联的按钮

  • 这将在单元格 D4 中填充“我的文本”,在 I4 中填充“1”。

  • 当我第二次点击时,它会在单元格 D5 中填充“我的文本”,在 J4 中填充“1”。

  • 然后,当我第三次点击时,它会在单元格 D6 中填充“我的文本”,在 K4 中填充“1”。

  • 然后,当我第四次点击时,它会在单元格 D7 中填充“我的文本”和在 L4 中填充“1”。

这真的很简单,但我无法解决这个问题。

有什么帮助吗?

【问题讨论】:

  • always going to the next row - 下一行是什么
  • 但是您稍后再手动运行它?您是否同时移动选择?您的意思是它应该作用于当前活动行,无论您单击按钮时可能是什么?或者在最后受这个宏影响的那一行之后的那一行?
  • 然后想想你必须如何坚持你最后影响的那一行。静态变量是一种解决方案,但它在 Excel 中并不可靠,因为项目可能会被重置。存储它的隐藏表更可靠。
  • 一次,无需单独暂停和运行每一行?
  • @GSerg 我编辑了这个问题。我想现在更清楚了。

标签: excel vba


【解决方案1】:

要将当前选定单元格的值设为“1”并选择其下方的单元格,您可以这样做:

Sub my_macro()
    Selection.Value = "1"
    Selection.Offset(1, 0).Select
End Sub

【讨论】:

  • 谢谢。我想差不多就是这个了。我应该如何插入文本:“我的文本在这里”
  • @Laura I think its almost this - 给定stackoverflow.com/questions/70525025/…,其实不是。
  • @ljdyer 我编辑了这个问题。我想现在更清楚了。对不起
【解决方案2】:

同时移动行和列

  • 修改 cCellrCell 范围以查看其灵活性。
Option Explicit

Sub my_first_macro()
    
    Dim cCell As Range: Set cCell = Range("D4")
    Dim rCell As Range: Set rCell = Range("I4")
    
    Dim cfRow As Long: cfRow = cCell.Row
    Dim cCol As Long: cCol = cCell.Column
    
    Dim rRow As Long: rRow = rCell.Row
    Dim rfCol As Long: rfCol = rCell.Column
    
    Dim cLastRow As Long: cLastRow = Cells(Rows.Count, cCol).End(xlUp).Row
    Debug.Print cLastRow, cfRow
    
    If cLastRow < cfRow Then
        cCell.Value = "My text Here"
        rCell.Value = "1"
    Else
        Cells(cLastRow + 1, cCol).Value = cCell.Value
        Cells(rRow, cLastRow + rfCol - cfRow + 1).Value = rCell.Value
    End If
    
End Sub

【讨论】:

  • 我试过这个,但它更新 I4。当我第二次运行 sub 时,J4 应该是“1”
  • 当我运行这段代码时,第一次单元格D4D5My text Here,而单元格I4J41。下一次,另外,单元格D6My text Here 和单元格K41...等。如果这不是您需要的,请分享它的问题所在。顺便说一句,您在单元格D4 下方的列D 中有数据吗?
  • 我编辑了这个问题。现在更清楚了。对不起英语;。
  • 当我运行这段代码时,J4K4L4不是1。只有I41
  • 对不起,我又修改了答案。这比我想象的要棘手。
【解决方案3】:

一种可能的解决方案:我已经注释了我的代码。

Public Sub shift_text_and_numbers()

'Use lngCounter as static variable, that means it keeps its value under the following conditions
'1. if the sub is finished and called again and
'2. as long as there occurs no error in your vba-project and
'3. your workbook is open
    
    Static lngCounter As Long

'Provide your content here

    Dim strText As String: strText = "My text here"
    Dim lngNumber As Long: lngNumber = 1

'Set the first cell for each shift

    Dim rngStartCellText As Range: Set rngStartCellText = Worksheet1.Range("D4")
    Dim rngStartCellNumber As Range: Set rngStartCellNumber = Worksheet1.Range("I4")
    
'Check, whether the required rows and columns are full of data
    
    If _
        rngStartCellText.Row + lngCounter > Worksheet1.Rows.Count Or _
        rngStartCellNumber.Column + lngCounter > Worksheet1.Columns.Count _
    Then

'If the rows and columns are full of data, leave this routine

        Exit Sub
        
    End If
    
'Calculates the next cell depending on lngCounter and the first cells, represents also your sheme

    Dim rngNextCellText As Range: Set rngNextCellText = Worksheet1.Cells(rngStartCellText.Row + lngCounter, rngStartCellText.Column)
    Dim rngNextCellNumber As Range: Set rngNextCellNumber = Worksheet1.Cells(rngStartCellNumber.Row, rngStartCellNumber.Column + lngCounter)

'Check, whether there is already the required data in the next cell

    If _
        rngNextCellText.Value = strText Or _
        rngNextCellNumber.Value = lngNumber _
    Then

'Count up for the next call

        lngCounter = lngCounter + 1
 
'If there is already the data, repeat the routine

        shift_text_and_numbers

'Check, whether the next cells are already filled with data
'Avoids overwriting of data

    ElseIf _
        VBA.Len(rngNextCellText.Value) > 0 Or _
        VBA.Len(rngNextCellNumber.Value) > 0 _
    Then
    
'If the next cells contain data, leave the routine

        Exit Sub
    
    Else

'Write the content in the next cells

        rngNextCellText.Value = strText
        rngNextCellNumber.Value = lngNumber

'Count up for the next call

        lngCounter = lngCounter + 1
    
    End If
    
End Sub

感谢@VBasic2008 学习了一种声明变量可爱的新方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多