Whats wrong with my code? (loop through each cell in range)

kesh321

New Member
Joined
Jun 13, 2018
Messages
19
Hi,

I've got my macro sorted, just today there was another bit I had to add to my code, and this is where it fails. Tried to sort it out for few hours but no luck.
What's wrong with the code? The bit in bold, everything else is fine without it.
Would also appreciate if someone could instruct me how to clean up my code a bit :)

Code:
Sub client_date_single()Dim main, index As Worksheet
Dim tname As String
Dim last_c, cel, rng, rng_last As Range
   Application.DisplayAlerts = False
   Application.ScreenUpdating = False
   Application.CopyObjectsWithCells = False
Set main = Sheets("MAIN (FORMULAS)")
Set index = Sheets("Index")
tname = ThisWorkbook.Path


With index
date1 = DateValue(Range("B2"))
client = Range("A2")
date3 = Range("B2")
End With




'create new main file with values only
  Set newb_main = Workbooks.Add
  With newb_main
  With .Sheets.Add(Before:=.Sheets(1))
  .Name = "ALL VALUES"
  End With
  End With
  
  newb_main.SaveAs Filename:=tname & "\" & "ALL"
  With Workbooks("ALL")
    .Sheets("Sheet1").Delete
     .Sheets("Sheet2").Delete
     .Sheets("Sheet3").Delete
     End With
     
     'create new sales file with values
     Set newb_sales = Workbooks.Add
     With newb_sales
     With .Sheets.Add(Before:=.Sheets(1))
     .Name = "SALES"
     End With
     End With
     
     newb_sales.SaveAs Filename:=tname & "\" & "SALESR"
     With Workbooks("SALESR")
     .Sheets("Sheet1").Delete
     .Sheets("Sheet2").Delete
     .Sheets("Sheet3").Delete
     End With
     
     
'copy over data to new file values only
main.Range("A1:R10000").Copy
With newb_main.Sheets("ALL VALUES")
    .Range("A1").PasteSpecial xlPasteValues
    .Range("A1").PasteSpecial xlPasteFormats
    .Columns("A:R").AutoFit
    .Range("S1:AZ10000").Clear
    .Range("A1:R1").AutoFilter
    End With
    newb_main.Save


'filter data i need with newly created file with values only
With newb_main.Sheets("ALL VALUES").Range("A1:R10000")
.AutoFilter 2, "=" & client
.AutoFilter 12, "=" & date1
.SpecialCells(xlVisible).Copy
End With






'creating new file with name of client choosen
  Set newbook = Workbooks.Add
  With newbook
  With .Sheets.Add(Before:=.Sheets(1))
  .Name = client
  End With
  End With
  
  ' this client required deleting of one of the data columns
If index.Range("A2").Value = "CLARITY USP" Then
  With newbook.Worksheets(client)
    .Range("A1").PasteSpecial xlPasteValues
    .Range("A1").PasteSpecial xlPasteFormats
    .Columns("A:R").AutoFit
    .Range("S1:AZ6666").Clear
    .Columns(4).EntireColumn.Delete
        End With
        Else
        With newbook.Worksheets(client)
    .Range("A1").PasteSpecial xlPasteValues
    .Range("A1").PasteSpecial xlPasteFormats
    .Columns("A:R").AutoFit
    .Range("S1:AZ6666").Clear
    End With
    End If


'saving the client report
newbook.SaveAs Filename:=tname & "\" & client
  With Workbooks(client)
    .Sheets("Sheet1").Delete
     .Sheets("Sheet2").Delete
     .Sheets("Sheet3").Delete
     .Save
     'test if data found
        count1 = .Sheets(client).Range("A1:A100").Cells.SpecialCells(xlCellTypeConstants).Count
        End With
       If count1 = 1 Then
       MsgBox ("No data found for " & client & " - with dispatch date: " & date3)
       Workbooks(client).Save
       newb_main.Close SaveChanges:=True
  Kill tname & "\" & client & ".xlsx"
  Kill tname & "\ALL.xlsx"
  Exit Sub
End If
       'end test
       
       'TESTING - PULLING PRODUCT LINES over to different sheets on client report
       'picking range to loop through for all invoice nubmers found
       With Workbooks(client).Sheets(client)
       Set rng_last = .Range("D2").End(xlDown)
       Set rng = .Range("D2", rng_last)
       End With
       
       'copying over the values only to new file for sales
       With ThisWorkbook.Sheets("Sales Report")
       .Range("A1:N10000").Copy
       End With
       With Workbooks("SALESR").Sheets("SALES")
       .Range("A1").PasteSpecial xlPasteValues
       .Range("A1").PasteSpecial xlPasteFormats
       End With
       
       'loop through all the cells with invoices in the client file, add the sheet named after current cell in range value, and pasting the values from filtered sales report to each sheet
