行中的第一个空单元格

时间:2012-10-23 10:14:09

标签: excel vba range cell rows

我正在将范围复制到另一个工作簿的下一个空单元格中。以下代码;

Public Sub InvoicedInstallments()

Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim fname As String
Dim mt As String, yr As String
mt = MonthBox.Value
yr = YearBox.Value
fname = yr & mt & "DB" & ".xlsx"
Set rng1 = Workbooks("201209TB.xlsx").Worksheets("excel").Range("E348")
Set rng2 = Workbooks("201209TB.xlsx").Worksheets("excel").Range("E295")
Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Cells(1, Columns.Count).End(xlToRight).Select

rng3.Value = (rng1.Value + rng2.Value)
rng3.Value = rng3.Value * -1
rng3.Value = Round(rng3.Value / 1000, 0)

End Sub

有线

Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Cells(1, Columns.Count).End(xlToRight).Select

更改为

Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Range("AA49").End(xlToRight).Offset(0, 1)

现在将把数据放入下一个空单元格(这是我想要的),但如果再次运行代码,它就不会将信息放入下一个空单元格中。

我想这只是一些简单但我无法发现的事情

它正在复制到合并的单元格中,我怀疑它存在很多问题 (见下面的评论确认)

1 个答案:

答案 0 :(得分:0)

我猜,你的问题将出现在这一行:

rng3.Value = Round(rng3.Value / 1000, 0)

但是这里引发了错误

Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Cells(1, Columns.Count).End(xlToRight).Select

这是因为select

Select,选择,它不会返回范围对象。

使用此代替

Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Cells(1, Columns.Count).End(xlToRight)

但是,我不确定,如果这是你想要的,因为你可能会说

Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Range("IV1")

因为这是你得到的 - 至少,如果你只使用.xlsx