The code at the bottom copies columns individually from one sheet to another and performs text to columns one by one. for example, after column A has been taken care of, the Text to columns procedure for column B (first sheet) is supposed to begin in the next available empty column in the second sheet.
The rows in the first sheet are of different lengths. So column A might have cells A1:A25 full but column N might only have some of its cells full because some of the rows have ended.
This code works fine until it encounters the first partially full column and then it keeps pasting columns into the same column.
I thought the line below would take care of this but it doesn’t seem to work:
If Application.WorksheetFunction.CountA(Excel.Sheets("Organise_R").Columns(b)) > 0 Then b = b + 1
I’ve been trying for the last few hours but I’ve had no success.Any help would be much appreciated! Thanks in advance!
For a = 1 To 60
'If Excel.WorksheetFunction.CountBlank(Excel.Sheets("Import_R").Columns(a)) < 1048576
If Application.WorksheetFunction.CountA(Excel.Sheets("Import_R").Columns(a)) > 0 Then
Excel.Sheets("Import_R").Columns(a).Copy
b = Excel.Sheets("Organise_R").Cells(1, Columns.Count).End(Excel.xlToLeft).column
Excel.Sheets("Organise_R").Select
'If Cells(1, b) <> ""
'If Excel.WorksheetFunction.CountBlank(Excel.Sheets("Organise_R").Columns(b)) < 1048576 Then
If Application.WorksheetFunction.CountA(Excel.Sheets("Organise_R").Columns(b)) > 0 Then b = b + 1
Excel.Sheets("Organise_R").Columns(b).EntireColumn.Select
Excel.ActiveSheet.Paste
Excel.Application.CutCopyMode = False
Selection.TextToColumns Destination:=Cells(1, b), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True
End If
Next a
This is not how I would do it – Specifically, I don’t understand your line of code:
Since I owuld assume you owuld need a loop here to find the last column, but this is a different matter.
But I think to answer your question, you would be better off using the
UsedRangemethod to find the last column in your sheet, maybe something like this:To replace your line:
Hope this helps.