从UserForm数据填充单元格。根据`sub`,目标单元格改变

时间:2019-11-22 08:21:19

标签: excel vba range userform

假设我有samenstelling1samenstelling2samenstelling3

在每个子模块中,我调用我的用户表单samenstelling,然后用户填写5个字段。

如何获取用户表单中填充的数据以填充每个模块不同的某些单元格?

当用户在填写数据后按OK时,我可以在子目录中显示一个msgbox,因此我知道该子目录与用户表单相关联。我一直坚持如何将数据粘贴到某些单元格。

对于samenstelling1单元格为“ Q500:O500”,对于samenstelling2单元格为“ Q501:O501”等。

btnok

的代码
Public Sub btnok_Click()
Dim letter As String
Dim tekeningnr As String
Dim omschrijving As String
Dim posnummer As String
Dim revletter As String

tekeningnr = txttekeningnummer.Value
omschrijving = txtomschrijving.Value

revletter = cmbrevisieletter.Value
posnummer = cmbposnummer.Value
letter = UCase(cmbletter.Value)

Unload Me

End Sub

samenstelling1

的代码
Sub samenstelling1()
Sheets("Artikelen_aanmaken").Activate

Dim letter As String
Dim tekeningnr As String
Dim omschrijving As String
Dim posnummer As String
Dim revletter As String

Samenstelling.UserForm_Initialize
Samenstelling.Show

'this part is not working because it doesn't receive that data from the userform, the cells stay empty

    Range("q500") = cmbletter.Value
    Range("N500") = txttekeningnummer.Value
    Range("P500") = cmbrevisieletter.Value
    Range("R500") = txtomschrijving.Value
    Range("O500") = cmbposnummer.Value

           Select Case posnummer
            Case Is = 1

我也在下面的代码中进行了尝试,但我认为它只是将我放入这些单元格中的数据定义为string,然后

        Range("q500") = letter
        Range("N500") = tekeningnr
        Range("P500") = revletter.Value
        Range("R500") = omschrijving.Value
        Range("O500") = posnummer.Value

