尝试调整宏以转到下一个空单元格,而不是最后一个打开的列单元格。

huangapple go评论59阅读模式
英文:

Trying to adjust a macro to go to next open cell, not last cell in column that's open

问题

I currently send a cells value to another spreadsheet to the next open cell in a column. But my current code sends it to the bottom most cell after all the values, so if I delete any cells in between it still only posts at the end of the data. I would like it to fill in cells I have deleted so it's not leaving empty rows. Any help is greatly appreciated.

Sub S2WL()
     Dim MyValue As Variant: MyValue = ThisWorkbook.ActiveSheet.Range("C2").Value
     With Workbooks("Dash").Worksheets("DASH")
         Dim last As Long: last = .Cells(.Rows.Count, "JF").End(xlUp).Row + 1
         .Cells(last, "JF").Value = MyValue
     End With
End Sub

I tried declaring a few more variables to try and loop it through but I can't get it to work, it keeps posting only in the very first cell.

英文:

I currently send a cells value to another spreadsheet to the next open cell in a column. But my current code sends it to the bottom most cell after all the values, so if I delete any cells in between it still only posts at the end of the data. I would like it to fill in cells I have deleted so it's not leaving empty rows. Any help is greatly appreciated.

Sub S2WL()

     Dim MyValue As Variant: My Value = ThisWorkbook.Activesheet.Range("C2").Value
     With Workbooks("Dash").Worksheets("DASH")
     Dim last As Long: last = .Cells(.Rows.Count, "JF").End(xlUp).Row + 1
     .Cells(last, "JF").Value = MyValue

End sub

I tried declaring a few more variables to try and loop it through but I can't get it to work, it keeps posting only in the very first cell.

答案1

得分: 1

我已经更新了我的代码,以处理其中一个JF1或JF2为空的特殊情况。

@BigBen提出了一个很好的观点。表格中的范围行为不同。这假设列JF不是表格的一部分。至于循环,根据@Tony的评论,我假设你是从循环中调用这个子程序。但是对于循环,请移除硬编码的"C2",用MyValue来代替。你想要循环输入值,对吧?

Sub S2WL()
    Dim last As Long, rngCell As Range
    Dim MyValue As Variant: MyValue = ThisWorkbook.ActiveSheet.Range("C2").Value
    
    With Workbooks("Dash").Worksheets("DASH")
        Set rngCell = .Cells(1, "JF")
        
        ' 查找第一个空单元格
        If rngCell = "" Then  ' 特殊情况:JF1 = ""
            last = 1
        ElseIf rngCell.Offset(1, 0) = "" Then  ' 特殊情况:JF1 <> "",JF2 = ""
            last = 2
        Else   ' JF1 <> "",JF2 <> "";向下是安全的
            Set rngCell = rngCell.End(xlDown)
            last = rngCell.Row
            If last < .Rows.Count Then  ' 避免在最后一行加1的错误
                last = last + 1
            End If
        End If
        
        If .Cells(last, "JF").Value = "" Then .Cells(last, "JF").Value = MyValue
    End With
End Sub
英文:

Edit

I've updated my code to handle the special case where either JF1 or JF2 is empty.

@BigBen made a good point. Ranges behave differently in tables. This assumes column JF is not part of a table. As for looping, per @Tony comment, I assume you're calling this sub from a loop. But for looping, remove the hardcoded "C2" for MyValue. You'd want to loop through input values, no?

Sub S2WL()
    Dim last As Long, rngCell As Range
    Dim MyValue As Variant: MyValue = ThisWorkbook.ActiveSheet.Range(&quot;C2&quot;).Value
    
    With Workbooks(&quot;Dash&quot;).Worksheets(&quot;DASH&quot;)
        Set rngCell = .Cells(1, &quot;JF&quot;)
        
        &#39; Find first empty cell
        If rngCell = &quot;&quot; Then  &#39; Special case: JF1 = &quot;&quot;
            last = 1
        ElseIf rngCell.Offset(1, 0) = &quot;&quot; Then  &#39; Special case: JF1 &lt;&gt; &quot;&quot;, JF2 = &quot;&quot;
            last = 2
        Else   &#39; JF1 &lt;&gt; &quot;&quot;, JF2 &lt;&gt; &quot;&quot;; end-down is safe
            Set rngCell = rngCell.End(xlDown)
            last = rngCell.Row
            If last &lt; .Rows.Count Then  &#39; Avoid error of adding 1 to the last row
                last = last + 1
            End If
        End If
        
        If .Cells(last, &quot;JF&quot;).Value = &quot;&quot; Then .Cells(last, &quot;JF&quot;).Value = MyValue
    End With
End Sub

huangapple
  • 本文由 发表于 2023年5月25日 03:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定