updating code loop throught specific sheets and ignore the others

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
hi
I have this code works from sheet to another . what I want loop for specific sheets are (export,import) and ignore sheets(main,data,archive ) .
so if any body help truly appreciate .

VBA Code:
Sub Copy_Columns()
  Dim cols As Variant, sh1 As Worksheet, sh2 As Worksheet, i As Long, c As Long, f As Range
  Set sh1 = Sheets("SHEET1")
  Set sh2 = Sheets("INVENTORY")


  cols = Array("BRAND", "MODEL", "CLIENT", "QTY IMP", "QTY EX") 'Put column titles here
  For i = 0 To UBound(cols)
    Set f = sh1.Rows(1).Find(cols(i), , xlValues, xlWhole)
    If Not f Is Nothing Then
      c = f.Column
      Set f = sh2.Rows(1).Find(cols(i), , xlValues, xlWhole)
      If Not f Is Nothing Then
        sh1.Columns(c).Copy sh2.Columns(f.Column)
      End If
    End If
  Next
 
  MsgBox "End"
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Just like you have an array to loop through the columns you want to apply it to, you can create another array that has the names of the sheets that you want to apply this to. Then loop through that array, activate each sheet, and apply the code you have above (so the array you have about would be nested inside of your sheet array).
 
Upvote 0
thanks I' m not good at vba but i try learning . based on your guiding . the code doesn't work after I changed a simple things .
VBA Code:
Sub Copy_Columns()
  Dim cols, sh As Variant, sh1 As Worksheet, sh2 As Worksheet, i, s As Long, c As Long, f As Range
  Set sh1 = Sheets("SHEET1")
  Set sh2 = Sheets("INVENTORY")

 sh = Array("export", "import")
  cols = Array("BRAND", "MODEL", "CLIENT", "QTY IMP", "QTY EX") 'Put column titles here
 s = 0 To UBound(sh)
  For i = 0 To UBound(cols)
    Set f = sh1.Rows(1).Find(cols(i), , xlValues, xlWhole)
    If Not f Is Nothing Then
      c = f.Column
      Set f = sh2.Rows(1).Find(cols(i), , xlValues, xlWhole)
      If Not f Is Nothing Then
        sh1.Columns(c).Copy sh2.Columns(f.Column)
      End If
    End If
  Next
  Next
 
  MsgBox "End"
End Sub
 
Upvote 0
You are missing the word "For" in this line here:
Rich (BB code):
s = 0 To UBound(sh)
You need it to loop through the array, just like you have for the other array loop:
Rich (BB code):
For i = 0 To UBound(cols)

Also, after this line, you will either want reference all your ranges with:
Rich (BB code):
Sheets(sh(s))...
or just select the sheet in the loop right after the first for line (so it is the active sheet), i.e.
Rich (BB code):
  For s = 0 To UBound(sh)
    Sheets(sh(s)).Activate
    For i = 0 To UBound(cols)

This is because looping through the sheet names does not actually activate/select the sheets.
So you either need to do so in the code, or reference the sheet in your range references.
 
Upvote 0
now it gives me subscript ou of range in this lin
Code:
Sheets(sh(s)).Activate
VBA Code:
Sub Copy_Columns()
  Dim cols, sh As Variant, sh1 As Worksheet, sh2 As Worksheet, i, s As Long, c As Long, f As Range
  'Set sh1 = Sheets("SHEET1")
  Set sh2 = Sheets("INVENTORY")
  sh = Array("export", "import")
  cols = Array("BRAND", "MODEL", "CLIENT", "QTY IMP", "QTY EX") 'Put column titles here
 For s = 0 To UBound(sh)
     Sheets(sh(s)).Activate
  For i = 0 To UBound(cols)
    Set f = sh.Rows(1).Find(cols(i), , xlValues, xlWhole)
    If Not f Is Nothing Then
      c = f.Column
      Set f = sh2.Rows(1).Find(cols(i), , xlValues, xlWhole)
      If Not f Is Nothing Then
        sh.Columns(c).Copy sh2.Columns(f.Column)
      End If
    End If
  Next
  Next
  MsgBox "End"
End Sub
 
Upvote 0
now it gives me subscript ou of range in this lin
Code:
Sheets(sh(s)).Activate
That seems to suggest that you do not have a sheet named "export" or "import".
Make sure that the names match EXACTLY.
 
Upvote 0
yes you're right , I fix it , but It shows another problem . it doesn't bring the values which header is QTY EXP from sheet "export"
 
Upvote 0
You would need to post the latest version of your code along with a sample of what the data on this sheet looks like and what your expected results should look like.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
SHEET"IMPORT"
loop.xlsm
ABCDEFGHIJ
1DATEBRANDMODELCLIENTQTY IMPPRICEDISCOUNTPRICE DDMARGINSALE PRICE
27/28/2021AS-10AL-100MM18013010%11710%128.7
37/29/2021AS-11AL-100MM10013010%11710%128.7
47/30/2021AS-12AL-101LL9512025%9015%103.5
5    
IMPORT
Cell Formulas
RangeFormula
G2:G5G2=IFERROR(VLOOKUP(D2,SS!$B$4:$C$1000,2,FALSE),"")
H2:H5H2=IFERROR(F2-F2*G2,"")
I2:I5I2=IFERROR(VLOOKUP(D2,SS!$B$4:$D$1000,3,FALSE),"")
J2:J5J2=IFERROR(H2+H2*I2,"")
Cells with Data Validation
CellAllowCriteria
D2:D5List=SS!$B$4:$B$1002


SHEET EXPORT
loop.xlsm
ABCDE
1DATEBRANDMODELCLIENTQTY EX
27/28/2021AS-10AL-100MM60
37/29/2021AS-11AL-100MM100
4
EXPORT
Cells with Data Validation
CellAllowCriteria
D2:D3List=SS!$B$4:$B$1002
D4List=SS!$B$4:$B$1002



EXPECTED RESULT IN SHEET INVENTORY
BEFORE
loop.xlsm
ABCDEF
1ITEMBRANDMODELCLIENTQTY IMPQTY EX
2
3
4
5
INVENTORY
Cells with Data Validation
CellAllowCriteria
D2:D5List=SS!$B$4:$B$1002




AFTER
loop.xlsm
ABCDEF
1ITEMBRANDMODELCLIENTQTY IMPQTY EX
21AS-10AL-100MM18060
32AS-11AL-100MM100100
43AS-12AL-101LL95
5
INVENTORY
Cells with Data Validation
CellAllowCriteria
D2:D5List=SS!$B$4:$B$1002



the final code
VBA Code:
Sub Copy_Columns()
  Dim cols, sh As Variant, sh1 As Worksheet, sh2 As Worksheet, i, s As Long, c As Long, f As Range
  'Set sh1 = Sheets("SHEET1")
  Set sh2 = Sheets("INVENTORY")
  sh = Array("IMPORT", "EXPORT")
  cols = Array("BRAND", "MODEL", "CLIENT", "QTY IMP", "QTY EX") 'Put column titles here
 For s = 0 To UBound(sh)
     Sheets(sh(s)).Activate
  For i = 0 To UBound(cols)
    Set f = sh.Rows(1).Find(cols(i), , xlValues, xlWhole)
    If Not f Is Nothing Then
      c = f.Column
      Set f = sh2.Rows(1).Find(cols(i), , xlValues, xlWhole)
      If Not f Is Nothing Then
        sh.Columns(c).Copy sh2.Columns(f.Column)
      End If
    End If
  Next
  Next
  MsgBox "End"
End Sub
 
Upvote 0
OK, I will play around with this a little later tonight when I have time to recreate all this on my side.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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