Página 1 de 1

Copiar e colar baseado em 3 condições diferentes

Enviado: 20 Mar 2017 às 16:31
por Krebs
Preciso de alguma formar copiar os itens de uma planilha para a outra levando em consideração 3 condições diferentes.

O que fiz foi:
Código: Selecionar todos
Do While Not IsEmpty(Sheets("Base").Cells(linha, colunaTime))
            If Sheets("Base").Cells(linha, colunaProjeto).Value = ComboBoxProjeto.Value Then
                If CDate(Sheets("Base").Cells(linha, colunaAtualização).Value) = CDate(ComboBoxAtualização.Value) Then
                    If DatePart("h", CDate(Sheets("Base").Cells(linha, colunaTime).Value)) = DatePart("h", CDate(ComboBoxTime.Value)) Then
                        Sheets("Report").Range("M15").Value = Sheets("Base").Cells(linha, colunaDP).Value
                        Sheets("Report").Range("M10").Value = Sheets("Base").Cells(linha, colunaDR).Value
                        Sheets("Report").Range("Q15").Value = Sheets("Base").Cells(linha, colunaMdP).Value
                        Sheets("Report").Range("Q10").Value = Sheets("Base").Cells(linha, colunaMdR).Value
                        Sheets("Report").Range("U15").Value = Sheets("Base").Cells(linha, colunaAP).Value
                        Sheets("Report").Range("U10").Value = Sheets("Base").Cells(linha, colunaAR).Value
                        Sheets("Report").Range("Y15").Value = Sheets("Base").Cells(linha, colunaMP).Value
                        Sheets("Report").Range("Y10").Value = Sheets("Base").Cells(linha, colunaMR).Value
                        Sheets("Report").Range("AC15").Value = Sheets("Base").Cells(linha, colunaCP).Value
                        Sheets("Report").Range("AC10").Value = Sheets("Base").Cells(linha, colunaCR).Value
                        Sheets("Report").Range("C18").Value = Sheets("Base").Cells(linha, colunaObjetivo).Value
                        Sheets("Report").Range("Q18").Value = Sheets("Base").Cells(linha, colunaAtividadesRealizadas).Value
                        Sheets("Report").Range("C26").Value = Sheets("Base").Cells(linha, colunaProximasAtividades).Value
                        Sheets("Report").Range("Q26").Value = Sheets("Base").Cells(linha, colunaProblemasRiscosEncontrados).Value
                        Sheets("Report").Range("D6").Value = TextBoxID.Value
                        Sheets("Report").Range("F6").Value = ComboBoxProjeto.Value
                        Sheets("Report").Range("C5").Value = ComboBoxLider.Value
                    End If
                End If
            End If
            linha = linha + 1
        Loop
Porém está muito "pesado" o código e acaba travando o excel. Acham que tem algum outro jeito mais fácil de fazer?

Re: Copiar e colar baseado em 3 condições diferentes

Enviado: 21 Mar 2017 às 10:01
por babdallas
Veja se ajuda.
Código: Selecionar todos
Do While Not IsEmpty(Sheets("Base").Cells(linha, colunaTime))
            If Sheets("Base").Cells(linha, colunaProjeto).Value = ComboBoxProjeto.Value Then
                If CDate(Sheets("Base").Cells(linha, colunaAtualização).Value) = CDate(ComboBoxAtualização.Value) Then
                    If DatePart("h", CDate(Sheets("Base").Cells(linha, colunaTime).Value)) = DatePart("h", CDate(ComboBoxTime.Value)) Then
                        Sheets("Report").Range("M15").Value = Sheets("Base").Cells(linha, colunaDP).Value
                        Sheets("Report").Range("M10").Value = Sheets("Base").Cells(linha, colunaDR).Value
                        Sheets("Report").Range("Q15").Value = Sheets("Base").Cells(linha, colunaMdP).Value
                        Sheets("Report").Range("Q10").Value = Sheets("Base").Cells(linha, colunaMdR).Value
                        Sheets("Report").Range("U15").Value = Sheets("Base").Cells(linha, colunaAP).Value
                        Sheets("Report").Range("U10").Value = Sheets("Base").Cells(linha, colunaAR).Value
                        Sheets("Report").Range("Y15").Value = Sheets("Base").Cells(linha, colunaMP).Value
                        Sheets("Report").Range("Y10").Value = Sheets("Base").Cells(linha, colunaMR).Value
                        Sheets("Report").Range("AC15").Value = Sheets("Base").Cells(linha, colunaCP).Value
                        Sheets("Report").Range("AC10").Value = Sheets("Base").Cells(linha, colunaCR).Value
                        Sheets("Report").Range("C18").Value = Sheets("Base").Cells(linha, colunaObjetivo).Value
                        Sheets("Report").Range("Q18").Value = Sheets("Base").Cells(linha, colunaAtividadesRealizadas).Value
                        Sheets("Report").Range("C26").Value = Sheets("Base").Cells(linha, colunaProximasAtividades).Value
                        Sheets("Report").Range("Q26").Value = Sheets("Base").Cells(linha, colunaProblemasRiscosEncontrados).Value
                        Sheets("Report").Range("D6").Value = TextBoxID.Value
                        Sheets("Report").Range("F6").Value = ComboBoxProjeto.Value
                        Sheets("Report").Range("C5").Value = ComboBoxLider.Value
			exit do
                    End If
                End If
            End If
            linha = linha + 1
        Loop

Re: Copiar e colar baseado em 3 condições diferentes

Enviado: 21 Mar 2017 às 10:39
por Krebs
babdallas escreveu:Veja se ajuda.
Código: Selecionar todos
Do While Not IsEmpty(Sheets("Base").Cells(linha, colunaTime))
            If Sheets("Base").Cells(linha, colunaProjeto).Value = ComboBoxProjeto.Value Then
                If CDate(Sheets("Base").Cells(linha, colunaAtualização).Value) = CDate(ComboBoxAtualização.Value) Then
                    If DatePart("h", CDate(Sheets("Base").Cells(linha, colunaTime).Value)) = DatePart("h", CDate(ComboBoxTime.Value)) Then
                        Sheets("Report").Range("M15").Value = Sheets("Base").Cells(linha, colunaDP).Value
                        Sheets("Report").Range("M10").Value = Sheets("Base").Cells(linha, colunaDR).Value
                        Sheets("Report").Range("Q15").Value = Sheets("Base").Cells(linha, colunaMdP).Value
                        Sheets("Report").Range("Q10").Value = Sheets("Base").Cells(linha, colunaMdR).Value
                        Sheets("Report").Range("U15").Value = Sheets("Base").Cells(linha, colunaAP).Value
                        Sheets("Report").Range("U10").Value = Sheets("Base").Cells(linha, colunaAR).Value
                        Sheets("Report").Range("Y15").Value = Sheets("Base").Cells(linha, colunaMP).Value
                        Sheets("Report").Range("Y10").Value = Sheets("Base").Cells(linha, colunaMR).Value
                        Sheets("Report").Range("AC15").Value = Sheets("Base").Cells(linha, colunaCP).Value
                        Sheets("Report").Range("AC10").Value = Sheets("Base").Cells(linha, colunaCR).Value
                        Sheets("Report").Range("C18").Value = Sheets("Base").Cells(linha, colunaObjetivo).Value
                        Sheets("Report").Range("Q18").Value = Sheets("Base").Cells(linha, colunaAtividadesRealizadas).Value
                        Sheets("Report").Range("C26").Value = Sheets("Base").Cells(linha, colunaProximasAtividades).Value
                        Sheets("Report").Range("Q26").Value = Sheets("Base").Cells(linha, colunaProblemasRiscosEncontrados).Value
                        Sheets("Report").Range("D6").Value = TextBoxID.Value
                        Sheets("Report").Range("F6").Value = ComboBoxProjeto.Value
                        Sheets("Report").Range("C5").Value = ComboBoxLider.Value
			exit do
                    End If
                End If
            End If
            linha = linha + 1
        Loop
Caramba deu certo!!! Poderia me explicar exatamente o que faz o Exit Do?? Ele encerra o while assim que os três ifs são cumpridos?

Re: Copiar e colar baseado em 3 condições diferentes

Enviado: 21 Mar 2017 às 10:52
por babdallas
Isso mesmo. Se as 3 condições foram atendidas, então faz as ações e depois sai do DO (usando exit do). Não tem necessidade de continuar nele.