I have five sheets named 'Yearly,Q1,Q2,Q3,Q4'. currently my code only update one worksheet at one time into the mySql database. i want it to get all worksheets to be done at one go. i've tried to implement 'For loop' but it just run on the active worksheet only. is there anything need to be added into my coding to make it works? here's my code;
For Each wksSheet In wbkFirst.Worksheets
Do While IsEmpty(Cells(lastRowCom, "B").Value) = False
stockCodeExist = False
stockCodeCom = Trim(Cells(lastRowCom, "B").Value)
marketCapCom = Trim(Cells(lastRowCom, "C").Value)
revenueCom = Trim(Cells(lastRowCom, "D").Value)
TotLiability = Trim(Cells(lastRowCom, "E").Value)
curLiability = Trim(Cells(lastRowCom, "F").Value)
tradeReceivable = Trim(Cells(lastRowCom, "G").Value)
inventory = Trim(Cells(lastRowCom, "H").Value)
shareholderEqyCom = Trim(Cells(lastRowCom, "I").Value)
totLiabToSE = Trim(Cells(lastRowCom, "J").Value)
currentAss = Trim(Cells(lastRowCom, "K").Value)
fixedAsset = Trim(Cells(lastRowCom, "L").Value)
operatingExpenses = Trim(Cells(lastRowCom, "M").Value)
ClosingPrice = Trim(Cells(lastRowCom, "N").Value)
priceEarningRatio = Trim(Cells(lastRowCom, "O").Value)
earningPerShare = Trim(Cells(lastRowCom, "P").Value)
salesToTotalAss = Trim(Cells(lastRowCom, "Q").Value)
totalAss = Trim(Cells(lastRowCom, "R").Value)
operatingCashFlow = Trim(Cells(lastRowCom, "S").Value)
peRatio = Trim(Cells(lastRowCom, "T").Value)
altmanZ = Trim(Cells(lastRowCom, "U").Value)
cashNeqvalent = Trim(Cells(lastRowCom, "V").Value)
EbitCom = Trim(Cells(lastRowCom, "W").Value)
sharesOutstdgCom = Trim(Cells(lastRowCom, "X").Value)
netAttributableCom = Trim(Cells(lastRowCom, "Y").Value)
incStock = Trim(Cells(lastRowCom, "Z").Value)
netInvest = Trim(Cells(lastRowCom, "AA").Value)
retainedPOL = Trim(Cells(lastRowCom, "AB").Value)
ReOTA = Trim(Cells(lastRowCom, "AC").Value)
ReOE = Trim(Cells(lastRowCom, "AD").Value)
AvergShares = Trim(Cells(lastRowCom, "AE").Value)
netPOL = Trim(Cells(lastRowCom, "AF").Value)
DebtEqy = Trim(Cells(lastRowCom, "AG").Value)
paidUpCapital = Trim(Cells(lastRowCom, "AH").Value)
fiyeMonth = Trim(Cells(lastRowCom, "AI").Value)
yearIdCom = Cells(1, 2)
periodIdCom = Cells(1, 3)
'check company financial exist
Set rs = New ADODB.Recordset
sqlStr = "SELECT year_id, period_id, stock_code FROM company_financial WHERE year_id = '" & yearIdCom & "' AND period_id = '" & periodIdCom & "' AND stock_code = '" & stockCodeCom & "' "
rs.Open sqlStr, conn, adOpenStatic
If rs.EOF Then
'record not exist in DB
recordExistCom = False
Else
stockCodeCom = rs.Fields("stock_code")
recordExistCom = True
End If
rs.Close
Set rs = Nothing
If recordExistCom = False Then
'insert new company
If stockCodeCom <> "" Then
sqlStr = "INSERT INTO company_financial "
sqlStr = sqlStr & "(year_id, period_id, stock_code, receivable, revenue, total_liabilities, shareholders_equity, total_liabilities_to_shareholders_equity, current_liabilities, total_current_assets, net_attributable, inventories, fixed_assets, operating_cost, net_cash_operation, price_close, pe, eps, pe_relative_sector, altman_z_score, paid_up_capital, net_profit_or_loss, dec_stock, dec_debtors, inc_creditors, net_investments, cash_and_equivalents, retained_profit_or_loss, ebit, mkt_cap, sales_to_assets, rota, roe, debt_to_equity, average_shares, total_assets, shares_outstanding, report_period_end_mth,"
sqlStr = sqlStr & "created_by,created_date)"
sqlStr = sqlStr & " VALUES "
sqlStr = sqlStr & " ('" & yearIdCom & "', '" & periodIdCom & "', '" & stockCodeCom & "', '" & tradeReceivable & "', '" & revenueCom & "', '" & TotLiability & "', '" & shareholderEqyCom & "', '" & totLiabToSE & "', '" & curLiability & "', '" & currentAss & "', '" & netAttributableCom & "', '" & inventory & "', '" & fixedAsset & "', '" & operatingExpenses & "', '" & operatingCashFlow & "', '" & ClosingPrice & "', '" & priceEarningRatio & "', '" & earningPerShare & "', '" & peRatio & "', '" & altmanZ & "', '" & paidUpCapital & "', '" & netPOL & "', '" & incStock & "', '" & netInvest & "', '" & netInvest & "', '" & netInvest & "', '" & cashNeqvalent & "', '" & retainedPOL & "', '" & EbitCom & "', '" & marketCapCom & "', '" & salesToTotalAss & "', '" & ReOTA & "', '" & ReOE & "', '" & DebtEqy & "', '" & AvergShares & "', '" & totalAss & "', '" & sharesOutstdgCom & "', '" & fiyeMonth & "',"
sqlStr = sqlStr & "'fistconv','" & Format(Now(), "yyyy-MM-dd HH:mm") & "')"
conn.Execute sqlStr
End If
lastRow = lastRow + 1
Range("C3").Value = lastRow - 5
Else
sqlStr = "UPDATE company_financial SET "
sqlStr = sqlStr & " receivable = '" & CDbl(tradeReceivable) & "',"
sqlStr = sqlStr & " revenue = '" & CDbl(revenueCom) & "',"
sqlStr = sqlStr & " total_liabilities = '" & CDbl(TotLiability) & "',"
sqlStr = sqlStr & " shareholders_equity = '" & CDbl(shareholderEqyCom) & "',"
sqlStr = sqlStr & " total_liabilities_to_shareholders_equity = '" & CDbl(totLiabToSE) & "',"
sqlStr = sqlStr & " current_liabilities = '" & CDbl(curLiability) & "',"
sqlStr = sqlStr & " total_current_assets = '" & currentAss & "',"
sqlStr = sqlStr & " net_attributable = '" & netAttributableCom & "',"
sqlStr = sqlStr & " inventories = '" & inventory & "',"
sqlStr = sqlStr & " fixed_assets = '" & fixedAsset & "',"
sqlStr = sqlStr & " operating_cost = '" & operatingExpenses & "',"
sqlStr = sqlStr & " net_cash_operation = '" & operatingCashFlow & "',"
sqlStr = sqlStr & " price_close = '" & ClosingPrice & "',"
sqlStr = sqlStr & " pe = '" & priceEarningRatio & "',"
sqlStr = sqlStr & " eps = '" & earningPerShare & "',"
sqlStr = sqlStr & " pe_relative_sector = '" & peRatio & "',"
sqlStr = sqlStr & " altman_z_score = '" & altmanZ & "',"
sqlStr = sqlStr & " paid_up_capital = '" & paidUpCapital & "',"
sqlStr = sqlStr & " net_profit_or_loss = '" & netPOL & "',"
sqlStr = sqlStr & " dec_stock = '" & incStock & "',"
sqlStr = sqlStr & " dec_debtors = '" & netInvest & "',"
sqlStr = sqlStr & " inc_creditors = '" & netInvest & "',"
sqlStr = sqlStr & " net_investments = '" & netInvest & "',"
sqlStr = sqlStr & " cash_and_equivalents = '" & cashNeqvalent & "',"
sqlStr = sqlStr & " retained_profit_or_loss = '" & retainedPOL & "',"
sqlStr = sqlStr & " ebit = '" & EbitCom & "',"
sqlStr = sqlStr & " mkt_cap = '" & marketCapCom & "',"
sqlStr = sqlStr & " sales_to_assets = '" & salesToTotalAss & "',"
sqlStr = sqlStr & " rota = '" & ReOTA & "',"
sqlStr = sqlStr & " roe = '" & ReOE & "',"
sqlStr = sqlStr & " debt_to_equity = '" & DebtEqy & "',"
sqlStr = sqlStr & " average_shares = '" & AvergShares & "',"
sqlStr = sqlStr & " total_assets = '" & totalAss & "',"
sqlStr = sqlStr & " shares_outstanding = '" & sharesOutstdgCom & "',"
sqlStr = sqlStr & " report_period_end_mth = '" & fiyeMonth & "',"
sqlStr = sqlStr & " update_by = 'fistconv',"
sqlStr = sqlStr & " update_date = '" & Format(Now(), "yyyy-MM-dd HH:mm") & "'"
sqlStr = sqlStr & " WHERE year_id = '" & yearIdCom & "' AND period_id = '" & periodIdCom & "' AND stock_code = '" & stockCodeCom & "'"
Debug.Print sqlStr
conn.Execute sqlStr
End If
lastRowCom = lastRowCom + 1
Range("E3").Value = lastRowCom - 5
Loop
Next wksSheet
thanks in advance for your help guys :)