mismatch error when using sheets in array to populate data on userform

Mussala

Board Regular
Joined
Sep 28, 2022
Messages
87
Office Version
  1. 2019
Platform
  1. Windows
Hello ,
I try to populate data for all of sheets in listbox on userform , but shows mismatch error in this line
VBA Code:
r = Sheets(sh).Range("A1").CurrentRegion.Value


VBA Code:
Dim r As Variant


Sub LoadData()
Dim sh As Variant
For Each sh In Sheets(Array("Mussala", "mssau", "mjhgsg"))
r = Sheets(sh).Range("A1").CurrentRegion.Value
Next sh
 With Me.ListBox1
        .List = r
End With

End Sub
Private Sub UserForm_Initialize()
Call LoadData
  
End Sub
I hope some body fix this problem .
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try:
VBA Code:
    For Each sh In Array("Mussala","mssau","mjhgsg")
        r = Sheets(sh).Range("A1").CurrentRegion.Value
    Next sh

Artik
 
Upvote 0
thanks,
but unfortunately it just show Mussala sheet and ignore the others !
should show all of data for all of sheets.
 
Upvote 0
Code:
Sub LoadData()
    Dim s$(1), x
    s(0) = "Select * From `Mussala$` Union All Select * From `mssau$` Union All Select * From `mjhgsg$`;"
    s(1) = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & _
         ";Extended Properties='Excel 12.0;HDR=Yes';"
    With CreateObject("ADODB.Recordset")
        .Open s(0), s(1), 3, 3, 1
        x = .GetRows
    End With
    With Me.ListBox1
        .ColumnCount = UBound(x, 1) + 1
        .Column = x
    End With
End Sub
 
Upvote 0
Awesome !
just I want getting rid of specific rows from showing in listbox . when there is OPENING word in column C and TOTAL word in column A , then shouldn't show .
sorry , your code is difficult to me and I can't modify it .
thanks again.
 
Upvote 0
Since you don't show us the data lyouts, my guess
Code:
    s(0) = "Select * From `Mussala$A2:G` Where F1 <> 'total' " & _
           "Union All (Select * From `mssau$A2:G` Where F1 <> 'total') " & _
           "Union All (Select * From `mjhgsg$A2:G` Where F1 <> 'total');"
    s(1) = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & _
         ";Extended Properties='Excel 12.0;HDR=No';"
 
Upvote 0
it shows me Run Time Error'-2147217913 (80040e07)' __Data type mismatch in criteria expression
VBA Code:
.Open s(0), s(1), 3, 3, 1
Since you don't show us the data lyouts, my guess
mu1.xlsm
ABCDEF
1DATEINVOICE NOTYPEDEBITCREDITBALANCE
231/12/2023OPENING2,000.002,000.00
301/01/2024AL0001CASH SALES1,000.001,000.00
402/01/2024AL0002FORWARD SALES2,000.003,000.00
503/01/2024AL0003CASH SALES1,000.002,000.00
604/01/2024AL0004FORWARD SALES2,000.004,000.00
704/01/2024AL0005FORWARD BUYING2,000.002,000.00
805/01/2024AL0006FORWARD BUYING1,000.001,000.00
906/01/2024VFRT 2000CASH ACCRUING 2,000.003,000.00
1007/01/2024VFRT 2001PAID ACCRUING1,000.002,000.00
1107/01/2024VFRT 2002CASH ACCRUING 1,500.003,500.00
1208/01/2024VFRT 2003PAID ACCRUING1,000.002,500.00
1309/01/2024FRT0001FORWARD BUYING RETURNS2,000.004,500.00
1410/01/2024FRT0002FORWARD BUYING RETURNS1,000.005,500.00
1511/01/2024FRT0003FORWARD SALES RETURNS1,000.004,500.00
1612/01/2024FRT0004FORWARD BUYING RETURNS500.004,000.00
1713/01/2024CCS0001CASH BUYING1,000.005,000.00
1813/01/2024ZXS1000CASH BUYING RETURNS1,000.006,000.00
1913/01/2024ZXS1001CASH BUYING RETURNS2,000.004,000.00
2014/01/2024ZXS1002CASH SALES RETURNS2,000.002,000.00
21TOTAL16,500.0010,500.006,000.00
Mussala
Cell Formulas
RangeFormula
D21:E21D21=SUM(D2:D20)
F21F21=D21-E21


