
我在谷歌上搜尋的大多數解決方案都是關於尋找第一個空白列或第一個空白行,但我特別需要在一系列行中找到第一個空白列。
我需要的是將工作簿中的內容貼到另一個工作簿,但首先,我需要在 (100:114) 的一系列行中找到第一個空白列。
我可以發佈到目前為止我所擁有的內容,但它還遠遠沒有發揮作用。
答案1
所以我的 VBA 技能完全生疏了,但如果你看看 Tom Urtis 的貢獻如何:選擇範圍在 Microsoft 開發人員網路上選擇未知起始位置的資料範圍,它具有必要的物件和方法,您需要這些物件和方法來建立您想要執行的操作的解決方案。以下是該連結中程式碼的重印:
Sub UnknownRange()
If WorksheetFunction.CountA(Cells) = 0 Then
MsgBox "There is no range to be selected.", , "No cells contain any values."
Exit Sub
Else
Dim FirstRow&, FirstCol&, LastRow&, LastCol&
Dim myUsedRange As Range
FirstRow = Cells.Find(What:="*", SearchDirection:=xlNext, SearchOrder:=xlByRows).Row
On Error Resume Next
FirstCol = Cells.Find(What:="*", SearchDirection:=xlNext, SearchOrder:=xlByColumns).Column
If Err.Number <> 0 Then
Err.Clear
MsgBox _
"There are horizontally merged cells on the sheet" & vbCrLf & _
"that should be removed in order to locate the range.", 64, "Please unmerge all cells."
Exit Sub
End If
LastRow = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
LastCol = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Set myUsedRange = Range(Cells(FirstRow, FirstCol), Cells(LastRow, LastCol))
myUsedRange.Select
MsgBox "The data range on this worksheet is " & myUsedRange.Address(0, 0) & ".", 64, "Range address:"
End If
End Sub