VBA ISSUE, COPY AND PASTE, IF function

Amenolakaky

New Member
Joined
Feb 17, 2022
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Hello all, this my first time to use the VBA and actually i started my first project also i'm a big fan for this community, appreciated
I got a codes from many sites to do the below
1- 'Open method has additional parameters '(This code done)
2-lCopyLastRow2 = wsCopy.Cells(wsCopy.Rows.Count, "D").End(xlUp).Row '(This code done)
3-wsDest.Range("A" & lDestLastRow).PasteSpecial Paste:=xlPasteValues''(This code done)
i succeed to select my data till the last row then open the destination sheet and paste my data.

the issue is !
i need to set if condition after identifying the range & last row = If the new section range "E11:E" & lst row equal "Building/Physical" or "Building/OneClick" then IF MID "D11:D" & LAST ROW equal "3601" then copy and past it in sheet names "SSS" After identifying the last row and past the new data.

else
If the new section range "E11:E" & lst row equal "Building/Physical" or "Building/OneClick" then IF MID "D11:D" & LAST ROW equal "44001" then copy and past it in sheet names "kkk" After identifying the last row and past the new data.
else
If the new section range "E11:E" & lst row equal "Building/Physical" or "Building/OneClick" then IF MID "D11:D" & LAST ROW equal "5500" then copy and past it in sheet names "uuu" After identifying the last row and past the new data.
else
If the new section range "E11:E" & lst row equal "Building/Physical" or "Building/OneClick" then IF MID "D11:D" & LAST ROW equal "6600" then copy and past it in sheet names "uuu" After identifying the last row and past the new data.

If the new section range "E11:E" & lst row equal "Building/Physical" or "Building/OneClick" then IF MID "D11:D" & LAST ROW equal "E360" then copy and past it in sheet names "EEE" After identifying the last row and past the new data.

else
If the new section range "E11:E" & lst row equal "Building/Physical" or "Building/OneClick" then IF MID "D11:D" & LAST ROW equal "E440" then copy and past it in sheet names "TTT" After identifying the last row and past the new data.
...............................................

Please note that every day i open a new sheet with today's name from this source
i have the first code which bring data and paste it in the destination sheet, i just need to set the if as i mentioned above "In the mail sheet or destination sheet" same will be useful.

Many thanks in advance
 

Attachments

  • Capture.PNG
    Capture.PNG
    66.5 KB · Views: 15

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi and welcome to MrExcel.

Check if the following is what you need.

VBA Code:
Sub Copy_Paste()
  Dim ary1 As Variant, ary2 As Variant
  Dim i As Long
  
  Application.ScreenUpdating = False
  ary1 = Array("44001", "5500", "6600", "E360", "E440", "E443")
  ary2 = Array("SSS", "KKK", "UUU", "UUU", "EEE", "TTT")

  With Sheets("mail")
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("A10").AutoFilter 5, "Building/Physical", xlOr, "Building/OneClick"
    For i = 0 To UBound(ary1)
      .Range("A10").AutoFilter 4, ary1(i)
      If .Range("A" & Rows.Count).End(3).Row > 10 Then
        .AutoFilter.Range.Offset(1).Copy Sheets(ary2(i)).Range("A" & Rows.Count).End(3)(2)
      End If
    Next
    .ShowAllData
  End With
End Sub
 
Upvote 0
SANSADATABASEE.xlsm
ABCDEFGHIJKLMNOR
1Bank NameDateApp#PolicyType RSTNUserStatusReason1The data pasted here with my previous code I need to copy from here rows If in column E written "Building/Physical", xlOr, "Building/OneClick" policy number columns if the above condition true then if the mid policy number started with 360 then copy and paste it in sheet aler after the last row as agreed policy number columns if the above condition true then if the mid policy number started with E36 then copy and paste it in sheet ALEX in the another range from column M : U policy number columns if the above condition true then if the mid policy number started with 440 then copy and paste it in sheet aler after the last row as agreed policy number columns if the above condition true then if the mid policy number started with E44 then copy and paste it in sheet AK in the another range from column M : U policy number columns if the above condition true then if the mid policy number started with 6600 then copy and paste it in sheet aler after the last row as agreed policy number columns if the above condition true then if the mid policy number started with E66 then copy and paste it in sheet AMO in the another range from column M : U N.B For the above also you can make the code based on the bank nam ecolumn but i'm afraid if the user didn't add it so we will make it look to the left of the policy number if 36 , e36 ETC.....
2Alex18/02/20220360120001F8okkdone
3Alex18/02/20221360120002Building/Physicalokldone
4Alex18/02/20222360120003Building/Physicalokmdone
5Alex18/02/20223360120004Building/OneClickokndone
6Alex18/02/20224360120005Building/Physicalokodone
7Alex18/02/20225360120006Building/OneClickokkdone
8Alex18/02/20220360120007Building/OneClickokldone
9AKK18/02/20223.571429440120001Building/Physicalokmdone
10AKK18/02/20223.928571440120002Building/OneClickokndone
11AKK18/02/20224.285714440120003Building/OneClickokodone
12AKK18/02/20224.642857440120004Building/Physicalokkdone
13AKK18/02/20225440120005Building/OneClickokldone
14AKK18/02/20225.357143440120006Building/OneClickokmdone
15AKK18/02/20225.714286440120007Building/Physicalokndone
16AKK18/02/20226.071429440120008Building/OneClickokodone
17AKK18/02/20226.428571440120009Building/OneClickokkdone
18Alex18/02/20226.785714360120005Building/Physicalokldone
19Alex18/02/20227.142857360120006Building/OneClickokmdone
20Alex18/02/20227.5360120007Building/OneClickokndone
21AKK18/02/20227.857143440120001Building/Physicalokodone
22AKK18/02/20228.214286440120002Building/OneClickokkdone
23Alex18/02/20228.571429360120005nothingokldone
24Alex18/02/20228.928571360120006nothingokmdone
25Alex18/02/20229.285714360120007nothingokndone
26AKK18/02/20229.642857440120001nothingokodone
27AKK18/02/202210440120002nothingokkdone
28AlexE3600001Building/OneClickokkdone
29AlexE3600002Building/OneClickokkdone
30AlexE3600003Building/OneClickokkdone
31AlexE3600004Building/OneClickokkdone
32AKKE4400001Building/OneClickokkdone
33AKKE4400002Building/OneClickokkdone
34AKKE4400003Building/OneClickokkdone
35AKKE4400004Building/OneClickokkdone
36AKKE4400005Building/OneClickokkdone
37AKKE4400006Building/OneClickokkdone
38AMO66000004Building/OneClickokkdone
39AMO66000004Building/OneClickokkdone
40AMO66000004Building/OneClickokkdone
41AMO66000004Building/OneClickokkdone
42AMO66000004Building/OneClickokkdone
43AMOE6600001Building/OneClickokkdone
44AMOE6600002Building/OneClickokkdone
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F28:H44Expression=$C29="Booked"textNO
E2:H2,F3:H27Expression=$C3="Booked"textNO
B1Cell ValueduplicatestextNO
 

