[RESOLVIDO] Como adaptar para incluir mais planilhas e variáveis?

jrs

08-10-2022 14:39:39

Como é feito para incluir mais planilhas e combobox a serem preenchidos?

Set ws = ThisWorkbook.Worksheets("Planilha4")
Set ws = ThisWorkbook.Worksheets("Planilha5")
Set ws = ThisWorkbook.Worksheets("Planilha6")


If x = 1 Then Me.ComboBox2.AddItem c
If x = 1 Then Me.ListBox1.AddItem c
If x = 1 Then Me.ListBox2.AddItem c

Código: Selecionar todos

Private Sub UserForm_Initialize()

'--------------------------'

Dim x As Long, ncell As Long
Dim Rng As Range, c As Range
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Planilha3")

With ws

    ncell = .Cells(.Rows.Count, "A").End(xlUp).Row
    Me.ComboBox1.Clear

    For y = 1 To ncell
        Set c = .Cells(y, 1)
        Set Rng = .Range(.Cells(2, 1), .Cells(y, 1))
        x = Application.WorksheetFunction.CountIf(Rng, c)
        If x = 1 Then Me.ComboBox1.AddItem c
    Next y
End With


End Sub

Reinaldo

09-10-2022 11:44:45

Algo +/- assim:

Código: Selecionar todos

Dim x As Long, ncell As Long, Z As Integer
Dim Rng As Range, c As Range
Dim ws As Worksheet
'Define matriz com planilhas e controles elegiveis
Dim strSheet(2) As String, ctrl(2) As String
strSheet(0) = "Plan1"
strSheet(1) = "Plan2"
strSheet(2) = "Plan3"
ctrl(0) = "ComboBox1"
ctrl(1) = "outrocontrole"
ctrl(2) = "outrocontrole"
For Z = 0 To 2

    Set ws = ThisWorkbook.Worksheets(strSheet(Z))

With ws

    ncell = .Cells(.Rows.Count, "A").End(xlUp).Row
    Me.Controls(ctrl(Z)).Clear

    For y = 1 To ncell
        Set c = .Cells(y, 1)
        Set Rng = .Range(.Cells(2, 1), .Cells(y, 1))
        x = Application.WorksheetFunction.CountIf(Rng, c)
        If x = 1 Then Me.Controls(ctrl(Z)).AddItem c
    Next y
    End With
    Exit Sub
Next[/code/

jrs

09-10-2022 11:52:53

Reinaldo escreveu: ↑Dom Out 09, 2022 8:44 am Algo +/- assim:

Código: Selecionar todos

Dim x As Long, ncell As Long, Z As Integer
Dim Rng As Range, c As Range
Dim ws As Worksheet
'Define matriz com planilhas e controles elegiveis
Dim strSheet(2) As String, ctrl(2) As String
strSheet(0) = "Plan1"
strSheet(1) = "Plan2"
strSheet(2) = "Plan3"
ctrl(0) = "ComboBox1"
ctrl(1) = "outrocontrole"
ctrl(2) = "outrocontrole"
For Z = 0 To 2

    Set ws = ThisWorkbook.Worksheets(strSheet(Z))

With ws

    ncell = .Cells(.Rows.Count, "A").End(xlUp).Row
    Me.Controls(ctrl(Z)).Clear

    For y = 1 To ncell
        Set c = .Cells(y, 1)
        Set Rng = .Range(.Cells(2, 1), .Cells(y, 1))
        x = Application.WorksheetFunction.CountIf(Rng, c)
        If x = 1 Then Me.Controls(ctrl(Z)).AddItem c
    Next y
    End With
    Exit Sub
Next[/code/
[/quote]

[code]

Dim x As Long, ncell As Long, Z As Integer
Dim Rng As Range, c As Range
Dim ws As Worksheet
'Define matriz com planilhas e controles elegiveis
Dim strSheet(2) As String, ctrl(2) As String
strSheet(0) = "Planilha2"
strSheet(1) = "Planilha3"
strSheet(2) = "Planilha4"
ctrl(0) = "TextBox4"
ctrl(1) = "ComboBox1"
ctrl(2) = "ComboBox2"
For Z = 0 To 2

    Set ws = ThisWorkbook.Worksheets(strSheet(Z))

With ws

    ncell = .Cells(.Rows.Count, "A").End(xlUp).Row
    Me.Controls(ctrl(Z)).Clear

    For y = 1 To ncell
        Set c = .Cells(y, 1)
        Set Rng = .Range(.Cells(2, 1), .Cells(y, 1))
        x = Application.WorksheetFunction.CountIf(Rng, c)
        If x = 1 Then Me.Controls(ctrl(Z)).AddItem c
    Next y
    End With
    Exit Sub
Next

Deu essse erro da imagem
q1.png

Reinaldo

10-10-2022 17:20:17

Não fiz a rotina para uso direto, somente uma ideia geral de como pode ser. (por isso o uso de "Algo +/-").
Deve ser adaptada a sua relalidade, coisa que desconhecemos

jrs

10-10-2022 20:50:11

O objetivo é enviar as informações das Coluna A, de cada planilha em seu respectivo ComboBox e ListBox

Segue imagem para demonstração.

jrs

10-10-2022 20:54:33

Para o campo de Fornecedor não vou precisar, achei outra forma de ser feito, com auto complete, só para os demais que não estou conseguindo... puxar a informação de cada Coluna A de sua planilha para seu respectivo Box

Ou seja, para o campo de

Comprador (Planilha3 é o ComboBox1);
Recebedor (Planilha4 é o ComboBox2);
Listagem de Intercorrências (Planilha5 é o ListBox1)
Unidade Requisitante (Planilha6 é o ListBox2)