-3

Not working

Range("C5:C" & iTotalRows + 1).Value = src.Worksheets("Loan").Range(Cells(4, 2), Cells(12, 2)).Value

is working

Range("D5:D" & iTotalRows + 1).Value=src.Worksheets("Loan").Range("C4:C" & iTotalRows).Value
Chronocidal
  • 6,827
  • 1
  • 12
  • 26

2 Answers2

1

You need to fully qualify your Range and Cells - try one of these

ActiveSheet.Range("C5:C" & iTotalRows + 1).Value = src.Worksheets("Loan").Range(src.Worksheets("Loan").Cells(4, 2), src.Worksheets("Loan").Cells(12, 2))
'Or
With src.Worksheets("Loan")
    ActiveSheet.Range("C5:C" & iTotalRows+1).Value = .Range(.Cells(4, 2), .Cells(iTotalRows, 2)).Value
End With

If we simplify this, your current code is effectively this:

ActiveSheet.Range("B2:B3").Value = Sheet1.Range(ActiveSheet.Cells(1,1),ActiveSheet.Cells(2,1)).Value

Which will only work if Sheet1 is the ActiveSheet - otherwise, Excel can't find the cell ActiveSheet.Cells(1,1) in worksheet Sheet1

Chronocidal
  • 6,827
  • 1
  • 12
  • 26
  • Thank you very much. It works perfectly – teerawat Apr 24 '19 at 08:41
  • Sorry, I have one more question. From my above question, the code can copy source to destination file using range object. Can range object able to do some calculation at source file before copy to destination file? I have tried something like below but it is not working. ActiveSheet.Range("B2:B3").Value = Sheet1.Range(ActiveSheet.Cells(1,1),ActiveSheet.Cells(2,1)).Value - Sheet1.Range(ActiveSheet.Cells(1,2),ActiveSheet.Cells(2,2)).Value Thank you for all of your help. – teerawat Apr 25 '19 at 02:14
  • @teerawat An interesting question! Unfortunately not - that will give a "Type Mismatch" error. However, if you were to `Set` your Ranges (`Dim output AS Range, leftVal AS Range, rightVal As Range: Set output = ActiveSheet.Range("B2:B3"): Set leftVal = Sheet1.Range(ActiveSheet.Cells(1,1),ActiveSheet.Cells(2,1)): Set rightVal As Sheet1.Range(ActiveSheet.Cells(1,2),ActiveSheet.Cells(2,2))`) then you could use a loop: `Dim x as Long: For x = 1 to output.Cells.Count: output.Cells(x) = leftVal.Cells(x) - rightVal.Cells(x): Next x` - if you can't get that to work, try posting a new question – Chronocidal Apr 25 '19 at 07:03
0

Understanding how to create and use workbook and worksheet variables will save you from lots of trouble.

You should always explicitly refer to the workbooks and worksheets to whitch the objects or collections you want to use, belong.

In this case you are using the .cells collection.

If you don't specify which worksheet this collection belongs to, excel will try to figure it out on it's own which could lead to unwanted behavior or even errors. Errors are actually the good scenario because they are easily traceable.

Using ActiveSheet references is a bad practice as well.

I would do it like so:

Dim src As Workbook 'source workbook
Dim dest As Workbook 'destination workbook
Dim srcWs As Worksheet 'source worksheet
Dim destWs As Worksheet 'destination worksheet
Set src = Application.Workbooks("Name of the source Workbook")
Set dest = Application.Workbooks("Name of the destination Workbook")
Set srcWs = src.Worksheets("Loan") 'assign the worksheet named "Loan", which belongs to the source workbook, to the srcWs variable
Set destWs = dest.Worksheets("Name of the destination Worksheet")

destWs.Range("C5:C" & iTotalRows + 1).Value = srcWs.Range(srcWs.Cells(4, 2), srcwsCells(12, 2)).Value

Also a very useful way to refer to the workbook in which the code is located is ThisWorkbook.

This way, you can get access to your workbook/worksheet properties and objects just by using the corresponding reference followed by a dot and then VBE's Intellisence will help you find what you're looking for.

Finally, I am not sure what you're trying to achieve exactly, but you should keep in mind that this:

destWs.Range("C5:C" & iTotalRows + 1).Value = srcWs.Range(srcWs.Cells(4, 2), srcwsCells(12, 2)).Value

will only assign as many values from the source range to the destination range, as the iTotalRows + 1 , will allow. So for example if iTotalRows=9 then you would have the following result:

enter image description here

Stavros Jon
  • 1,695
  • 2
  • 7
  • 17