Working with different sheets

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

Wondering if you someone could help me out:

Scenario:

Two sheets:

Sheets(1) = OutputSheet
Sheets(3) = SourceSheet



I am using WorksheetFunction.Vlookup to look-up the rows of Sheets(1) Col.B into Sheets(3) Col. The row from Sheet(1) Col.B is concatenate with a variable called GetPeriod. For example:

Getperiod = 01.01.2020

Row Value = Bang

Result: Bang_1/01/2020

Using the Result Bang_1/01/2020 look up to Sheets(3) Col. A and if it exists, then count how many times this value is showing in the Sheets(3) Col. A

Col.A

Bang_1/01/2020

Bang_1/02/2020

So the result showing in Sheets(1). Col.B should be 2

Col. B

2

I am doing a nested loop to get the repeated items from sheet3 but even specifying the Sheet(3).Cells(i,1) I cannot get i :( ?

I am getting lost between the two sheets!!! Any help please!



Here is the code:

VBA Code:
Option Explicit


Public Function GetPeriod() As Variant


    GetPeriod = ThisWorkbook.Sheets(1).Range("a2").Value 'Variable to keep the period for analysis


End Function



Sub GetValueFrom ()


    Dim SourceLastRow As Long

    Dim OutputLastRow As Long

    Dim SourceSheet As Worksheet

    Dim OutputSheet As Worksheet

    Dim i As Integer, r As Integer, VLK As Variant

    Dim Cell As Range


    'Worksheet names:


    Set SourceSheet = Worksheets(3)

    Set OutputSheet = Worksheets(1)


 

    'Determine last row of source


    With SourceSheet


        SourceLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row


    End With



    With OutputSheet


      OutputLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row  'Determine last row in col OutPutSheet


         r = 6 'Vlookup row start at 6 position


         .Range("D6:D" & OutputLastRow).ClearContents 'Deleting previous records everytime the variable  GetPeriod is changed


      

      For Each Cell In .Range("B6:B" & OutputLastRow) 'Looping through Outputsheet


           On Error Resume Next ' if value is not found to avoid error 1004

    


           With SourceSheet


               For i = 2 To .Range("A" & SourceLastRow)                  'Looping through SourceSheet to count values


                  If .Cells(i, 1).Value <> .Cells(i + 1, 1).Value Then        'if row n is different from (n+1) count


                    VLK = WorksheetFunction.Vlookup(Cells(r, 2) & "_" & GetPeriod, Sheets(3). _


                     Range("a1:a" & SourceLastRow).Value, 1, 0)


                     .Cells(r, 4).Value = WorksheetFunction.Count(VLK)        'write the value in Outputsheet


                 Else


                    VLK = VLK + 1 ' if row n is igual to (n+1) accumulate value before writing it to Outputsheet


                     i = i + 1


                  End If


               Next


             End With


                   r = r + 1 'Move to the next role in Outputsheet


        Next


    End With


End Sub
 
Last edited by a moderator:
Hi @DanteAmor,

Sorry to bug you again with the same question, but wondering if you could help me out, your code works perfectly with the example that I gave you, my mistake was not to give you the exact sheet format that I have, so I am trying to adjust your code to my original sheet but without success o_O.

In my original file I have to join two ranges: A and B in sheet1 like you did in yours, however range B is just one row (B2):

Range A: sheets(1)
Syspro Supplier Code
AVI02
PRI05
KLX01
KLX02
QUA03
LAM02
PPG01
HEX01
CYT04
ALC01
INT04
LOV01
AER03
FOR02
WES02
APP05
HEN01
INT07
INT08
RUB01
TEN02
CEM01
Range B: sheets(1)
Jan-20


After joining these two ranges in sheet1 I can follow your logic using the Countif with the 3rd range in sheet3:

Range C in sheets(3)
Lookup Value
APP05_1/02/2020
AVI02_1/01/2020
AVI02_1/02/2020
AVI02_1/02/2020
AVI02_1/02/2020
CEM01_1/02/2020
INT07_1/02/2020
KLX02_1/01/2020
KLX02_1/01/2020
KLX02_1/01/2020
KLX02_1/01/2020
KLX02_1/01/2020
LAM02_1/01/2020
LOV01_1/02/2020
RUB01_1/02/2020


Could you please help me to adjust your sub to capture this scenario?

Here is what I have tried:

VBA Code:
Sub CountValues()

Dim Range_A As Range
Dim Range_B As Range
Dim Range_C As Range 'Union btw A and B
Dim LastRow As Long

LastRow = Sheets(1).Range("B6:B" & Rows.Count).End(xlDown).row

Set Range_A = Sheets(1).Range("B6:B" & LastRow)
Set Range_B = Sheets(1).Range("A2")

Range_C = Union(Range_A, Range_B).Select

  With Range_C
    .Formula = "=COUNTIF(Sheet3!B:B,Range_C&""*"")"
    .Value = .Value
  End With
  
End Sub
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try this:

VBA Code:
Sub CountValues()
  With Sheets("Sheet1").Range("C2:C" & Sheets("Sheet1").Range("A" & Rows.Count).End(3).Row)
    .Formula = "=COUNTIF(Sheet3!C:C,A2&""_""&TEXT($B$2,""d/mm/yyyy""))"
    .Value = .Value
  End With
End Sub

Results:
Dante Amor
ABC
1Syspro Supplier CodeDateResult
2AVI0201-ene1
3PRI050
4KLX010
5KLX025
6QUA030
7LAM021
Sheet1


Dante Amor
ABC
1Lookup Value
2APP05_1/02/2020
3AVI02_1/01/2020
4AVI02_1/02/2020
5AVI02_1/02/2020
6AVI02_1/02/2020
7CEM01_1/02/2020
8INT07_1/02/2020
9KLX02_1/01/2020
10KLX02_1/01/2020
11KLX02_1/01/2020
12KLX02_1/01/2020
13KLX02_1/01/2020
14LAM02_1/01/2020
15LOV01_1/02/2020
16RUB01_1/02/2020
Sheet3
 
Upvote 0
Not sure what I am missing @DanteAmor o_O I cross-checked everything but the result is coming zero

Sheet 1 - Col A, B, C
Supplier CodeDateResult
AVI021/01/20190
PRI050
KLX010
KLX020
QUA030
LAM020
PPG010
HEX010
CYT040
ALC010
INT040
LOV010
AER030
FOR020
WES020
APP050
HEN010
INT070
INT080
RUB010
TEN020
CEM010

Sheet 3 - Col.C
Lookup Value
APP05_1/02/2020
AVI02_1/01/2020
AVI02_1/01/2020
AVI02_1/01/2020
AVI02_1/01/2020
CEM01_1/02/2020
INT07_1/02/2020
KLX02_1/01/2020
KLX02_1/01/2020
KLX02_1/01/2020
KLX02_1/01/2020
KLX02_1/01/2020
LAM02_1/01/2020
LOV01_1/02/2020
RUB01_1/02/2020

VBA Code:
Sub CountValues()
  With Sheets("Sheet1").Range("C2:C" & Sheets("Sheet1").Range("A" & Rows.Count).End(3).Row)
    .Formula = "=COUNTIF(Sheet3!C:C,A2 & ""_"" & TEXT($B$2,""d/mm/yyyy""))"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,414
Messages
6,119,373
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top