2016-11-22 1 views
1

私は次のコードを書いていますが、私のユーザーフォームのデータを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、そのは、同じモジュール内にある場合でも:

Sub Show_Box_Text() 

MsgBox This_String 

End Sub 

は、空のメッセージボックスが表示されるでしょう。

This_Stringは、宣言されているUserform、Sub、またはFunctionのスコープ内でのみ使用可能です。

解決法1: コールのような形から引数を持つフォームのコードの中から別のサブ:

対処方法2:正しい値かでテキストボックスが表示されます

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 

WICHは、実行して、ブック/ドキュメント内の任意の場所に割り当てることができ

:これらは、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

ます。Private Sub Capturedata_Click() Thisworkbook.This_String = TextBox1.Value End Subの プライベートサブUserForm_Click() のMsgBox Thisworkbook.This_String End Subの – Max

+0

私がコードすることを試みたが、それはいくつかの理由のために働くのdidnt。 – Max

関連する問題