I'm trying to make a range value "paste" without using the copy/paste-function. I'm fairly new to VBA and don't understand why my code doesn't work. I'm sorry if this is a type of question which is already answered but I can't figure my problem out with other posts.
I'm able to select the range I want my values to copy from and paste too with
name_task = [code].select name_task_2 = [code].select But i can't write values to name_task_2 with
name_task_2 = name_task.value This works, and for ME is what I've written somehow the same just more complex, but since it doesn't work it obviously isn't the same :D
sht2.Range("C2:D12") = sht1.Range("A8:B18").value Sub Time_Estimate() Application.ScreenUpdating = False Dim name_task As Variant Dim name_task_2 As Variant Dim R_count As Double Dim C_count As Double Dim sht1 As Worksheet Dim sht2 As Worksheet Set sht1 = Sheet1 'Sheets("Tekla_2016") name_task = sht1.Range("A8:B8", sht1.Range("A8").End(xlDown)) R_count = sht1.Range("A8:B8", sht1.Range("A8").End(xlDown)).Rows.Count 'Debug.Print name_task.Rows.Count 'Debug.Print name_task.Columns.Count Debug.Print R_count Set sht2 = Sheet2 'Sheets("Timeforbruk_2016 - UFERDIG") name_task_2 = sht2.Range("C2:D2", sht2.Range("C2:D2").Offset(R_count - 1, 0)) 'name_task_2 = name_task.value 'Why doesn't this work??? 'sht2.Range("C2:D12") = sht1.Range("A8:B18").value 'This works but it's not dynamic Application.ScreenUpdating = True End Sub EDIT: This code now work as I want:
Sub Time_Estimate() Application.ScreenUpdating = False Dim name_task As Range Dim name_task_2 As Range Dim rng_sht1 As Range Dim rng_sht2 As Range Dim R_count As Double Dim C_count As Double Dim sht1 As Worksheet Dim sht2 As Worksheet Set sht1 = Sheet1 'Sheets("Tekla_2016") Set sht2 = Sheet2 'Sheets("Timeforbruk_2016 - UFERDIG") Set name_task = sht1.Range("A8:B8", sht1.Range("A8").End(xlDown)) R_count = sht1.Range("A8:B8", sht1.Range("A8").End(xlDown)).Rows.Count Set name_task_2 = sht2.Range("C2:D2", sht2.Range("C2:D2").Offset(R_count - 1, 0)) name_task_2 = name_task.value Application.ScreenUpdating = True End Sub
name_task = [code].selectDoesn't work, butname_task = [code]should work, if[code]returns a range object. Just like in your other example:sht2.Range("C2:D12") = sht1.Range("A8:B18").valueworks, butsht2.Range("C2:D12").Select = sht1.Range("A8:B18").valuewill not work - it doesn't make sense.Double. It's better to use an integer type. In VBA it is best to useLong(4 bytes) asIntegerhas only 2 bytes and thus has an upper limit of 32767