+ Reply to Thread
Results 1 to 7 of 7

why when copy data from user form don't change by using worksheet change event ?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-14-2021
    Location
    LY
    MS-Off Ver
    2019
    Posts
    271

    why when copy data from user form don't change by using worksheet change event ?

    Hi experts ,
    I have two codes
    first
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
        If Intersect(Target, [B:B]) Is Nothing Or Target.Count > 1 Then Exit Sub
        If Not Intersect(Target, [B:B]) Is Nothing Then
        Target.Offset(, -1) = Date
       Range("H2", Range("F" & Rows.Count).End(xlUp).Offset(, 2)).Formula = "=F2*G2"
       Range("H2", Range("F" & Rows.Count).End(xlUp).Offset(, 2)).Value = Range("H2", Range("F" & Rows.Count).End(xlUp).Offset(, 2)).Value
      End If
    End Sub
    second
    Private Sub CommandButton1_Click()
     
    Dim i As Long, j As Long
    Dim LstRw As Long, CurrCon As String, ws As Worksheet
    Dim va, a As Long, b As Long, h As Long
    
    Set ws = sheet2
    'find last used row
    LstRw = ws.Range("B" & Rows.Count).End(xlUp).Row
    
        h = 11 * 7    'columns count on userform are 7,rows are 11
        a = 1
        ReDim va(1 To 11, 1 To 7)
    
        For i = 1 To h 'loop userform textbox
              b = b + 1
                va(a, b) = Me.Controls("Textbox" & i + 65).Value   'textboxes number start at 66
    
                  If b = 7 Then a = a + 1: b = 0
        Next i
    
    ws.Range("B" & LstRw + 1).Resize(UBound(va, 1), UBound(va, 2)) = va
    MsgBox "  ok"
    Dim m, n As Long
    For m = 66 To 145
    For n = 99 To 101
    
    Me.Controls("textbox" & m) = ""
    Me.Controls("label" & n) = ""
    Next
    Next
    End Sub
    when Press commandbutton1 will copy data to sheet2 without add date in column A and add formula in column H based on first code.
    I'm not sure why don't show in sheet!! , despite of I using this way with another code and works without any problem .
    is it problem from second code?
    I don't attach file because I have sensitive data , if anybody is capable to solve it without attached file I truly appreciated , otherwise I will attach file with random data .
    thanks

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: why when copy data from user form don't change by using worksheet change event ?


    Hi,

    as this is so obvious just reading the condition within the first event codeline !

    [Spoiler ►] What is obviously the Target.Count value ?‼ [◄ Spoiler]

  3. #3
    Forum Contributor
    Join Date
    07-14-2021
    Location
    LY
    MS-Off Ver
    2019
    Posts
    271

    Re: why when copy data from user form don't change by using worksheet change event ?

    which line ?

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,629

    Re: why when copy data from user form don't change by using worksheet change event ?

    "ws.Range("B" & LstRw + 1).Resize(UBound(va, 1), UBound(va, 2)) = va"
    va = 7 cols x 11 rows = 77, therefore, the target.count = 77. So:

    If Intersect(Target, [B:B]) Is Nothing Or Target.Count > 1 Then Exit Sub
    Ben Van Johnson

  5. #5
    Forum Contributor
    Join Date
    07-14-2021
    Location
    LY
    MS-Off Ver
    2019
    Posts
    271

    Re: why when copy data from user form don't change by using worksheet change event ?

    @protonLeah
    so I should get rid of this Target.Count > 1?
    if it's so will pops application defined error in this line

        Target.Offset(, -1) = Date

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,629

    Re: why when copy data from user form don't change by using worksheet change event ?

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("B:B")) Is Nothing _
            And Target.Rows.Count = 11 _
            And Target.Columns.Count = 7 Then
                Application.EnableEvents = False
                Cells(Target.Row, "A").Resize(rowsize:=Target.Rows.Count).Value = Date
                Range("H2", Range("F" & Rows.Count).End(xlUp).Offset(, 2)).Formula = "=F2*G2"
                Range("H2", Range("F" & Rows.Count).End(xlUp).Offset(, 2)).Value = Range("H2", Range("F" & Rows.Count).End(xlUp).Offset(, 2)).Value
                Application.EnableEvents = True
        Else
            MsgBox "out of range"
        End If
    End Sub

  7. #7
    Forum Contributor
    Join Date
    07-14-2021
    Location
    LY
    MS-Off Ver
    2019
    Posts
    271

    Re: why when copy data from user form don't change by using worksheet change event ?

    @protonLeah
    thanks for correction !
    but based on worksheet change event should just populate in column A,H based on filled for adjacent cell in column B . as you see the picture will populate date until eleventh row without any filling in adjacent cell in column B , should not do that .
    Attached Images Attached Images

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] change event help on a worksheet based on an activex drop down change in another sheet?
    By lilsnoop in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-05-2023, 12:13 AM
  2. [SOLVED] vba excel - worksheet change copy event and target different sheet
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-24-2017, 06:52 PM
  3. [SOLVED] Worksheet change event copy column error
    By ricklou in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-21-2017, 06:59 AM
  4. [SOLVED] How to prevent worksheet change invoking System X Control change event
    By dmw2014 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-13-2014, 10:35 PM
  5. [SOLVED] Change event problem when the user switches worksheet without pressing ENTER
    By franklyn in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-05-2012, 08:12 PM
  6. change event macro to evaluate windows user who initiated the change then send email
    By pmanoloff in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-23-2012, 03:31 PM
  7. Filling form fields via worksheet dropdowns, user update via form, change form color
    By Demosthenes&Locke in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2010, 08:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1