how can use array with this long lines code?

leap out

Active Member
Joined
Dec 4, 2020
Messages
271
Office Version
  1. 2016
  2. 2010
hello
I have this code but it's very slow if anybody has make shorter by using array I truly appreciate
VBA Code:
Private Sub CommandButton1_Click()
 Dim ws As Worksheet, matchFormula1, matchFormula2, matchFormula13 As String, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9 As String
    Set ws = Sheets("Sheet1")
    cb1 = ComboBox1.Text
    cb2 = ComboBox2.Text
    cb3 = ComboBox3.Text
    cb4 = ComboBox4.Text
    cb5 = ComboBox5.Text
    cb6 = ComboBox6.Text
    cb7 = ComboBox7.Text
    cb8 = ComboBox8.Text
    cb9 = ComboBox9.Text
    
    matchFormula1 = "match(" & Chr(34) & cb1 & cb2 & cb3 & Chr(34) & ",A:A&B:B&C:C,0)"
    matchFormula2 = "match(" & Chr(34) & cb4 & cb5 & cb6 & Chr(34) & ",A:A&B:B&C:C,0)"
   matchFormula3 = "match(" & Chr(34) & cb7 & cb8 & cb9 & Chr(34) & ",A:A&B:B&C:C,0)"
    On Error Resume Next
    ws.Cells(Evaluate(matchFormula1), ws.Columns.Count).End(xlToLeft).Offset(, 1) = TextBox1.Value
    ws.Cells(Evaluate(matchFormula2), ws.Columns.Count).End(xlToLeft).Offset(, 1) = TextBox2.Value
    ws.Cells(Evaluate(matchFormula3), ws.Columns.Count).End(xlToLeft).Offset(, 1) = TextBox3.Value
    On Error GoTo 0
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You use complete columns to match. It is better to search for the last row and match till this last row.
 
Upvote 0
thanks but honestly it shows slowly when copy data from user form to sheet
 
Upvote 0
Because the matchformula.
The range A:A&B:B&C:C is the same as A1:A1048576&B1:B1048576&C1:C1048576
When this is more compact like A1:A1000&B1:B1000&C1:C1000 then your code is faster.
 
Upvote 0
you're right it's fast ,but is there any way the code just search for the data which is filling rows without search for empty rows to doesn't make slow the code ?
 
Upvote 0
I used as example row 1000.
In practice you search the last row of your data like:
VBA Code:
lastrow=ws.Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0
this part of amending code but it doesn't copy anything , can you guide me please?
VBA Code:
lastrow = ws.Range("C" & Rows.Count).End(xlUp).Row

    matchFormula1 = "match(" & Chr(34) & cb1 & cb2 & cb3 & Chr(34) & "(,A1:A & LR) & (B1:B & LR) & (C1:C & LR ),0)"
    matchFormula2 = "match(" & Chr(34) & cb4 & cb5 & cb6 & Chr(34) & "(,A1:A & LR) & (B1:B & LR) & (C1:C & LR ),0)"
   matchFormula3 = "match(" & Chr(34) & cb7 & cb8 & cb9 & Chr(34) & "(,A1:A & LR) & (B1:B & LR) & (C1:C & LR ),0)"
 
Upvote 0
VBA Code:
matchFormula1 = "match(" & Chr(34) & cb1 & cb2 & cb3 & Chr(34) & ",A1:A" & lastrow &" & B1:B" & lastrow & " & C1:C" & lastrow & ",0)"
 
Upvote 0
Solution
I have no problems.
Can you switch off the ON ERROR statement.
Do you get an error?
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,969
Members
449,059
Latest member
oculus

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