2014-07-25 3 views
0

사용자가 단추를 클릭하고 붙여 넣기 단추로만 작동하는 다른 매크로 단추가있는 새 워크 시트를 채우는 매크로 작업을하는 중입니다. 사용자는 그들이 무엇을 복사했는지. 현재 사용자는 "스크린 샷 추가"라는 버튼을 클릭하고 스크린 샷 워크 시트의 이름을 묻는 입력 상자가 표시됩니다. 사용자는 제목을 쓰고 사용자의 입력 제목으로 워크 시트의 이름으로 새 탭이 형성됩니다.붙여 넣기 단추가있는 새로운 워크 시트 열기

Sub AddScreenShot() 

Dim Title As Variant 


Title = Application.InputBox("Enter a Title: ", "Add Screen Shot", , 400, 290, , , Type:=2) 

    If Title = False Then 
     Exit Sub 

    ElseIf Title = vbNullString Then 
     MsgBox "A title was not entered. Please enter a Title" 
     Exit Sub 

    ElseIf Len(Title) > 15 Then 
     MsgBox "No more than 15 characters please" 
     Run "AddScreenShot" 

    Else 

    ActiveWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count)).name = Title 

    End If 


End Sub 

이미 열려 시트 내에서 활성 셀에 클립 보드의 이미지를 붙여 넣 서브 루틴이 있습니다 : 여기에 그렇게 할 수있는 코드

Sub Paste_Image() 

On Error GoTo PasteError 

    Application.ScreenUpdating = False 
    Range("E5").Activate 
    ActiveSheet.Paste 
    Application.ScreenUpdating = True 
    ActiveSheet.Unprotect Password:=xxxx 

GetOutOfHere: 
    Exit Sub 

PasteError: 
    MsgBox "Please verify that an image has been copied", vbInformation, "Paste Image" 
    Resume GetOutOfHere 

End Sub 

문제는 나도 몰라하는 방법에 사용자가 시트의 제목을 입력하고 확인을 클릭하면 새 시트가 위의 붙여 넣기 서브 루틴을 실행할 매크로 버튼으로 채워지도록 두 코드 스 니펫을 함께 연결합니다. 두 링크를 연결하고 사용자가 확인을 클릭하여 새 워크 시트를 만들 때 붙여 넣기 하위를 실행하는 것에 대한 제안 사항은 무엇입니까?

감사합니다.

+0

:

Dim btn As Button Application.ScreenUpdating = False Dim t As Range Dim sht As Sheet 'Added to ensure we don't add duplicate sheets Set t = ActiveSheet.Range(Cells(1, 1)) Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height) With btn .OnAction = "Paste_Image" 'Calls the Paste_Image subroutine when clicked .Caption = "Paste" 'Change caption as you see fit .Name = "btnPaste" 'Change name as you see fit End With Next i Application.ScreenUpdating = True 

그래서 전체 코드는 다음과 같이한다 명시 적으로 Paste_Image 호출 코드'Call Paste_Image'를 추가하여 호출합니다. – Soulfire

+0

감사합니다. Josh .. 이전에 시도 했었지만 새로운 시트가 만들어지면 Paste_Image 코드가 그대로 셀 E5에 채워집니다. 양식을 만들지 않았기 때문입니다. 그 매크로를 실행하는 버튼 .. 내가 추측하는 문제는 해당 Paste_Image Sub를 실행할 수있는 새 워크 시트에 Button을 채우는 것입니다. –

+0

아 맞아, 나는 마지막 (그리고 가장 중요한) 부분을 건너 뛴다. 이 점에 대해 좀 더 생각해 보겠습니다. 나는 당신을 위해 뭔가를 생각해 낼 수 있습니다. 'AddScreenShot()'서브 루틴에 워크 시트를 추가하는 대신 이미 워크 시트에 버튼이있는 모의 워크 시트를 복사하면됩니다. – Soulfire

답변

1

런타임에 버튼을 만들 수 있습니다.

이 메서드를 사용하면 시트를 만들 때 프로그래밍 방식으로 단추를 추가 할 수 있습니다. . 당신이 할 수있는 이름 = Title` 라인 :은`ActiveWorkbook.Worksheets.Add (= 워크 시트 (Worksheets.Count) 후) 후

Sub AddScreenShot() 

    Dim Title As Variant 
    Dim btn As Button 
    Dim t As Range 
    Dim sht As Worksheet 

    Title = Application.InputBox("Enter a Title: ", "Add Screen Shot", , 400, 290, , , Type:=2) 

     If Title = False Then 
      Exit Sub 

     ElseIf Title = vbNullString Then 
      MsgBox "A title was not entered. Please enter a Title" 
      Exit Sub 

     ElseIf Len(Title) > 15 Then 
      MsgBox "No more than 15 characters please" 
      Run "AddScreenShot" 

     Else 

      On Error Resume Next 
      Set sht = ActiveWorkbook.Worksheets(Title) 
      On Error GoTo 0 

      If Not sht Is Nothing Then 
       MsgBox "A worksheet named " & Title & " already exists!" 
       Run "AddScreenShot" 

      Else 

       Application.ScreenUpdating = False 
       ActiveWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = Title 
       Set t = ActiveSheet.Range("A1:B2") 'Button will appear in cell A1:B2, change to whatever you want. 

       Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height) 'This will make the button the size of the cell, may want to adjust 
       With btn 
        .OnAction = "Paste_Image" 'Calls the Paste_Image subroutine when clicked 
        .Caption = "Paste" 'Change caption as you see fit 
        .Name = "btnPaste" 'Change name as you see fit 
       End With 

       Application.ScreenUpdating = True 
      End If 
     End If 


    End Sub 
+0

감사합니다. Josh. 이것은 깔끔한 해결 방법처럼 보입니다. 세트 t = ActiveSheet.Range (셀 (1,1)) 및 오류가 있습니다. –

+0

그래, A1 : B2 범위의 버튼을 배치 할 Range ("A1 : B2")로 업데이트했습니다. 미안합니다! 그게 내가 테스트하기 전에 코드를 게시하는거야! – Soulfire

+1

하하 내가 이것을 읽기 전에 그것을 바꿨다! 큰 마음은 다 비슷 하네! 고마워요 조쉬가 멋지게 작동합니다. –

관련 문제