OzGrid

How to use VBA to read data from one worksheet and copy to another formatted one

< Back to Search results

 Category: [Excel]  Demo Available 

How to use VBA to read data from one worksheet and copy to another formatted one

 

Requirement:

 

The user is asking for some help here and needs a Report in tabular format (sheet2) i.e.

 

Vendor Company Code Name City Assignment DocumentNo Type Doc..Date Amount in Local Crcy LCurr Text Reference


To populated this report the user needs to read the data from sheet2, however the data is formatted as below:

Vendor       1000              
Company Code       TA10              
                       
Name       Grape Supplie            
City       Sydney              
                       
                       
    St Assignment   DocumentNo Type Doc..Date Amount in Local Crcy LCurr Text Reference
                       
      1514   1500000136 KZ 10.08.2018 50 NZD   TEST
      1073   1500000137 KZ 14.08.2018 51 NZD   TEST_TRAD
      1521   1500000138 KZ 14.08.2018 52 NZD   DDSDF1
      1905   1500000139 KZ 14.08.2018 53 NZD   DDSDF1
      1541   1500000140 KZ 14.08.2018 54 NZD    
      1331   5100000305 RE 20.08.2018 55 NZD   4500000776
      1055   5100000306 RE 20.08.2018 56 NZD   4500000777
      1609   5100000075 RE 17.05.2018 57 NZD   7500000146INV
      1548   5100000077 RE 17.05.2018 58 NZD   7500000148INV
      1209   5100000087 RE 18.05.2018 59 NZD   7500000183INV
      1794   5100000090 RE 18.05.2018 60 NZD   7500000184CRDM
      1343   5100000089 RE 18.05.2018 61 NZD   7500000184INV
      1251   5100000092 RE 28.05.2018 62 NZD   7500000186CRDM
      1915   5100000091 RE 18.05.2018 63 NZD   7500000186INV
      1782   5100000094 RE 18.05.2018 64 NZD   7500000188CRDM
      1018   5100000093 RE 18.05.2018 65 NZD   7500000188INV

 

Solution:

 

In a Module:

Code:
Sub Main()
  Dim VendorCell As Range, SearchRange As Range
  Dim FirstVendorCell As String
  Dim ws1 As Worksheet, ws2 As Worksheet
  Dim r2 As Range, i As Long
  
  Set ws1 = Worksheets("Sheet1")
  Set ws2 = Worksheets("Sheet2")
  
  Set SearchRange = ws1.Range("A1", ws1.Cells(Rows.Count, "A").End(xlUp))
  Set VendorCell = SearchRange.Find("Vendor", MatchCase:=True)

  If VendorCell Is Nothing Then
    MsgBox "No Vendor was found"
    Exit Sub
  Else
    FirstVendorCell = VendorCell.Address
    Do
      Set r2 = ws2.Cells(Rows.Count, "A").End(xlUp).Offset(1)
      i = 1
      If VendorCell(, 5).Value = "*" Then Exit Sub 'Vendor, ws1 Column E
      Do While VendorCell(9 + i, 11).Value <> ""
        r2(i).Value = VendorCell(, 5).Value 'Vendor, ws1 Column E
        r2(i, 2).Value = VendorCell(9 + i, 15).Value 'Reference, ws1 Column O
        r2(i, 3).Value = VendorCell(9 + i, 11).Value '$, ws1 Column K
        r2(i, 4).Value = VendorCell(9 + i, 8).Value 'Date, ws1 Column H
        i = i + 1
      Loop
      Set VendorCell = SearchRange.FindNext(VendorCell)
    Loop While VendorCell.Address <> FirstVendorCell
  End If
End Sub

 

Obtained from the OzGrid Help Forum.

Solution provided by Kenneth Hobson.

 

See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets

 

See also:

How to use VBA to find and replace or If/Then with conditional formatting
How to auto populate blank cells using VBA
How to skip VBA Code if table filter returns nothing
How to use VBA - Split Worksheets and Display SaveAs Prompt

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)