英文:
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("C2").Value
With Workbooks("Dash").Worksheets("DASH")
Set rngCell = .Cells(1, "JF")
' Find first empty cell
If rngCell = "" Then ' Special case: JF1 = ""
last = 1
ElseIf rngCell.Offset(1, 0) = "" Then ' Special case: JF1 <> "", JF2 = ""
last = 2
Else ' JF1 <> "", JF2 <> ""; end-down is safe
Set rngCell = rngCell.End(xlDown)
last = rngCell.Row
If last < .Rows.Count Then ' Avoid error of adding 1 to the last row
last = last + 1
End If
End If
If .Cells(last, "JF").Value = "" Then .Cells(last, "JF").Value = MyValue
End With
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论