我有选择案例的样本,其中填充了posnummers的数量(因此,这个选择案例位于每个samenstelling_子中。.

Select Case posnummer
            Case Is = 1
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k2"), Type:=xlFillSeries

            Case Is = 2
                Sheets("Artikelen_aanmaken").Activate
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k3"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c3"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i3"), Type:=xlFillDefault

            Case Is = 3
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k4"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c4"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i4"), Type:=xlFillDefault

            Case Is = 4
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k5"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c5"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i5"), Type:=xlFillDefault

            Case Is = 5
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k6"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c6"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i6"), Type:=xlFillDefault

            Case Is = 6
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k7"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c7"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i7"), Type:=xlFillDefault

            Case Is = 7
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k8"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c8"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i8"), Type:=xlFillDefault

            Case Is = 8
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k9"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c9"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i9"), Type:=xlFillDefault

            Case Is = 9
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k10"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c10"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i10"), Type:=xlFillDefault

            Case Is = 10
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k11"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c11"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i11"), Type:=xlFillDefault

            Case Is = 11
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k12"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c13"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i13"), Type:=xlFillDefault

            Case Is = 12
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k13"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c14"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i14"), Type:=xlFillDefault

            Case Is = 13
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k14"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c15"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i15"), Type:=xlFillDefault

            Case Is = 14
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k15"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c16"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i17"), Type:=xlFillDefault

            Case Is = 15
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k16"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c18"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i18"), Type:=xlFillDefault

            Case Is = 16
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k17"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c19"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i19"), Type:=xlFillDefault

            Case Is = 17
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k18"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c20"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i20"), Type:=xlFillDefault

            Case Is = 18
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k19"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c21"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i21"), Type:=xlFillDefault

            Case Is = 19
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k14"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c22"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i22"), Type:=xlFillDefault

            Case Is = 20
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k15"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c23"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i23"), Type:=xlFillDefault

            Case Is = 21
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k16"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c24"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i24"), Type:=xlFillDefault

            Case Is = 22
                Range("18:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k17"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c25"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i25"), Type:=xlFillDefault

            Case Is = 23
                Range("19:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k18"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c26"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i26"), Type:=xlFillDefault

            Case Is = 24
                Range("20:30").EntireRow.Hidden = True
                Range("A2:k2").Select
                Range("A2:k2").AutoFill Destination:=Range("A2:k19"), Type:=xlFillSeries

                    Sheets("Artikelen_in_stuklijsten").Activate
                    Range("18:30").EntireRow.Hidden = True
                    Range("A2:c2").Select
                    Range("A2:c2").AutoFill Destination:=Range("A2:c27"), Type:=xlFillSeries
                    Range("d2:i2").Select
                    Range("d2:i2").AutoFill Destination:=Range("d2:i27"), Type:=xlFillDefault

2 个答案:

答案 0 :(得分:0)

这不是完整的代码,但它应该使您入门。

Option Explicit

Public Sub btnok_Click()
Dim posnummer As Integer
Public Sub btnok_Click()
Dim letter As String
Dim tekeningnr As String
Dim omschrijving As String
Dim posnummer As String
Dim revletter As String

tekeningnr = txttekeningnummer.Value
omschrijving = txtomschrijving.Value

revletter = cmbrevisieletter.Value
posnummer = cmbposnummer.Value
letter = UCase(cmbletter.Value)

posnummer = "" 'Put the code which determines which row it should go on here.

Samenstelling (posnummer)

Unload Me

End Sub

Function Samenstelling(posnummer As Integer)

'Sheets("Artikelen_aanmaken").Activate 'No need to activate sheets, use the following instead, and call it before your range
Dim blad As Worksheet: blad = Sheets("Artikelen_aanmaken")

    blad.Range("Q" & 500 + posnummer) = cmbletter.Value 'the 500 + posnummer means it starts at row 500, plus a row higher for every position you define. Start at zero (or change this to 499 +)
    Range("N" & 500 + posnummer) = txttekeningnummer.Value
    Range("P" & 500 + posnummer) = cmbrevisieletter.Value
    Range("R" & 500 + posnummer) = txtomschrijving.Value
    Range("O" & 500 + posnummer) = cmbposnummer.Value

End Sub

答案 1 :(得分:0)

使用许多不同的模块来做完全相同的事情,却只是引用另一范围的单元格,这使您很困难。

使用 @Component({ selector: 'foo', template: ` foo `, }) export class FooComponent { } @Directive({ selector: `hello` }) export class HelloDirective implements AfterViewInit{ @ViewChild(FooComponent, {static: true}) fooChild: FooComponent; ngAfterViewInit(){ console.log('ngAfterViewInit'); console.log(this.fooChild); } } @Component({ selector: 'hello', template: ` <foo></foo> ` }) export class HelloComponent{ @Input() name: string; } Select Case语句可以解决该问题。如果您以某种方式识别了工作表中的16个范围,那么即使If Then也可以轻松地排成一行。

但是,您当前的问题是从.Find获取值。在这种情况下,您可以使用UserForm变量。

例如:

Public

现在,如果我有一个Public wrd As String Sub Mod1() UserForm1.Show Debug.Print wrd End Sub Sub Mod2() UserForm1.Show Debug.Print wrd End Sub Sub Mod3() UserForm1.Show Debug.Print wrd End Sub 像这样:

enter image description here

我可以在按钮下方放置的代码如下:

UserForm

在您的特定情况下,它看起来像:

Private Sub CommandButton1_Click()

ts = Me.TextBox1.Value
Unload Me

End Sub

Public letter As String Public tekeningnr As String Public omschrijving As String Public posnummer As String Public revletter As String Sub samenstelling1() Samenstelling.Show 'Do more with the returned values from UserForm End sub Sub samenstelling2() Samenstelling.Show 'Do more with the returned values from UserForm End sub Sub samenstelling3() Samenstelling.Show 'Do more with the returned values from UserForm End sub 如下所示:

btnok_Click()