0

I am writing a VBA macro that imports some text from a text file and then I am splitting the text based on the delimiter ^. I have managed to get this working but am only able to run the macro if is does from the 'Import Data' sheet, otherwise I get a 1004 error.

I have debugged the issue and know the problem is with

Worksheets("Import Data").Range("A" & rownumber).Select

After some googling I have determined that I can't select a cell in another worksheet. But I am not sure how to avoid this. How can I run the below code without selecting the cell beforehand. Any suggestions would be greatly appreciated.

Worksheets("Import Data").Range("A" & rownumber).Select Selection.TextToColumns Destination:=Worksheets("Import Data").Range("c" & rownumber), DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ :="^", 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), Array(9, 1), Array(10, 1), Array(11, 1), Array(12 _ , 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), _ Array(19, 1), Array(20, 1), Array(21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array( _ 25, 1), Array(26, 1), Array(27, 1), Array(28, 1), Array(29, 1), Array(30, 1), Array(31, 1), _ Array(32, 1), Array(33, 1), Array(34, 1), Array(35, 1)), TrailingMinusNumbers:=True 
1
  • 2
    Remove Worksheets("Import Data").Range("A" & rownumber).Select and replace Selection with Worksheets("Import Data").Range("A" & rownumber). Commented Aug 20, 2021 at 2:58

1 Answer 1

0

The <something>.Select, Selection.<do_something> part is a result of recording a macro.

If you want to launch this piece of code in your current (Active) worksheet, you can change your code into:

ActiveSheet.Range("A" & rownumber).TextToColumns ... 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.