I am getting an overflow error on my attempt to trim cells within my range. The line I get the error on is, C.Value = .Trim(C.Value)
Perhaps this can be done without intersect? I've tried without it but it leads to mismatch error.
Dim masterWB As Workbook
Dim dailyWB As Workbook
Dim C As Range
Application.DisplayAlerts = False
'Set Current Workbook as Master
Set masterWB = Application.ThisWorkbook
'Set some Workbook as the one you are copying from
Set dailyWB = Workbooks.Open("excelguy.xlsm")
'Copy the Range from dailyWB and Paste it into the MasterWB
dailyWB.Sheets("Summary").Range("A1:BJ200").Copy masterWB.Sheets("Master Summary").Range("A1").Rows("1:1")
'formatting and paste as values
Workbooks("excelguy Master.xlsm").Activate
Worksheets("Master Summary").Select
'trim values
columns("A:BJ").Select
With Application.WorksheetFunction
For Each C In Intersect(columns("A:BJ"), ActiveSheet.UsedRange)
C.Value = .Trim(C.Value) 'Overflow Error
Next C
End With
Any help would be appreciated.
.Select
or.Activate
your workbooks/sheets. You declared workbook variables so use them!UsedRange
can be unreliable. I recomend switching to a more standard last row calculation. Right now, the code is usingColumn A
to determine last row for all columns in your range, which in return determines the range you are going to loop through.TRIM
function can be called directly fromVBA
.Please see comment from @Tim Williams below to determine if the VBA version of Trim is acceptable
If you are just trimming the values, you could load your range into an array, modify the values into a new array, and then do a value transfer of your new trimmed array to a range
Try this. To use Variant Array is faster.