2

I'm trying to do this in macro:

A cell should be filled with =CONCATENATE("[Text]",Variable,"[Text2]")

Variable is a number in a different case a string.

Let's say: Text=Hello, Variable is filled with 1, Text2=Community

Then it should say in the cell

=CONCATENATE("Hello";"1";"Community")

and it should be read as: Hello1Community

When I try ActiveCell.FormulaR1C1 = "=CONCATENATE(""Hello"",Variable,""Community"")" then I get this: =CONCATENATE("Hello";Variable;"Community").

Sadly it is the name of the Variable but not its content. I get the #Name? error because the Variable is not wrapped around "".

In a different case I'm trying to put a reference to a previous sheet and cell.

In my macro I'm creating a new sheet and want to reference to it. I save the sheet's name into a variable (string). Then create a new sheet and certain cells should reference to the previous sheet.

Let's say previous sheet's name = sheet1 Cell I wanna reference to = B4

Then I want to fill the new cell with: =sheet1!B4

sheet1 is in a variable.

When I try ActiveCell.FormulaR1C1 = "=" & variable & "!B4", then it fills the cell with =sheet1'!B4'

I get the #Name? error because of the ' '

1 Answer 1

4

In the first case, you need to work out where to put double quotes:

    ActiveCell.FormulaR1C1 = "=CONCATENATE(""Hello"",""" & Variable & """,""Community"")"

In the second case, you need to work out where to put single quotes, and change the property to Formula instead of FormulaR1C1:

    ActiveCell.Formula = "='" & Variable & "'!B4"
Sign up to request clarification or add additional context in comments.

1 Comment

You sir are awesome! Worked like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.