繁体   English   中英

当“sheet.range”列不为空时,在下一个空列中插入数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM