Sim, é básico, mas satisfaz. Pelo menos àqueles que têm pouca experiência.
No código abaixo aprenderemos como usar o looping num range de células, extraindo deste os resultados desejados, transferindo-os a outra planilha.
Exemplo:
Tenho uma lista começando com uma data na primeira planilha e quero percorrê-la verificando se o mês e o ano da data na primeira coluna da lista, combinam com o mês e o ano da segunda planilha. Se assim for, a informação no resto da linha da data correspondente deve ser copiada para trabalho 2.
Há um download da planilha com alguns dados de exemplo aqui.
É possível ajustar qual dos dados são transferidos para as colunas alterando as variáveis no início.
Sub ProcessRangeData()Dim wsSource As WorksheetDim wsDestination As WorksheetDim rowSourceStart As LongDim colSourceDate As LongDim rSource As LongDim colDestStart As LongDim rowDestMonthYear As LongDim cDest As LongDim destBlankColumnCount As IntegerDim colDestRegion As IntegerDim colDestAccountName As IntegerDim colDestPotentialName As IntegerDim colDestAmount As IntegerDim colDestWeightedAmount As IntegerDim colSourceRegion As IntegerDim colSourceAccountName As IntegerDim colSourcePotentialName As IntegerDim colSourceAmount As IntegerDim colSourceWeightedAmount As IntegerSet wsSource = Sheet1Set wsDestination = Sheet2'Destination column offsets from month columncolDestRegion = 0colDestAccountName = 1colDestPotentialName = 2colDestAmount = 3colDestWeightedAmount = 4'Source columnscolSourceRegion = 5colSourceAccountName = 3colSourcePotentialName = 4colSourceAmount = 6colSourceWeightedAmount = 7colSourceDate = 2 'Source column for daterowSourceStart = 3 'Source starting rowrowDestMonthYear = 2 'Destination row to check for month & year matchingrSource = rowSourceStart'loop until the date field on the source sheet is blankDo While wsSource.Cells(rSource, colSourceDate).Value <> ""cDest = 1destBlankColumnCount = 0'loop through the destination columns until we've seen 5 blanks'(only 3 are ever expected)Do Until destBlankColumnCount > 5If wsDestination.Cells(rowDestMonthYear, cDest).Value <> "" ThendestBlankColumnCount = 0'check if month matchesIf Month(wsSource.Cells(rSource, colSourceDate).Value) = wsDestination.Cells(rowDestMonthYear, cDest).Value Then'check if year matchesIf Year(wsSource.Cells(rSource, colSourceDate).Value) = wsDestination.Cells(rowDestMonthYear, (cDest + 1)).Value Then'copy field datawsDestination.Cells((rowDestMonthYear + 2), (cDest + colDestAccountName)).Value = wsSource.Cells(rSource, colSourceAccountName).ValuewsDestination.Cells((rowDestMonthYear + 2), (cDest + colDestPotentialName)).Value = wsSource.Cells(rSource, colSourcePotentialName).ValuewsDestination.Cells((rowDestMonthYear + 2), (cDest + colDestRegion)).Value = wsSource.Cells(rSource, colSourceRegion).ValuewsDestination.Cells((rowDestMonthYear + 2), (cDest + colDestAmount)).Value = wsSource.Cells(rSource, colSourceAmount).ValuewsDestination.Cells((rowDestMonthYear + 2), (cDest + colDestWeightedAmount)).Value = wsSource.Cells(rSource, colSourceWeightedAmount).ValueEnd IfEnd IfElsedestBlankColumnCount = destBlankColumnCount + 1End IfcDest = cDest + 1LooprSource = rSource + 1LoopEnd Sub
Tags: Range, Excel, VBA, copy, worksheet,
Nenhum comentário:
Postar um comentário