[B]       On Error Resume Next[/B]
[B]       For Each cel In rng.Cells[/B]
[B]       'With cel[/B]
[B]        With Workbooks(client)[/B]
[B]        With .Sheets.Add(After:=.Sheets(1))[/B]
[B]        .Name = cel[/B]
[B]        End With[/B]
[B]        End With[/B]
[B]       With Workbooks("SALESR").Sheets("SALES")[/B]
[B]       .Range("A1:N10000").AutoFilter 10, cel[/B]
[B]       .Range("A1:N10000").SpecialCells(xlVisible).Copy[/B]
[B]       .Range("A1:N10000").Select[/B]
[B]       .Selection.Copy[/B]
[B]       '.Range("A1:N1").AutoFilter[/B]
[B]       '.Range("A1:N1").AutoFilter[/B]
[B]       End With[/B]


[B]        With Workbooks(client).Sheets(cel).Range("A1")[/B]
[B]        .PasteSpecial xlPasteValues[/B]
[B]        .PasteSpecial xlPasteFormats[/B]
[B]        End With[/B]
[B]        'End With[/B]

[B]       On Error Resume Next[/B]
[B]        Next cel[/B]




       
     Workbooks("SALESR").Close SaveChanges:=True
     Workbooks(client).Close SaveChanges:=True
    
  newb_main.Sheets("ALL VALUES").Range("A1:r1").AutoFilter
  newb_main.Sheets("ALL VALUES").Range("A1:r1").AutoFilter
  
  newb_main.Close SaveChanges:=True


  
    Kill tname & "\ALL.xlsx"
    Kill tname & "\SALESR.xlsx"
  On Error GoTo 0
  
   Application.ScreenUpdating = True
   Application.CopyObjectsWithCells = True
  Application.CutCopyMode = False
     Application.DisplayAlerts = True
End Sub

Thanks, any help appreciated.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Remove this line
Code:
On Error Resume Next
from your code (both instances) & see what happens
 
Upvote 0
Hi Fluff,

Thanks for getting back to me. I've replied yesterday but apparently the message did not went through.

I've tried that and many things, like moving stuff around and assigning some ranges to variables, playing around 'with's... and finally got it sorted, I'm not sure what exactly was that got the code sorted, but I reckon the variables.

That's the fully working code:
Code:
Sub client_date_single()Dim main, index As Worksheet
Dim tname, ShtNum As String
Dim ShtCnt As Long
Dim n As Integer
Dim last_c, cel, rng, rng_last, rang As Range
   Application.DisplayAlerts = False
   Application.ScreenUpdating = False
   Application.CopyObjectsWithCells = False
Set main = Sheets("MAIN (FORMULAS)")
Set index = Sheets("Index")
tname = ThisWorkbook.Path
n = 1
With index
date1 = DateValue(Range("B2"))
client = Range("A2")
date3 = Range("B2")
End With




'create new main file with values only
  Set newb_main = Workbooks.Add
  With newb_main
  With .Sheets.Add(Before:=.Sheets(1))
  .Name = "ALL VALUES"
  End With
  End With
  
  newb_main.SaveAs Filename:=tname & "\" & "ALL"
  With Workbooks("ALL")
    .Sheets("Sheet1").Delete
     .Sheets("Sheet2").Delete
     .Sheets("Sheet3").Delete
     End With
     
     'create new sales file with values
     Set newb_sales = Workbooks.Add
     With newb_sales
     With .Sheets.Add(Before:=.Sheets(1))
     .Name = "SALES"
     End With
     End With
     
     newb_sales.SaveAs Filename:=tname & "\" & "SALESR"
     With Workbooks("SALESR")
     .Sheets("Sheet1").Delete
     .Sheets("Sheet2").Delete
     .Sheets("Sheet3").Delete
     End With
     
     
'copy over data to new file values only
main.Range("A1:R10000").Copy
With newb_main.Sheets("ALL VALUES")
    .Range("A1").PasteSpecial xlPasteValues
    .Range("A1").PasteSpecial xlPasteFormats
    .Columns("A:R").AutoFit
    .Range("S1:AZ10000").Clear
    .Range("A1:R1").AutoFilter
    End With
    newb_main.Save


'filter data i need with newly created file with values only
With newb_main.Sheets("ALL VALUES").Range("A1:R10000")
.AutoFilter 2, "=" & client
.AutoFilter 12, "=" & date1
.SpecialCells(xlVisible).Copy
End With






