how to copy dynamic data from one Sheet to another with criteria in Excel with VBA
I need to copy the names from the resulting filtered content of the sheet POSTO X to the corresponding sheet day, but the copy must follow these specific criteria:
if column day is SN, then the name goes to Servico Diurno section, SN to Servico Noturno, and PL must be on both Servico Diurno e Servico Noturno…
My problem is to copy only the required data to the destination, this code is filtering correctly, but I can’t figure out how to copy just what I need in this range to the destination…
Public Sub UpdateExchangesBook()
Dim wsCt As Worksheet, wsMD As Worksheet, wsOrg As Worksheet, wsDst As Worksheet, rgDC As Range, rgDataEval As Range, rgDataFlt As Range, rgDsCel As Range, cM As Byte, cD As Byte, CalcCL As Byte, strStNm As String
With ThisWorkbook
Set wsCt = .Sheets(“Dados Gerais”)
Set wsMD = .Sheets(“TOTALIZAÇÃO”)
Set rgDC = wsMD.Range(wsMD.Range(“B1”), wsMD.Range(“B1”).End(xlToRight)).Cells
cM = wsCt.Range(“B2”).Value
CalcCL = 3
For Each x In rgDC
cD = Day(x)
If cM = 1 Then
If cD > 30 Then Exit For
cD = cD + 1
End If
strStNm = cD
If cM = 12 Then
If cD > 31 Then
strStNm = cD & “J”
End If
End If
Set wsDst = .Sheets(strStNm)
Set wsOrg = .Sheets(“POSTO A”)
CalcCL = CalcCL + 1
wsDst.Range(“A8:B27, A31:B50”).ClearContents
With wsOrg
.Unprotect “101”
If .AutoFilterMode Then .AutoFilterMode = False
Set rgDataEval = .Range(“A1:” & Cells(33, CalcCL).Address)
rgDataEval.AutoFilter Field:=CalcCL, Criteria1:=”S?”, Operator:=xlOr, Criteria2:=”PL”
Set rgDataFlt = .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Offset(2, 0)
‘ this part is not generating the result as intended
If rgDataFlt.Rows.Count > 0 Then
For Each Z In rgDataFlt.Columns(CalcCL)
If StrComp(Z.Text, “SD”, vbTextCompare) Then
Set rgDsCel = wsDst.Range(“A27”).End(xlUp).Offset(1, 0)
rgDsCel.Value = rgDataFlt.Columns(1)
Set rgDsCel = wsDst.Range(“B27”).End(xlUp).Offset(1, 0)
rgDsCel.Value = Right(wsOrg.Range(“A1”).Value, 1)
End If
If StrComp(Z.Text, “SN”, vbTextCompare) Then
Set rgDsCel = wsDst.Range(“A50”).End(xlUp).Offset(1, 0)
rgDsCel.Value = rgDataFlt.Columns(1)
Set rgDsCel = wsDst.Range(“B50”).End(xlUp).Offset(1, 0)
rgDsCel.Value = Right(wsOrg.Range(“A1”).Value, 1)
End If
If StrComp(Z.Text, “PL”, vbTextCompare) Then
Set rgDsCel = wsDst.Range(“A27”).End(xlUp).Offset(1, 0)
rgDsCel.Value = rgDataFlt.Columns(1)
Set rgDsCel = wsDst.Range(“B27”).End(xlUp).Offset(1, 0)
rgDsCel.Value = Right(wsOrg.Range(“A1”).Value, 1)
Set rgDsCel = wsDst.Range(“A50”).End(xlUp).Offset(1, 0)
rgDsCel.Value = rgDataFlt.Columns(1)
Set rgDsCel = wsDst.Range(“B50”).End(xlUp).Offset(1, 0)
rgDsCel.Value = Right(wsOrg.Range(“A1”).Value, 1)
End If
Next
End If
.AutoFilterMode = False
.Protect “101”
End With
Next
End With
End Sub
Any help would be greatly appreciated!
I need to copy the names from the resulting filtered content of the sheet POSTO X to the corresponding sheet day, but the copy must follow these specific criteria: if column day is SN, then the name goes to Servico Diurno section, SN to Servico Noturno, and PL must be on both Servico Diurno e Servico Noturno…My problem is to copy only the required data to the destination, this code is filtering correctly, but I can’t figure out how to copy just what I need in this range to the destination… Public Sub UpdateExchangesBook()
Dim wsCt As Worksheet, wsMD As Worksheet, wsOrg As Worksheet, wsDst As Worksheet, rgDC As Range, rgDataEval As Range, rgDataFlt As Range, rgDsCel As Range, cM As Byte, cD As Byte, CalcCL As Byte, strStNm As String
With ThisWorkbook
Set wsCt = .Sheets(“Dados Gerais”)
Set wsMD = .Sheets(“TOTALIZAÇÃO”)
Set rgDC = wsMD.Range(wsMD.Range(“B1”), wsMD.Range(“B1”).End(xlToRight)).Cells
cM = wsCt.Range(“B2”).Value
CalcCL = 3
For Each x In rgDC
cD = Day(x)
If cM = 1 Then
If cD > 30 Then Exit For
cD = cD + 1
End If
strStNm = cD
If cM = 12 Then
If cD > 31 Then
strStNm = cD & “J”
End If
End If
Set wsDst = .Sheets(strStNm)
Set wsOrg = .Sheets(“POSTO A”)
CalcCL = CalcCL + 1
wsDst.Range(“A8:B27, A31:B50”).ClearContents
With wsOrg
.Unprotect “101”
If .AutoFilterMode Then .AutoFilterMode = False
Set rgDataEval = .Range(“A1:” & Cells(33, CalcCL).Address)
rgDataEval.AutoFilter Field:=CalcCL, Criteria1:=”S?”, Operator:=xlOr, Criteria2:=”PL”
Set rgDataFlt = .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Offset(2, 0)
‘ this part is not generating the result as intended
If rgDataFlt.Rows.Count > 0 Then
For Each Z In rgDataFlt.Columns(CalcCL)
If StrComp(Z.Text, “SD”, vbTextCompare) Then
Set rgDsCel = wsDst.Range(“A27”).End(xlUp).Offset(1, 0)
rgDsCel.Value = rgDataFlt.Columns(1)
Set rgDsCel = wsDst.Range(“B27”).End(xlUp).Offset(1, 0)
rgDsCel.Value = Right(wsOrg.Range(“A1”).Value, 1)
End If
If StrComp(Z.Text, “SN”, vbTextCompare) Then
Set rgDsCel = wsDst.Range(“A50”).End(xlUp).Offset(1, 0)
rgDsCel.Value = rgDataFlt.Columns(1)
Set rgDsCel = wsDst.Range(“B50”).End(xlUp).Offset(1, 0)
rgDsCel.Value = Right(wsOrg.Range(“A1”).Value, 1)
End If
If StrComp(Z.Text, “PL”, vbTextCompare) Then
Set rgDsCel = wsDst.Range(“A27”).End(xlUp).Offset(1, 0)
rgDsCel.Value = rgDataFlt.Columns(1)
Set rgDsCel = wsDst.Range(“B27”).End(xlUp).Offset(1, 0)
rgDsCel.Value = Right(wsOrg.Range(“A1”).Value, 1)
Set rgDsCel = wsDst.Range(“A50”).End(xlUp).Offset(1, 0)
rgDsCel.Value = rgDataFlt.Columns(1)
Set rgDsCel = wsDst.Range(“B50”).End(xlUp).Offset(1, 0)
rgDsCel.Value = Right(wsOrg.Range(“A1”).Value, 1)
End If
Next
End If
.AutoFilterMode = False
.Protect “101”
End With
Next
End With
End Sub Any help would be greatly appreciated!should be populated with 3sCamila And only on Serviço Diurno and the other names on both Servico Diurno e NortunoThis is the source dataFilter applied on the that should match the destination day sheet ( in this case #2) Read More