2017-09-28 113 views
1

我知道這個問題已經被問,但我想了解我在做什麼錯Excel宏複製功能

我只想一個細胞在不同的工作表複製到另一個細胞。複製的單元格取決於按鈕所在的行。

這裏是我的代碼:

Option Explicit 

Sub CreateButtons() 

    Dim i As Long 
    Dim shp As Object 
    Dim dblLeft As Double 
    Dim dblTop As Double 
    Dim dblWidth As Double 
    Dim dblHeight As Double 

    With Sheets("SALES UNFULFILLED") 
     dblLeft = .Columns("AZ:AZ").Left  'All buttons have same Left position 
     dblWidth = .Columns("AZ:AZ").Width 'All buttons have same Width 
     For i = 2 To 200      'Starts on row 2 and finishes row 20 
      dblHeight = .Rows(i).Height  'Set Height to height of row 
      dblTop = .Rows(i).Top   'Set Top top of row 
      Set shp = .Buttons.Add(dblLeft, dblTop, dblWidth, dblHeight) 
      shp.OnAction = "IdentifySelected" 
      shp.Characters.Text = "Invoice" 
     Next i 
    End With 
End Sub 


Sub IdentifySelected() 
    'NOTE: The button will always be on the active sheet 
    Dim strButtonName 
    Dim lngRow As Long 

    strButtonName = ActiveSheet.Shapes(Application.Caller).Name 
    lngRow = ActiveSheet.Shapes(strButtonName).TopLeftCell.Row 
    'MsgBox "Button is on row " & lngRow 
    Worksheets("invoice").Range(Cells(5, 3)).Value = Worksheets("SALES UNFULFILLED").Range(Cells(lngRow, 4)).Value 
End Sub 

我一直在這行代碼得到一個錯誤1004,但我不明白爲什麼 Worksheets("invoice").Range(Cells(5, 3)).Value = Worksheets("SALES UNFULFILLED").Range(Cells(lngRow, 4)).Value

我試圖改變「價值」,「選擇「但沒有機會

+1

嘗試'工作表( 「收據」),範圍(工作表( 「發票」)。將細胞(5,3))。值=工作表( 「SALES秕」) (工作表(「發票」)。單元格(lngRow,4))。值或只是工作表(「發票」)。單元格(5,3)。值=工作表單元(lngRow ,4).Value' – danieltakeshi

回答

1

我認爲你必須改變:

Worksheets("invoice").Range(Cells(5, 3)).Value = Worksheets("SALES UNFULFILLED").Range(Cells(lngRow, 4)).Value 

Worksheets("invoice").Cells(5, 3).Value = Worksheets("SALES UNFULFILLED").Cells(lngRow, 4).Value 

希望這有助於