+ Reply to Thread
Results 1 to 8 of 8

For loop not working when i assign form to button in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2017
    Location
    Lusaka
    MS-Off Ver
    2016
    Posts
    6

    For loop not working when i assign form to button in VBA

    I created a form in VBA with 4 fields, the ideal is that when i pick using the first field (Selecting from a combobox) the rest of the field gets updated with information using 'for loop'. This works fine until i assign the form to a button, when the form loads and show suddenly the 3 fields do get updated automatically depending on the data selected in the first field.

    Private Sub Relocate_Change()
    Dim i As Integer
    Dim lastrow As Integer

    lastrow = WsData.Range("A10000").End(xlUp).Row
    For i = 1 To lastrow
    If Cells(i, 2) = Relocate.Value Then
    RelocateToCombo.Value = Cells(i, 4)
    RelocateDepartment.Value = Cells(i, 5)
    RelocateBranch.Value = Cells(i, 6)
    End If
    Next i
    End Sub

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: For loop not working when i assign form to button in VBA

    Hi,

    It may be that you simply haven't got the right sheet
    Private Sub Relocate_Change() 
    Dim i As Integer 
    Dim lastrow As Integer 
    with WsData
    lastrow = .Range("A10000").End(xlUp).Row 
    For i = 1 To lastrow 
    If .Cells(i, 2) = Relocate.Value Then 
    RelocateToCombo.Value = .Cells(i, 4)
    RelocateDepartment.Value = .Cells(i, 5) 
    RelocateBranch.Value = .Cells(i, 6) 
    End If 
    Next i 
    end with
    End Sub
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    11-22-2017
    Location
    Lusaka
    MS-Off Ver
    2016
    Posts
    6

    Re: For loop not working when i assign form to button in VBA

    Hi, So just edited the code to include "with statement" but still when i assign the form to a button (in another sheet) when i can the form using the button it comes but failing to populate the fields

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: For loop not working when i assign form to button in VBA

    Did you also make the other changes- namely using .Cells in all instances and not just Cells?

  5. #5
    Registered User
    Join Date
    11-22-2017
    Location
    Lusaka
    MS-Off Ver
    2016
    Posts
    6

    Re: For loop not working when i assign form to button in VBA

    Have made the edits and the code looks like this now

    Dim i As Integer
    Dim lastrow As Integer

    With WsData
    lastrow = WsData.Range("A10000").End(xlUp).Row

    For i = 1 To lastrow
    If Cells(i, 2) = RelocateAssetNumber.Value Then
    RelocateAssignedToCombo.Value = .Cells(i, 4)
    RelocateDepartment.Value = .Cells(i, 5)
    RelocateBranch.Value = .Cells(i, 6)

    End If
    Next i
    End With


    But unfortunately, am still facing the same problem

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: For loop not working when i assign form to button in VBA

    You missed one
    If .Cells(i, 2) = RelocateAssetNumber.Value Then

  7. #7
    Registered User
    Join Date
    11-22-2017
    Location
    Lusaka
    MS-Off Ver
    2016
    Posts
    6

    Re: For loop not working when i assign form to button in VBA

    It has worked. THANK YOU VERY MCUH

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: For loop not working when i assign form to button in VBA

    You're welcome.

+ 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. Form Controls - Option Button not working
    By excel_googler in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2016, 12:32 PM
  2. [SOLVED] Assign macro on spin button on sheet (form control)
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-14-2016, 07:44 AM
  3. [SOLVED] How to assign the form in the command button.
    By somani123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-04-2014, 02:32 PM
  4. [SOLVED] Using macro to create a Form Control Button and Assign macro to it in a specified Cell
    By weige1989 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-30-2014, 11:51 AM
  5. Form (button) Macro not working
    By ufopilot3 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-25-2012, 10:37 AM
  6. A macro to create a form button, assign a macro and name the button
    By cl361 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2008, 01:07 AM
  7. Read-only error using Form Button with Assign Macro
    By samf88 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-09-2007, 02:15 PM

Tags for this Thread

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