mu1.xlsm
ABCDEF
1DATEINVOICE NOTYPEDEBITCREDITBALANCE
231/12/2023OPENING5,000.00-5,000.00
301/01/2024AL0007FORWARD SALES6,000.001,000.00
402/01/2024AL0008FORWARD SALES4,000.005,000.00
503/01/2024AL0009CASH SALES1,000.004,000.00
604/01/2024AL0010CASH SALES500.003,500.00
704/01/2024AL0011FORWARD BUYING2,000.002,000.00
805/01/2024AL0012PAID ACCRUING1,000.001,000.00
905/01/2024AL0013PAID ACCRUING500.00500.00
1005/01/2024AL0014PAID ACCRUING500.000.00
1108/01/2024AL0015FORWARD BUYING500.00-500.00
1209/01/2024AL0016FORWARD BUYING500.00-1,000.00
1310/01/2024FRT0005FORWARD BUYING RETURNS2,000.001,000.00
1410/01/2024FRT0006FORWARD BUYING RETURNS2,000.003,000.00
1511/01/2024ZXS1003CASH BUYING RETURNS1,000.004,000.00
1612/01/2024ZXS1004CASH SALES RETURNS10003,000.00
17TOTAL15,000.0012,500.002,500.00
mssau
Cell Formulas
RangeFormula
D17:E17D17=SUM(D2:D16)
F17F17=D17-E17



mu1.xlsm
ABCDEF
1DATEINVOICE NOTYPEDEBITCREDITBALANCE
201/01/2024AL00017FORWARD SALES3,000.003,000.00
302/01/2024AL00018FORWARD SALES4,000.007,000.00
403/01/2024AL00019CASH SALES1,000.004,000.00
504/01/2024AL00020CASH SALES500.003,500.00
604/01/2024AL00021FORWARD BUYING2,000.002,000.00
705/01/2024VFRT 2003CASH ACCRUING 5,000.007,000.00
806/01/2024AL00023FORWARD BUYING4,000.003,000.00
907/01/2024VFRT 2004CASH ACCRUING 1,000.004,000.00
1008/01/2024AL00025FORWARD BUYING5,000.00-1,000.00
1109/01/2024VFRT 2005CASH ACCRUING 3,000.00-2,000.00
1210/01/2024FRT0006FORWARD SALES RETURNS1,000.00-3,000.00
1311/01/2024FRT0007FORWARD SALES RETURNS1,000.00-4,000.00
1412/01/2024CCS0002CASH BUYING1,000.00-3,000.00
1513/01/2024CCS0003CASH BUYING3,000.000.00
16TOTAL20,000.0014,500.005,500.00
mjhgsg
Cell Formulas
RangeFormula
D16:E16D16=SUM(D2:D15)
F16F16=D16-E16

so when show data in listbox will exclude the last row for each sheet contains TOTAL word in column A also exclude row if there is OPENING word is existed in column C
not all of sheets will contain OPENING word, but always contains TOTAL word in lastrow.
 
Last edited:
Upvote 0
Code:
Sub LoadData()
    Dim s$(1), x, e
    For Each e In Array("Mussala", "mssau", "mjhgsg")
        s(0) = s(0) & IIf(s(0) = "", "", "Union All (") & "Select " & _
        "Format(`DATE`,'dd/mm/yyyy'), `INVOICE NO`, `TYPE`, `DEBIT`, `CREDIT`, " & _
        "`BALANCE` From `" & e & "$` Where `TYPE` Is Not Null " & IIf(s(0) = "", "", ") ")
    Next
    s(1) = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & _
         ";Extended Properties='Excel 12.0;HDR=Yes';"
    With CreateObject("ADODB.Recordset")
        .Open s(0), s(1), 3, 3, 1
        x = .GetRows
    End With
    With Me.ListBox1
        .ColumnCount = UBound(x, 1) + 1
        .Column = x
    End With
End Sub
 
Upvote 0
thanks ,
also I would exclude row contains OPENING word in column C as I said
also exclude row if there is OPENING word is existed in column C

when there is OPENING word in column C and TOTAL word in column A , then shouldn't show .
 
Upvote 0

Forum statistics

Threads
1,225,626
Messages
6,186,094
Members
453,337
Latest member
fiaz ahmad

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