[英]Insert data in next empty column when “sheet.range” column is not empty
我正在使用下面的代碼通過 VBA 的索引匹配功能用每周數據填充表。
在第一周,每周工作表的 C 列需要填充,這就像一個魅力。
但是,從提供新數據的第 2 周開始,我需要確保不要覆蓋第 1 周的數據並將新數據添加到應該是 D 的下一列中。依此類推。
關於如何使用以下代碼實現此目標的任何建議?
Sub IndexMatchFunctionDynamic()
Dim destinationWs As Worksheet, dataWs As Worksheet
Dim destinationLastRow As Long, dataLastRow As Long, x As Long
Dim IndexRng As Range, MatchRng As Range
Set destinationWs = ThisWorkbook.Worksheets("Weekly")
Set dataWs = ThisWorkbook.Worksheets("Market Data")
destinationLastRow = destinationWs.Range("A" & Rows.Count).End(xlUp).Row
dataLastRow = dataWs.Range("A" & Rows.Count).End(xlUp).Row
Set IndexRng = dataWs.Range("P3:P" & dataLastRow)
Set MatchRng = dataWs.Range("A3:A" & dataLastRow)
For x = 2 To destinationLastRow
On Error Resume Next
a = Application.Match(destinationWs.Range("A" & x).Value, MatchRng, 0)
Nights = Application.Index(IndexRng, a)
If (IsError(a) Or IsEmpty(Nights)) Or (IsError(a) And IsEmpty(Nights)) Then
destinationWs.Range("C" & x).Value = 0
Else
destinationWs.Range("C" & x).Value = Nights
End If
On Error GoTo 0
Next x
End Sub
提前致謝
首先獲取工作表中的下一個可用列
PrintToColumn = destinationWs.Cells(1,Columns.Count).XlEnd(XlToLeft).Column + 1
然后打印該列中的值
destinationWs.Cells(x, PrintToColumn).Value = Nights
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.