问题
I am copying a range into the next empty cell of a different workbook. the following code;
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
Has had the line
Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Cells(1, Columns.Count).End(xlToRight).Select
changed to
Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Range("AA49").End(xlToRight).Offset(0, 1)
this will now put the data into the next empty cell (which is what I wanted) but it does not then put information into the next empty cell if you run the code again.
I imagine it is just something simple but I can't spot it
It is copying into merged cells which I suspect holds a lot of issues (see below comments for confirmation of this)
回答1:
I am guessing, your problem will be in this line:
rng3.Value = Round(rng3.Value / 1000, 0)
but the error is thrown here
Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Cells(1, Columns.Count).End(xlToRight).Select
it is because of the select
.
Select
, selects, it does not return you a range object.
use this instead
Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Cells(1, Columns.Count).End(xlToRight)
However, I am not sure, if this is what you want, because you might just say
Set rng3 = Workbooks(fname).Worksheets("UK monthly dashboard").Range("IV1")
because this is what you get - at least, if you are working only with .xlsx
来源:https://stackoverflow.com/questions/13028201/first-empty-cell-in-row