nialcooper
New Member
- Joined
- Mar 14, 2022
- Messages
- 17
- Office Version
- 365
- Platform
- Windows
Hi I am trying to populate an excel sheet with data using the barcode from a book, i have found a code that works as a button assigned macro and for all purposes it works well. the problem i have with it is that when it runs it performs a search in google books for every line in the sheet which dramatically slows it down when you get to around 40+ lines, i don't know enough about coding to edit tis code so that it will only perform the search on rows that have not already been populated. here is the code
here is a snip of the work sheet, i would post the worksheet but it is a mess of code that dosn't apply to the sheet i am working with. currently whenever i press the button top right it will perform a search on all rows, iwould like for it to search on lines that do not have data in column B.is this possible? Thanks in advance for looking
VBA Code:
Public Sub isbnExample()
Dim dset As cDataSet
' get ISBN book data
' load to a dataset
Set dset = New cDataSet
With dset
' create a dataset from the isbn worksheet
.populateData wholeSheet("isbn"), , "isbn", , , , True
If .where Is Nothing Then
MsgBox ("No data to process")
Else
'check we have the isbn column present
If .headingRow.validate(True, cISBNColumnHeading) Then
' if there were any updates then commit them
If processISBN(dset) > 0 Then
.bigCommit
End If
End If
End If
End With
Set dset = Nothing
End Sub
Private Function processISBN(ds As cDataSet) As Long
Dim dset As cDataSet, cb As cBrowser, dr As cDataRow
Dim jo As cJobject, job As cJobject, n As Long
Dim sWire As String
' gets a book details by isbn number
Set cb = New cBrowser
Set jo = New cJobject
n = 0
For Each dr In ds.rows
With dr.cell(cISBNColumnHeading)
sWire = cb.httpGET(cISBNGoogleBooks & .toString)
Set job = jo.deSerialize(sWire)
If Not job.isValid Then
MsgBox ("Badly formed jSon returned for ISBN" & .toString & "-" _
& sWire)
ElseIf Not job.childExists("error") Is Nothing Then
MsgBox ("Google books refuses to co-operate for ISBN " _
& .toString _
& "-" & job.child("error").serialize)
ElseIf job.childExists("Items") Is Nothing _
Or job.childExists("totalItems") Is Nothing Then
MsgBox ("Could find no data for ISBN " _
& .toString _
& "-" & job.serialize)
ElseIf job.child("totalItems").value <> 1 Then
MsgBox ("Multiple entries for " _
& .toString _
& "-" & job.child("totalItems").serialize)
Else
' fill in this row
n = n + rowProcess(dr, job.child("Items"))
End If
End With
Next dr
processISBN = n
End Function
Private Function rowProcess(dr As cDataRow, job As cJobject) As Long
Dim hc As cCell, n As Long, jo As cJobject, jom As cJobject
n = 0
For Each hc In dr.parent.headings
' any headings that are present in the dataset
' and also in the returned json get populated
With hc
If .toString <> cISBNColumnHeading Then
Set jo = job.find(.toString)
If Not jo Is Nothing Then
With dr.cell(.column)
' if multiple then include the array separated by commas
If jo.isArrayRoot Then
.value = vbNullString
If jo.hasChildren Then
For Each jom In jo.children
If .toString <> vbNullString Then
.value = .value & ","
End If
.value = .value & jom.toString
Next jom
End If
Else
.value = jo.value
End If
End With
n = n + 1
End If
End If
End With
Next hc
rowProcess = n
End Function
here is a snip of the work sheet, i would post the worksheet but it is a mess of code that dosn't apply to the sheet i am working with. currently whenever i press the button top right it will perform a search on all rows, iwould like for it to search on lines that do not have data in column B.is this possible? Thanks in advance for looking