'creating new file with name of client choosen
  Set newbook = Workbooks.Add
  With newbook
  With .Sheets.Add(Before:=.Sheets(1))
  .Name = client
  End With
  End With
  
  ' this client required deleting of one of the data columns
If index.Range("A2").Value = "CLARITY USP" Then
  With newbook.Worksheets(client)
    .Range("A1").PasteSpecial xlPasteValues
    .Range("A1").PasteSpecial xlPasteFormats
    .Columns("A:R").AutoFit
    .Range("S1:AZ6666").Clear
    .Columns(4).EntireColumn.Delete
        End With
        Else
        With newbook.Worksheets(client)
    .Range("A1").PasteSpecial xlPasteValues
    .Range("A1").PasteSpecial xlPasteFormats
    .Columns("A:R").AutoFit
    .Range("S1:AZ6666").Clear
    End With
    End If


'saving the client report
newbook.SaveAs Filename:=tname & "\" & client
  With Workbooks(client)
    .Sheets("Sheet1").Delete
     .Sheets("Sheet2").Delete
     .Sheets("Sheet3").Delete
     .Save
     'test if data found
        count1 = .Sheets(client).Range("A1:A100").Cells.SpecialCells(xlCellTypeConstants).Count
        End With
       If count1 = 1 Then
       MsgBox ("No data found for " & client & " - with dispatch date: " & date3)
       Workbooks(client).Save
       newb_main.Close SaveChanges:=True
  Kill tname & "\" & client & ".xlsx"
  Kill tname & "\ALL.xlsx"
  Exit Sub
End If
       'end test
       
       'TESTING - PULLING PRODUCT LINES over to different sheets on client report
       'picking range to loop through for all invoice nubmers found
       With Workbooks(client).Sheets(client)
      Set rng_last = .Range("D2").End(xlDown)
       Set rng = .Range("D2", rng_last)
       End With
       
       'copying over the values only to new file for sales
       With ThisWorkbook.Sheets("Sales Report")
       .Range("A1:N10000").Copy
       End With
       With Workbooks("SALESR").Sheets("SALES")
       .Range("A1").PasteSpecial xlPasteValues
       .Range("A1").PasteSpecial xlPasteFormats
       End With
       
       'loop through all the cells with invoices in the client file, add the sheet named after current cell in range value, and pasting the values from filtered sales report to each sheet
       On Error Resume Next
       For Each cel In rng.Cells
       With Workbooks("SALESR").Sheets("SALES")
       .Range("A1:N10000").AutoFilter 10, cel
       If .Range("A1:A10000").SpecialCells(xlVisible).Count > 1 Then
               With Workbooks(client)
        ShtNum = cel.Value
        With .Sheets.Add(After:=.Sheets(n))
        .Name = ShtNum
        n = n + 1
        End With
        End With
       Set rang = .Range("b1:j10000")
       rang.WrapText = False
       rang.RowHeight = 15
       rang.Bold = True
       rang.SpecialCells(xlCellTypeVisible).Copy
       Workbooks(client).Sheets(ShtNum).Range("A1").PasteSpecial xlValues
        Workbooks(client).Sheets(ShtNum).Range("A1").PasteSpecial xlPasteFormats
        Workbooks(client).Sheets(ShtNum).Columns("A:Z").AutoFit
       .Range("A1:N1").AutoFilter
       .Range("A1:N1").AutoFilter
       Else
       MsgBox "No entries found for invoice number: " & cel & "! Sales reports are generated since 05/07/2018.", False, client
       GoTo ErrRes
       End If
    End With
ErrRes:
        Next cel




       
     Workbooks("SALESR").Close SaveChanges:=True
     Workbooks(client).Close SaveChanges:=True
    
  newb_main.Sheets("ALL VALUES").Range("A1:r1").AutoFilter
  newb_main.Sheets("ALL VALUES").Range("A1:r1").AutoFilter
  
  newb_main.Close SaveChanges:=True


  
    Kill tname & "\ALL.xlsx"
    Kill tname & "\SALESR.xlsx"
  On Error GoTo 0
  
   Application.ScreenUpdating = True
   Application.CopyObjectsWithCells = True
  Application.CutCopyMode = False
     Application.DisplayAlerts = True
End Sub

Sorry, I know it's messy and all, but I am just a beginner, and I still reckon this as a big win for me :D Maybe someone will need this code later.

Anyways, this is sorted, thanks for always getting back to me Fluff, appreciated :)
Cheers!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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