why code strat from row3 instead of row2 when copy data from userform?

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
Hello
I'm not sure where is the mistake should be fixing. the code works except one thing . should start from row2 when copy data from userform .
but now start from row3
VBA Code:
Private Sub CommandButton4_Click()
 Dim i As Long
   
   With Sheets("PURCHASE")
      .Range("e5").MergeArea = Me.TextBox1.Value
      .Range("e8").MergeArea = Me.TextBox2.Value

      For i = 1 To 11
         .Cells(i + 1, 1).Value = Me.Controls("TextBox" & i)
      Next i
      For i = 12 To 44
         .Cells(((i - 11) Mod 11) + 1, Int((i - 11) / 11) + 6).Value = Me.Controls("Textbox" & i)
      Next i
      For i = 2 To 45
         .Cells(((i - 1) Mod 11) + 1, Int((i - 1) / 11) + 2).Value = Me.Controls("ComboBox" & i)
      Next i
   End With
   TextBox34.Value = Me.TextBox12.Value * Me.TextBox23.Value
 TextBox35.Value = Me.TextBox13.Value * Me.TextBox24.Value
 TextBox36.Value = Me.TextBox14.Value * Me.TextBox25.Value
 
End Sub
any help guys,please?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Your three loops copy the content of the controls to a cell address as below:
Code:
    For i = 1 To 11:
TextBox_1     A2
TextBox_2     A3
TextBox_3     A4
TextBox_4     A5
TextBox_5     A6
TextBox_6     A7
TextBox_7     A8
TextBox_8     A9
TextBox_9     A10
TextBox_10    A11
TextBox_11    A12

    For i = 12 To 44:
TextBox_12    F2
TextBox_13    F3
TextBox_14    F4
TextBox_15    F5
TextBox_16    F6
TextBox_17    F7
TextBox_18    F8
TextBox_19    F9
TextBox_20    F10
TextBox_21    F11
TextBox_22    G1
TextBox_23    G2
TextBox_24    G3
TextBox_25    G4
TextBox_26    G5
TextBox_27    G6
TextBox_28    G7
TextBox_29    G8
TextBox_30    G9
TextBox_31    G10
TextBox_32    G11
TextBox_33    H1
TextBox_34    H2
TextBox_35    H3
TextBox_36    H4
TextBox_37    H5
TextBox_38    H6
TextBox_39    H7
TextBox_40    H8
TextBox_41    H9
TextBox_42    H10
TextBox_43    H11
TextBox_44    I1

    For i = 2 To 45:
ComboBox_2    B2
ComboBox_3    B3
ComboBox_4    B4
ComboBox_5    B5
ComboBox_6    B6
ComboBox_7    B7
ComboBox_8    B8
ComboBox_9    B9
ComboBox_10   B10
ComboBox_11   B11
ComboBox_12   C1
ComboBox_13   C2
ComboBox_14   C3
ComboBox_15   C4
ComboBox_16   C5
ComboBox_17   C6
ComboBox_18   C7
ComboBox_19   C8
ComboBox_20   C9
ComboBox_21   C10
ComboBox_22   C11
ComboBox_23   D1
ComboBox_24   D2
ComboBox_25   D3
ComboBox_26   D4
ComboBox_27   D5
ComboBox_28   D6
ComboBox_29   D7
ComboBox_30   D8
ComboBox_31   D9
ComboBox_32   D10
ComboBox_33   D11
ComboBox_34   E1
ComboBox_35   E2
ComboBox_36   E3
ComboBox_37   E4
ComboBox_38   E5
ComboBox_39   E6
ComboBox_40   E7
ComboBox_41   E8
ComboBox_42   E9
ComboBox_43   E10
ComboBox_44   E11
ComboBox_45   F1

So I seem that there is more confusion then "starting from line 3"

In case the following mapping is correct...
Code:
    For i = 1 To 11:
