简体   繁体   中英

How can I use input data from a userform on word to be later inserted into a excel file that my coding will open

I have the following coding and I am struggling to Input the data from my userform into an Excel file that my Code will open. I have tried puting in coding which allows the Code to continue once I have clicked submit on my userform, but than it looses the data or just doesn't Input it into the Excel file I selected. This therefore doesn't allow me to copy the necessary data from Excel into my word file.

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

Without seeing the form-code I would say:

You are losing the saved variables from that form because like a sub or function the declarations and assignments made within it are only saved locally.

For example if this code is in your form:

Input_Button_Click()

Dim This_String As String
This_string = Textbox1.Value

End Sub

And another Sub, even if its within the same module would be:

Sub Show_Box_Text ()

MsgBox This_String

End Sub

Would show an empty messagebox.

Thats because This_String is only available within the scope of the Userform, Sub or Function in wich it is declared.

Solution1: Call another sub from within the form-code with arguments from the form like:

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

Will show a textbox with right the value or:

Solution2: Make public Variables by putting;

Public This_String as String 'now publicly available

In the "Workbook" or "Document" code

Wich can then be assigned anywhere within the workbook/document by doing:

Input_Button_Click()

Thisworkbook.This_String = Textbox1.Value 

End Sub

Sub Show_Box_Text ()

MsgBox Thisworkbook.This_String

End Sub

These are IMO the easyest solutions to your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM