2016-11-22 1 views
1

다음 코드를 사용하고 있습니다. 내 userform의 데이터를 Excel 파일로 입력하려고합니다. 열 예정이다. 나는 내 userform에서 submit을 클릭하면 코드를 계속할 수있는 코딩을 시도했지만 데이터를 잃어 버리거나 선택한 Excel 파일에 입력하지는 않습니다. 따라서 Excel에서 필요한 데이터를 내 단어 파일로 복사 할 수 없습니다. 내가 말할 것 형태 코드 보지 않고단어에 대한 사용자 형식의 입력 데이터를 나중에 코드가 열리는 Excel 파일에 삽입하려면 어떻게해야합니까?

Sub Data() 

    UserForm1.Show 'show the userform 

    Dim exl As Object 'exl ist der Verweis auf Excel 
    Dim oExl As Object 

    Dim ImportDatei As Variant 


    Set exl = CreateObject("excel.application") 
    ImportDatei = exl.Application.GetOpenFilename(FileFilter:="Microsoft Excel-Dateien (*.xlsm), *.xlsm", Title:="Eine Datei auswählen") 'ab exl. wir der Excel Befehl angefügt 
    If ImportDatei = False Then Exit Sub 


    exl.Workbooks.Open (ImportDatei) 
    exl.Visible = True 

    'Input data into the excel field 
    exl.Range("C1").Select 'Select the cell 
    exl.ActiveCell.FormulaR1C1 = TextBox1 'Insert the input Value in the cell 
    exl.ActiveSheet.Range("$A$5:$D$65").AutoFilter Field:=1, Criteria1:="<>" ' Filtern 


    ' Product (variante) copy and formating 
    exl.Range("A1:A1").Copy 
     Selection.PasteAndFormat (wdFormatPlainText) 
     Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend 
     Selection.Style = ActiveDocument.Styles("Heading 2") 
     Selection.MoveDown Unit:=wdLine, Count:=1 
     Selection.InsertBreak Type:=wdLineBreak 'Insert line space 

    ' Copy other relevant info 
    exl.Range("A5:A7").Copy 
     Selection.PasteAndFormat (wdFormatPlainText) 
     Selection.InsertBreak Type:=wdLineBreak 

    'Copy table 
    exl.Range("A8:D79").Copy 
     Selection.Paste 
     Selection.InsertBreak Type:=wdLineBreak 
     Selection.InsertBreak Type:=wdLineBreak 
End Sub 
+0

불행히도 저는 일주일 중 나머지 시간 동안 바빠서 더 이상 사과 할 수 없으므로 제 대답을 삭제했습니다. 사과드립니다. –

답변

0

: 때문에 또는 하위처럼

당신은 그 양식에서 저장 한 변수를 잃고은 이내에 선언 및 할당 만 로컬로 저장하는 기능을 수행합니다. 예를 들어

이 코드는 양식에있는 경우 :

Input_Button_Click() 

Dim This_String As String 
This_string = Textbox1.Value 

End Sub 

그리고 또 다른 하위는 그 같은 모듈 내는 것입니다 경우에도 :

Sub Show_Box_Text() 

MsgBox This_String 

End Sub 

빈 메시지 박스를 보여 주 시겠어요.

This_String은 선언 된 Userform, Sub 또는 Function의 범위 내에서만 사용할 수 있기 때문에 문제가됩니다.

Solution1 : 전화 같은 형태에서 인수 형태 코드 내에서 다른 하위 :

Solution2 : 오른쪽 값이나와 텍스트 상자가 표시됩니다

Input_Button_Click() 

Dim This_String As String 
This_string = Textbox1.Value 
Call Show_Text_Box(This_String) 'calls another sub or fucntion with argument This_String 

End Sub 

Sub Show_Box_Text (This_String as String) 
'now requires to be called with 1 argument of type String, 
'wich it will, for the duration of this sub refer to as This_String 
'name in the calling sub doesnt need to match This_String 
MsgBox This_String 

End Sub 

: 만들기 공개 퍼팅하여 변수; 은 "통합 문서"또는 "문서"코드에서

Public This_String as String 'now publicly available 

느릅 나무는 다음을 수행하여 통합 문서/문서 내에서 어디서나 할당 할 수 있습니다

: 다음은 IMO에 easyest 솔루션입니다

Input_Button_Click() 

Thisworkbook.This_String = Textbox1.Value 

End Sub 

Sub Show_Box_Text() 

MsgBox Thisworkbook.This_String 

End Sub 

당신의 문제.

+0

안녕하세요, 코드에 이러한 변경 사항을 추가하려고했지만 작동하지 않습니다. 여기에 내 코드를 업로드했습니다.이 변경 사항이 내 코드에 적용될 수있는 곳을 표시 할 수 있습니까? – Max

+0

개인 서브 Capturedata_Click() Thisworkbook.This_String = TextBox1.Value 최종 하위 개인 서브 UserForm_Click() 있는 MsgBox Thisworkbook.This_String 최종 하위 – Max

+0

나는 노력 그 어떤 이유 코드 그러나 didnt 한 작품. – Max