我試圖使範圍值「粘貼」而不使用複製/粘貼功能。我對VBA相當陌生,不明白爲什麼我的代碼不起作用。對不起,如果這是一種已經回答的問題,但我無法通過其他帖子找出問題。動態範圍內的VBA範圍值
我能選擇我想我的價值觀中複製並粘貼過與
name_task = [code].select
name_task_2 = [code].select
的範圍,但我不能寫值與
name_task_2 = name_task.value
這工作name_task_2,對於我來說,我寫了一些相同的東西,但是因爲它不起作用,所以它顯然不一樣: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
編輯: 此代碼現在的工作,因爲我想:
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
那究竟是什麼不起作用?您發佈的子版本或僅僅是第一行代碼?錯誤信息也很重要。 – arcadeprecinct
'name_task = [code] .select'不工作,但'name_task = [code]'應該可以工作,如果'[code]'返回一個範圍對象。就像你的另一個例子:'sht2.Range(「C2:D12」)= sht1.Range(「A8:B18」).value' works,but'sht2.Range(「C2:D12」)。Select = sht1 .Range(「A8:B18」)。value不起作用 - 它沒有任何意義。 – vacip
如果你正在計算類似行的東西,你不需要'Double'。最好使用整數類型。在VBA中,最好使用'Long'(4個字節),因爲'Integer'只有2個字節,因此它的上限爲32767 – arcadeprecinct