Attachments

  • AMO.PNG
    AMO.PNG
    100.1 KB · Views: 5
Upvote 0
Hi and welcome to MrExcel.

Check if the following is what you need.

VBA Code:
Sub Copy_Paste()
  Dim ary1 As Variant, ary2 As Variant
  Dim i As Long
 
  Application.ScreenUpdating = False
  ary1 = Array("44001", "5500", "6600", "E360", "E440", "E443")
  ary2 = Array("SSS", "KKK", "UUU", "UUU", "EEE", "TTT")

  With Sheets("mail")
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("A10").AutoFilter 5, "Building/Physical", xlOr, "Building/OneClick"
    For i = 0 To UBound(ary1)
      .Range("A10").AutoFilter 4, ary1(i)
      If .Range("A" & Rows.Count).End(3).Row > 10 Then
        .AutoFilter.Range.Offset(1).Copy Sheets(ary2(i)).Range("A" & Rows.Count).End(3)(2)
      End If
    Next
    .ShowAllData
  End With
End Sub
Dear DanteAmor,
Many thanks for your help,

I have sent the mini sheet with what i need,

vba code look to sheet one and copy data Ppolicy number starts with "360" to alex sheet in the range for "360" then copy data starts with "e36" to alex sheet in the range for "e36"
then do the same as it mentioned in the mini sheet.
much appreciated !!!!!!
 
Upvote 0
Now I understand less your requirement.
i need to set if condition after identifying the range & last row = If the new section range "E11:E" & lst row equal "Building/Physical" or "Building/OneClick" then IF MID "D11:D" & LAST ROW equal "3601" then copy and past it in sheet names "SSS" After identifying the last row and past the new data.
That is different from this:
I need to copy from here rows If in column E written "Building/Physical", xlOr, "Building/OneClick"
policy number columns if the above condition true then if the mid policy number started with 360 then copy and paste it in sheet aler after the last row as agreed. Do you mean "Alex" sheet?

policy number columns if the above condition true then if the mid policy number started with E36 then copy and paste it in sheet ALEX in the another range from column M : U . This part is new and not clear to me.


You can comment:
Is the sheet name in column A?
 
Upvote 0
sorry for this Everyday i create a new sheet data range from a11 till H
I created a VBA code to take all this range and save it to database " policy number, user, building or what kind, ETC...
There is three kinds of the building should be serialized based on the bank name "ALEX, AKK, ......so i aimed to amend the code to make it send only data related to "alex" in sub sheet alex

with your help
the vba code with if condition after filtering on building and building oneclick will check the left or mid of the policy number then will copy in paste it in its sheet
you got my point

after you finish this, i will add a formula in everyday sheet to look on the database and find the largest or the last policy number which saved before and add one to bring a new policy number bases on the bank name "ALEX, AKK, ......s.

i think the formula is ready now but i couldn't make the code write the data in sheets based on the first digits of the policy number, with your help the job will be done
Now I understand less your requirement.

That is different from this:



You can comment:
Is the sheet name in column A?
 
Upvote 0
column A contain the sheets name end every sheet contains the range for policies which start with "36, 44, 55, 66" in columns ABCD , then policies which start with "E36,E44.E55" IN columns i till p ow what you will set.
I appreciate the time that you are giving to me!
 
Upvote 0
column A contain the sheets name
Try this:

VBA Code:
Sub Copy_Paste()
  Dim ary1 As Variant, ary2 As Variant
  Dim i As Long, lr As Long
  
  Application.ScreenUpdating = False
  ary1 = Array("E36", "E44", "E55")  'add the others

  With Sheets("mail")
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("A10").AutoFilter 5, "Building/Physical", xlOr, "Building/OneClick"
    For i = 0 To UBound(ary1)
      .Range("A10").AutoFilter 4, "=" & ary1(i) & "*"
      lr = .Range("A" & Rows.Count).End(3).Row
      If lr > 10 Then
        .AutoFilter.Range.Offset(1).Copy Sheets(.Range("A" & lr).Value).Range("A" & Rows.Count).End(3)(2)
      End If
    Next
    .ShowAllData
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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