TextBox_1     A2
TextBox_2     A3
TextBox_3     A4
TextBox_4     A5
TextBox_5     A6
TextBox_6     A7
TextBox_7     A8
TextBox_8     A9
TextBox_9     A10
TextBox_10    A11
TextBox_11    A12

    For i = 12 To 44:
TextBox_12    F2
TextBox_13    F3
TextBox_14    F4
TextBox_15    F5
TextBox_16    F6
TextBox_17    F7
TextBox_18    F8
TextBox_19    F9
TextBox_20    F10
TextBox_21    F11
TextBox_22    F12
TextBox_23    G2
TextBox_24    G3
TextBox_25    G4
TextBox_26    G5
TextBox_27    G6
TextBox_28    G7
TextBox_29    G8
TextBox_30    G9
TextBox_31    G10
TextBox_32    G11
TextBox_33    G12
TextBox_34    H2
TextBox_35    H3
TextBox_36    H4
TextBox_37    H5
TextBox_38    H6
TextBox_39    H7
TextBox_40    H8
TextBox_41    H9
TextBox_42    H10
TextBox_43    H11
TextBox_44    H12

    For i = 2 To 45:
ComboBox_2    B2
ComboBox_3    B3
ComboBox_4    B4
ComboBox_5    B5
ComboBox_6    B6
ComboBox_7    B7
ComboBox_8    B8
ComboBox_9    B9
ComboBox_10   B10
ComboBox_11   B11
ComboBox_12   B12
ComboBox_13   C2
ComboBox_14   C3
ComboBox_15   C4
ComboBox_16   C5
ComboBox_17   C6
ComboBox_18   C7
ComboBox_19   C8
ComboBox_20   C9
ComboBox_21   C10
ComboBox_22   C11
ComboBox_23   C12
ComboBox_24   D2
ComboBox_25   D3
ComboBox_26   D4
ComboBox_27   D5
ComboBox_28   D6
ComboBox_29   D7
ComboBox_30   D8
ComboBox_31   D9
ComboBox_32   D10
ComboBox_33   D11
ComboBox_34   D12
ComboBox_35   E2
ComboBox_36   E3
ComboBox_37   E4
ComboBox_38   E5
ComboBox_39   E6
ComboBox_40   E7
ComboBox_41   E8
ComboBox_42   E9
ComboBox_43   E10
ComboBox_44   E11
ComboBox_45   E12

...then I should suggest that your loops are modified as follows:
Code:
      For i = 1 To 11                       'This is unmodified
''        Debug.Print "TextBox_" & i, Cells(i + 1, 1).Address(0, 0)
         .Cells(i + 1, 1).Value = Me.Controls("TextBox" & i)
      Next i

      For i = 12 To 44
''        Debug.Print "TextBox_" & i, Range("F2").Offset((i - 12) Mod 11, Int((i - 12) / 11)).Address(0, 0)
        .Range("F2").Offset((i - 12) Mod 11, Int((i - 12) / 11)).Value = Me.Controls("Textbox" & i)
      Next i
      
      For i = 2 To 45
''        Debug.Print "ComboBox_" & i, Range("B2").Offset((i - 2) Mod 11, Int((i - 2) / 11)).Address(0, 0)
         .Range("B2").Offset((i - 2) Mod 11, Int((i - 2) / 11)).Value = Me.Controls("ComboBox" & i)
      Next i

If the mapping is not corrected (and then the suggested modification would not fit the need) then try describing how the several controls have to be mapped using the sheet cell addresses
 
Upvote 0
Solution
So I seem that there is more confusion then "starting from line 3"
My apologies !!

yes you're right this is not the original code . result of many tries to modifying to overcome this problem I really forgot post the original code. the only problem for posted code clears header in row1 starts from column C after I modified code.

your revised code works very well, thanks very much !
 
Upvote 0

Forum statistics

Threads
1,215,419
Messages
6,124,796
Members
449,189
Latest member
kristinh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top