Copy & Export to Excel Sheet issue.

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
Office Version
  1. 2016
Platform
  1. Windows
My Full code which is below, works on creating a copy of a worksheet, sorting out the header and freeze frame, as well as naming and date/time stamping the new sheet. All this works fine. The only issues I have is that it should also fill all BLANK sells with a Hyphen. Currently is is only doing the first 10 rows and I can not work out why.

I am adding a hyphen so the cells have some data in them, if left blank the adjacent cells data spills into the next column, making it hard to read the sheet data.

This is the issue code
VBA Code:
   'Fill all BLANK CELLS with Hyphen
    Dim r As Range, LastRow As Long
    LastRow = Cells(Rows.Count, a).End(xlUp).Row
    For Each r In Range("A1:g1" & LastRow)
        If r.Text = "" Then r.Value = "-"
    Next r

Full code
VBA Code:
Private Sub ExcelExportBt_Click()
'##### Export to excel #########
If Sheet8.Range("a2").Value = "" Then
ExportError.Show
Else
    Application.SheetsInNewWorkbook = 1
        Workbooks.Add
    With ThisWorkbook
        .Sheets("Sheet8").UsedRange.Copy 'Copy this sheet
            ActiveWorkbook.Sheets(1).Range("A1").PasteSpecial xlPasteValues
                ActiveWorkbook.Sheets(1).Name = "Data Search" ' new sheet name
        On Error Resume Next
     
    'column width and header
    Worksheets("Data Search").Columns("A:j").ColumnWidth = 25
    'Formatting the header
        Range("A1:g1").Font.Name = "Calibri"
            Range("A1:g1").HorizontalAlignment = xlCenter
                Range("A1:g1").Font.Color = vbWhite
                    Range("A1:g1").Interior.ColorIndex = 16 'Color Grey

  '##########################  THIS BIT IS NOT FULLY WORKING #############################                     
    'Fill all BLANK CELLS with Hyphen
    Dim r As Range, LastRow As Long
    LastRow = Cells(Rows.Count, a).End(xlUp).Row
    For Each r In Range("A1:g1" & LastRow)
        If r.Text = "" Then r.Value = "-"
    Next r
  '##########################  THIS BIT IS NOT FULLY WORKING #############################                      
                                    
    'Name of Sheet and date + time stamp
       ActiveWorkbook.SaveAs "Data Search" & Format(Now, " dd_mm_yyyy    HH_mm_ss") & ".xlsx"
      On Error Resume Next

    ' Create a Freeze panel on new sheet
        Dim ws As Worksheet
            For Each ws In Worksheets
                 ws.Activate
                     With Application.ActiveWindow
                .SplitColumn = 0
            .SplitRow = 1
        End With
            Application.ActiveWindow.FreezePanes = True
                If Not ActiveSheet.AutoFilterMode Then
                ActiveSheet.Range("A1").AutoFilter

        End If
         Next ws
    End With
End If
End Sub
 

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.
Try removing this line, everywhere it appears
VBA Code:
On Error Resume Next
You should never use it like that as all it does is mask any errors in your code & therefore makes it almost impossible to figure out why something is not doing what you want.
 
Upvote 0
I get an error on this row in the issue code
VBA Code:
LastRow = Cells(Rows.Count, a).End(xlUp).Row
1594985556231.png


1594985516680.png
 
Upvote 0
What is a, it has not been defined anywhere in the code?
 
Upvote 0
I'm not sure what the "a" I think i put it in as I wanted it to start from column A, that did not work so I made changes. i took out the "a" is I took it out still the same problem. My excel for some reason has become very slow and unresponsive today. hence the delay in the replies.
 
Upvote 0
If you are trying to find the last row in column A then it should be
VBA Code:
Cells(Rows.count,"A")
 
Upvote 0
Thanks fluff

I have done it as such now

VBA Code:
'Fill all BLANK CELLS with Hyphen
    Dim r As Range, LastRow As Long
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each r In Range("A1:G1" & LastRow)
        If r.Text = "" Then r.Value = "-"
    Next r

For some reason it fills up to row 123. I have gone into sheet8 and cleared all content/comments/formats, yet it still stops at row 123. Any idea why?
1594987882035.png
 
Upvote 0
This line
VBA Code:
Range("A1:G1" & LastRow)
concatenates G1 with Lastrow, so if lastrow is 23 you get G123. It should be
VBA Code:
Range("A1:G" & LastRow)
 
Upvote 0
Fluff, as always super work.

Thanks again
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,877
Messages
6,122,051
Members
449,064
Latest member
scottdog129

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