Concatenating reference with formula gives runtime errorHow to create strings containing double quotes in Excel formulas?Excel formula to reference 'CELL TO THE LEFT'Return empty cell from formula in ExcelError in finding last used cell in Excel with VBAHow to add excel range as a picture to outlook message bodyExcel VBA to search for up to 15 values in one searchExcel VBA macro to send emails to unique users in rangeI need to save a file to a folder - this file cannot overwrite an existing file
Can a Rogue exploit a tiny familiar for automatic Sneak Attack in melee?
Producing a print layout using 2nd map view in QGIS
How to equalize the chance of throwing the highest dice? (Riddle)
Suppose I capture encrypted data that I want to decrypt. Could I use a server farm to decrypt?
What is the largest piece of space debris volumetrically?
My advisor wants me to make my PhD thesis weaker
Is my friend from Egypt with a Schengen visa allowed to visit the UK?
Why don't electrical receptacles have more than one ground?
What is the quickest way to raise Affection?
Because things smell, is everything evaporating?
How to evaluate math equation, one per line in a file?
How to pass Collection of exceptions as a root cause?
What does können correspond to in this sentence?
Can a Way of the Open Hand monk's Open Hand Technique prevent Legendary Action reactions?
Adding something to a book from an unpublished paper
Someone said to me, "We basically literally did." What were they trying to express to me?
Who is Gail Gasram?
A bob hanging in an accelerating train moves backward. What is the force moving it backward?
Did any of the Space Shuttles land through rain or rainclouds?
What's a good use case for SELECT * in production code?
How can you weaponize a thermos?
Why is it ethical for Ambassador Sondland to have been given an ambassadorship for campaign contributions?
Memory models for assembly libraries for Turbo C
If password expiration is applied, should door-lock expiration be applied too?
Concatenating reference with formula gives runtime error
How to create strings containing double quotes in Excel formulas?Excel formula to reference 'CELL TO THE LEFT'Return empty cell from formula in ExcelError in finding last used cell in Excel with VBAHow to add excel range as a picture to outlook message bodyExcel VBA to search for up to 15 values in one searchExcel VBA macro to send emails to unique users in rangeI need to save a file to a folder - this file cannot overwrite an existing file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I'm trying to use VBA to concatenate a string inside a formula. If I only use the code below i'm not getting any errors but when i add the IFERROR together with the code I get a runtime error.
Is there any way to work around it?
text1 = "='C:UsersJOHLA\DesktopYield arkNyt-yield-ark[Yield-Uge-"
text2 = ".xlsm]Scrap'!H7"
The code including string with IFERROR that gives runtime error is given below.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Integer
Dim preRange As Range
Dim path, filename1 As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set ws = ActiveWorkbook.Sheets("Sheet1")
Set preRange = ws.Range("E9:I17")
i = ws.Range("C1").Value
text1 = "=IFERROR('C:UsersJOHLADesktopYield arkNyt-yield-ark[Yield-Uge-"
text2 = ".xlsm]Scrap'!H7;0)"
With ws
For i = .Range("C1").Value To .Range("C1").Value + 4
debug.print text1 & i & text2
preRange = text1 & i & text2
Set preRange = preRange.Offset(0, 5)
Next i
End With
End Sub
excel vba excel-formula
|
show 1 more comment
I'm trying to use VBA to concatenate a string inside a formula. If I only use the code below i'm not getting any errors but when i add the IFERROR together with the code I get a runtime error.
Is there any way to work around it?
text1 = "='C:UsersJOHLA\DesktopYield arkNyt-yield-ark[Yield-Uge-"
text2 = ".xlsm]Scrap'!H7"
The code including string with IFERROR that gives runtime error is given below.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Integer
Dim preRange As Range
Dim path, filename1 As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set ws = ActiveWorkbook.Sheets("Sheet1")
Set preRange = ws.Range("E9:I17")
i = ws.Range("C1").Value
text1 = "=IFERROR('C:UsersJOHLADesktopYield arkNyt-yield-ark[Yield-Uge-"
text2 = ".xlsm]Scrap'!H7;0)"
With ws
For i = .Range("C1").Value To .Range("C1").Value + 4
debug.print text1 & i & text2
preRange = text1 & i & text2
Set preRange = preRange.Offset(0, 5)
Next i
End With
End Sub
excel vba excel-formula
1
Debug this codetext1 & i & text2
you'll see there are many errors such as you haveJ0HLA\Desktop
when it should beJ0HLADesktop
and yourIFERROR
ends like;")
– Damian
Mar 26 at 12:27
1
Note that quotation marks in formulas in VBA should be doubled. E.g."=IF(A1=2,"","OK")"
should be"=IF(A1=2,"""",""OK"")"
– Tim Stack
Mar 26 at 12:33
I've updated the code, but still getting runtime error "1004". Tim Stack the syntax should be alright. If i write i manually the error doesn't occur. I can copy the print from my debug and then it works.
– Johan Larsen
Mar 26 at 12:54
Have you triedpreRange.Formula1:= text1 & i & text2
?
– Tim Stack
Mar 26 at 13:10
I'm not exactly sure for formula1 is supposed to work? The description on the function is very vague.
– Johan Larsen
Mar 26 at 13:34
|
show 1 more comment
I'm trying to use VBA to concatenate a string inside a formula. If I only use the code below i'm not getting any errors but when i add the IFERROR together with the code I get a runtime error.
Is there any way to work around it?
text1 = "='C:UsersJOHLA\DesktopYield arkNyt-yield-ark[Yield-Uge-"
text2 = ".xlsm]Scrap'!H7"
The code including string with IFERROR that gives runtime error is given below.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Integer
Dim preRange As Range
Dim path, filename1 As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set ws = ActiveWorkbook.Sheets("Sheet1")
Set preRange = ws.Range("E9:I17")
i = ws.Range("C1").Value
text1 = "=IFERROR('C:UsersJOHLADesktopYield arkNyt-yield-ark[Yield-Uge-"
text2 = ".xlsm]Scrap'!H7;0)"
With ws
For i = .Range("C1").Value To .Range("C1").Value + 4
debug.print text1 & i & text2
preRange = text1 & i & text2
Set preRange = preRange.Offset(0, 5)
Next i
End With
End Sub
excel vba excel-formula
I'm trying to use VBA to concatenate a string inside a formula. If I only use the code below i'm not getting any errors but when i add the IFERROR together with the code I get a runtime error.
Is there any way to work around it?
text1 = "='C:UsersJOHLA\DesktopYield arkNyt-yield-ark[Yield-Uge-"
text2 = ".xlsm]Scrap'!H7"
The code including string with IFERROR that gives runtime error is given below.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Integer
Dim preRange As Range
Dim path, filename1 As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set ws = ActiveWorkbook.Sheets("Sheet1")
Set preRange = ws.Range("E9:I17")
i = ws.Range("C1").Value
text1 = "=IFERROR('C:UsersJOHLADesktopYield arkNyt-yield-ark[Yield-Uge-"
text2 = ".xlsm]Scrap'!H7;0)"
With ws
For i = .Range("C1").Value To .Range("C1").Value + 4
debug.print text1 & i & text2
preRange = text1 & i & text2
Set preRange = preRange.Offset(0, 5)
Next i
End With
End Sub
excel vba excel-formula
excel vba excel-formula
edited Mar 26 at 13:12
Johan Larsen
asked Mar 26 at 12:22


Johan LarsenJohan Larsen
134 bronze badges
134 bronze badges
1
Debug this codetext1 & i & text2
you'll see there are many errors such as you haveJ0HLA\Desktop
when it should beJ0HLADesktop
and yourIFERROR
ends like;")
– Damian
Mar 26 at 12:27
1
Note that quotation marks in formulas in VBA should be doubled. E.g."=IF(A1=2,"","OK")"
should be"=IF(A1=2,"""",""OK"")"
– Tim Stack
Mar 26 at 12:33
I've updated the code, but still getting runtime error "1004". Tim Stack the syntax should be alright. If i write i manually the error doesn't occur. I can copy the print from my debug and then it works.
– Johan Larsen
Mar 26 at 12:54
Have you triedpreRange.Formula1:= text1 & i & text2
?
– Tim Stack
Mar 26 at 13:10
I'm not exactly sure for formula1 is supposed to work? The description on the function is very vague.
– Johan Larsen
Mar 26 at 13:34
|
show 1 more comment
1
Debug this codetext1 & i & text2
you'll see there are many errors such as you haveJ0HLA\Desktop
when it should beJ0HLADesktop
and yourIFERROR
ends like;")
– Damian
Mar 26 at 12:27
1
Note that quotation marks in formulas in VBA should be doubled. E.g."=IF(A1=2,"","OK")"
should be"=IF(A1=2,"""",""OK"")"
– Tim Stack
Mar 26 at 12:33
I've updated the code, but still getting runtime error "1004". Tim Stack the syntax should be alright. If i write i manually the error doesn't occur. I can copy the print from my debug and then it works.
– Johan Larsen
Mar 26 at 12:54
Have you triedpreRange.Formula1:= text1 & i & text2
?
– Tim Stack
Mar 26 at 13:10
I'm not exactly sure for formula1 is supposed to work? The description on the function is very vague.
– Johan Larsen
Mar 26 at 13:34
1
1
Debug this code
text1 & i & text2
you'll see there are many errors such as you have J0HLA\Desktop
when it should be J0HLADesktop
and your IFERROR
ends like ;")
– Damian
Mar 26 at 12:27
Debug this code
text1 & i & text2
you'll see there are many errors such as you have J0HLA\Desktop
when it should be J0HLADesktop
and your IFERROR
ends like ;")
– Damian
Mar 26 at 12:27
1
1
Note that quotation marks in formulas in VBA should be doubled. E.g.
"=IF(A1=2,"","OK")"
should be "=IF(A1=2,"""",""OK"")"
– Tim Stack
Mar 26 at 12:33
Note that quotation marks in formulas in VBA should be doubled. E.g.
"=IF(A1=2,"","OK")"
should be "=IF(A1=2,"""",""OK"")"
– Tim Stack
Mar 26 at 12:33
I've updated the code, but still getting runtime error "1004". Tim Stack the syntax should be alright. If i write i manually the error doesn't occur. I can copy the print from my debug and then it works.
– Johan Larsen
Mar 26 at 12:54
I've updated the code, but still getting runtime error "1004". Tim Stack the syntax should be alright. If i write i manually the error doesn't occur. I can copy the print from my debug and then it works.
– Johan Larsen
Mar 26 at 12:54
Have you tried
preRange.Formula1:= text1 & i & text2
?– Tim Stack
Mar 26 at 13:10
Have you tried
preRange.Formula1:= text1 & i & text2
?– Tim Stack
Mar 26 at 13:10
I'm not exactly sure for formula1 is supposed to work? The description on the function is very vague.
– Johan Larsen
Mar 26 at 13:34
I'm not exactly sure for formula1 is supposed to work? The description on the function is very vague.
– Johan Larsen
Mar 26 at 13:34
|
show 1 more comment
2 Answers
2
active
oldest
votes
Judging by your use of semicolon in your formula, it would suggest that you're using local settings which are not compatible with VBA.Formula
in this case, you either need to change the formula to use a comma or set the formula using FormulaLocal
:
preRange.FormulaLocal = Replace(text1 & i & text2, "'", Chr(34))
As you can see, I've also added a Replace
that changes '
into "
- as I think you need this also.
Lastly, don't forget to enable ScreenUpdating
and DisplayAlerts
at the end of your routine.
1
FormulaLocal was the final piece to the puzzle! I'm referencing to a cell in another closed workbook and that's why the " ' " is in both endes of the filepath. This is just a codesnippet so i've got screenUpdating and DisplayAlerts futher down.
– Johan Larsen
Mar 26 at 14:32
Glad you sorted it out.
– CLR
Mar 26 at 14:40
1
Make sure that you're using good error handling around those ScreenUpdating and DisplayAlerts calls. If you aren't making sure that you always reset them, an error in your code will leave Excel in a really bad state.
– Frank Ball
Mar 26 at 14:55
@FrankBall I'm new at programming, but could it be solved by a IF statement where i check if it is true or false?
– Johan Larsen
Mar 26 at 15:01
No need for anIf
really. If you decide to add an error trap to your code, just set them toTrue
. If they'reTrue
already, there's no harm.
– CLR
Mar 26 at 15:41
|
show 1 more comment
Any time you use
Application.ScreenUpdating = False
Application.DisplayAlerts = False
you need to make sure that you are using error handling to manage if your code breaks.
On Error GoTo ExitErr
Application.ScreenUpdating = False
Application.DisplayAlerts = False
<your code here>
ExitErr:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
This makes sure that if your code breaks (in your case the most obvious being someone renaming "Sheet1"), Excel isn't left with ScreenUpdating and DisplayAlerts left turned off. I can't tell you how many times I've had to fix other people's code because they turned off these functions and then couldn't figure out why Excel was acting up.
You're setting them toFalse
in yourExitErr
..? I think you meanTrue
?
– CLR
Mar 27 at 10:36
I would have to make them true in exiterr, right?
– Johan Larsen
Mar 28 at 6:31
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357064%2fconcatenating-reference-with-formula-gives-runtime-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Judging by your use of semicolon in your formula, it would suggest that you're using local settings which are not compatible with VBA.Formula
in this case, you either need to change the formula to use a comma or set the formula using FormulaLocal
:
preRange.FormulaLocal = Replace(text1 & i & text2, "'", Chr(34))
As you can see, I've also added a Replace
that changes '
into "
- as I think you need this also.
Lastly, don't forget to enable ScreenUpdating
and DisplayAlerts
at the end of your routine.
1
FormulaLocal was the final piece to the puzzle! I'm referencing to a cell in another closed workbook and that's why the " ' " is in both endes of the filepath. This is just a codesnippet so i've got screenUpdating and DisplayAlerts futher down.
– Johan Larsen
Mar 26 at 14:32
Glad you sorted it out.
– CLR
Mar 26 at 14:40
1
Make sure that you're using good error handling around those ScreenUpdating and DisplayAlerts calls. If you aren't making sure that you always reset them, an error in your code will leave Excel in a really bad state.
– Frank Ball
Mar 26 at 14:55
@FrankBall I'm new at programming, but could it be solved by a IF statement where i check if it is true or false?
– Johan Larsen
Mar 26 at 15:01
No need for anIf
really. If you decide to add an error trap to your code, just set them toTrue
. If they'reTrue
already, there's no harm.
– CLR
Mar 26 at 15:41
|
show 1 more comment
Judging by your use of semicolon in your formula, it would suggest that you're using local settings which are not compatible with VBA.Formula
in this case, you either need to change the formula to use a comma or set the formula using FormulaLocal
:
preRange.FormulaLocal = Replace(text1 & i & text2, "'", Chr(34))
As you can see, I've also added a Replace
that changes '
into "
- as I think you need this also.
Lastly, don't forget to enable ScreenUpdating
and DisplayAlerts
at the end of your routine.
1
FormulaLocal was the final piece to the puzzle! I'm referencing to a cell in another closed workbook and that's why the " ' " is in both endes of the filepath. This is just a codesnippet so i've got screenUpdating and DisplayAlerts futher down.
– Johan Larsen
Mar 26 at 14:32
Glad you sorted it out.
– CLR
Mar 26 at 14:40
1
Make sure that you're using good error handling around those ScreenUpdating and DisplayAlerts calls. If you aren't making sure that you always reset them, an error in your code will leave Excel in a really bad state.
– Frank Ball
Mar 26 at 14:55
@FrankBall I'm new at programming, but could it be solved by a IF statement where i check if it is true or false?
– Johan Larsen
Mar 26 at 15:01
No need for anIf
really. If you decide to add an error trap to your code, just set them toTrue
. If they'reTrue
already, there's no harm.
– CLR
Mar 26 at 15:41
|
show 1 more comment
Judging by your use of semicolon in your formula, it would suggest that you're using local settings which are not compatible with VBA.Formula
in this case, you either need to change the formula to use a comma or set the formula using FormulaLocal
:
preRange.FormulaLocal = Replace(text1 & i & text2, "'", Chr(34))
As you can see, I've also added a Replace
that changes '
into "
- as I think you need this also.
Lastly, don't forget to enable ScreenUpdating
and DisplayAlerts
at the end of your routine.
Judging by your use of semicolon in your formula, it would suggest that you're using local settings which are not compatible with VBA.Formula
in this case, you either need to change the formula to use a comma or set the formula using FormulaLocal
:
preRange.FormulaLocal = Replace(text1 & i & text2, "'", Chr(34))
As you can see, I've also added a Replace
that changes '
into "
- as I think you need this also.
Lastly, don't forget to enable ScreenUpdating
and DisplayAlerts
at the end of your routine.
edited Mar 26 at 13:56
answered Mar 26 at 13:49


CLRCLR
7,2891 gold badge4 silver badges21 bronze badges
7,2891 gold badge4 silver badges21 bronze badges
1
FormulaLocal was the final piece to the puzzle! I'm referencing to a cell in another closed workbook and that's why the " ' " is in both endes of the filepath. This is just a codesnippet so i've got screenUpdating and DisplayAlerts futher down.
– Johan Larsen
Mar 26 at 14:32
Glad you sorted it out.
– CLR
Mar 26 at 14:40
1
Make sure that you're using good error handling around those ScreenUpdating and DisplayAlerts calls. If you aren't making sure that you always reset them, an error in your code will leave Excel in a really bad state.
– Frank Ball
Mar 26 at 14:55
@FrankBall I'm new at programming, but could it be solved by a IF statement where i check if it is true or false?
– Johan Larsen
Mar 26 at 15:01
No need for anIf
really. If you decide to add an error trap to your code, just set them toTrue
. If they'reTrue
already, there's no harm.
– CLR
Mar 26 at 15:41
|
show 1 more comment
1
FormulaLocal was the final piece to the puzzle! I'm referencing to a cell in another closed workbook and that's why the " ' " is in both endes of the filepath. This is just a codesnippet so i've got screenUpdating and DisplayAlerts futher down.
– Johan Larsen
Mar 26 at 14:32
Glad you sorted it out.
– CLR
Mar 26 at 14:40
1
Make sure that you're using good error handling around those ScreenUpdating and DisplayAlerts calls. If you aren't making sure that you always reset them, an error in your code will leave Excel in a really bad state.
– Frank Ball
Mar 26 at 14:55
@FrankBall I'm new at programming, but could it be solved by a IF statement where i check if it is true or false?
– Johan Larsen
Mar 26 at 15:01
No need for anIf
really. If you decide to add an error trap to your code, just set them toTrue
. If they'reTrue
already, there's no harm.
– CLR
Mar 26 at 15:41
1
1
FormulaLocal was the final piece to the puzzle! I'm referencing to a cell in another closed workbook and that's why the " ' " is in both endes of the filepath. This is just a codesnippet so i've got screenUpdating and DisplayAlerts futher down.
– Johan Larsen
Mar 26 at 14:32
FormulaLocal was the final piece to the puzzle! I'm referencing to a cell in another closed workbook and that's why the " ' " is in both endes of the filepath. This is just a codesnippet so i've got screenUpdating and DisplayAlerts futher down.
– Johan Larsen
Mar 26 at 14:32
Glad you sorted it out.
– CLR
Mar 26 at 14:40
Glad you sorted it out.
– CLR
Mar 26 at 14:40
1
1
Make sure that you're using good error handling around those ScreenUpdating and DisplayAlerts calls. If you aren't making sure that you always reset them, an error in your code will leave Excel in a really bad state.
– Frank Ball
Mar 26 at 14:55
Make sure that you're using good error handling around those ScreenUpdating and DisplayAlerts calls. If you aren't making sure that you always reset them, an error in your code will leave Excel in a really bad state.
– Frank Ball
Mar 26 at 14:55
@FrankBall I'm new at programming, but could it be solved by a IF statement where i check if it is true or false?
– Johan Larsen
Mar 26 at 15:01
@FrankBall I'm new at programming, but could it be solved by a IF statement where i check if it is true or false?
– Johan Larsen
Mar 26 at 15:01
No need for an
If
really. If you decide to add an error trap to your code, just set them to True
. If they're True
already, there's no harm.– CLR
Mar 26 at 15:41
No need for an
If
really. If you decide to add an error trap to your code, just set them to True
. If they're True
already, there's no harm.– CLR
Mar 26 at 15:41
|
show 1 more comment
Any time you use
Application.ScreenUpdating = False
Application.DisplayAlerts = False
you need to make sure that you are using error handling to manage if your code breaks.
On Error GoTo ExitErr
Application.ScreenUpdating = False
Application.DisplayAlerts = False
<your code here>
ExitErr:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
This makes sure that if your code breaks (in your case the most obvious being someone renaming "Sheet1"), Excel isn't left with ScreenUpdating and DisplayAlerts left turned off. I can't tell you how many times I've had to fix other people's code because they turned off these functions and then couldn't figure out why Excel was acting up.
You're setting them toFalse
in yourExitErr
..? I think you meanTrue
?
– CLR
Mar 27 at 10:36
I would have to make them true in exiterr, right?
– Johan Larsen
Mar 28 at 6:31
add a comment
|
Any time you use
Application.ScreenUpdating = False
Application.DisplayAlerts = False
you need to make sure that you are using error handling to manage if your code breaks.
On Error GoTo ExitErr
Application.ScreenUpdating = False
Application.DisplayAlerts = False
<your code here>
ExitErr:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
This makes sure that if your code breaks (in your case the most obvious being someone renaming "Sheet1"), Excel isn't left with ScreenUpdating and DisplayAlerts left turned off. I can't tell you how many times I've had to fix other people's code because they turned off these functions and then couldn't figure out why Excel was acting up.
You're setting them toFalse
in yourExitErr
..? I think you meanTrue
?
– CLR
Mar 27 at 10:36
I would have to make them true in exiterr, right?
– Johan Larsen
Mar 28 at 6:31
add a comment
|
Any time you use
Application.ScreenUpdating = False
Application.DisplayAlerts = False
you need to make sure that you are using error handling to manage if your code breaks.
On Error GoTo ExitErr
Application.ScreenUpdating = False
Application.DisplayAlerts = False
<your code here>
ExitErr:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
This makes sure that if your code breaks (in your case the most obvious being someone renaming "Sheet1"), Excel isn't left with ScreenUpdating and DisplayAlerts left turned off. I can't tell you how many times I've had to fix other people's code because they turned off these functions and then couldn't figure out why Excel was acting up.
Any time you use
Application.ScreenUpdating = False
Application.DisplayAlerts = False
you need to make sure that you are using error handling to manage if your code breaks.
On Error GoTo ExitErr
Application.ScreenUpdating = False
Application.DisplayAlerts = False
<your code here>
ExitErr:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
This makes sure that if your code breaks (in your case the most obvious being someone renaming "Sheet1"), Excel isn't left with ScreenUpdating and DisplayAlerts left turned off. I can't tell you how many times I've had to fix other people's code because they turned off these functions and then couldn't figure out why Excel was acting up.
edited Mar 28 at 22:04
answered Mar 26 at 20:24
Frank BallFrank Ball
7515 silver badges14 bronze badges
7515 silver badges14 bronze badges
You're setting them toFalse
in yourExitErr
..? I think you meanTrue
?
– CLR
Mar 27 at 10:36
I would have to make them true in exiterr, right?
– Johan Larsen
Mar 28 at 6:31
add a comment
|
You're setting them toFalse
in yourExitErr
..? I think you meanTrue
?
– CLR
Mar 27 at 10:36
I would have to make them true in exiterr, right?
– Johan Larsen
Mar 28 at 6:31
You're setting them to
False
in your ExitErr
..? I think you mean True
?– CLR
Mar 27 at 10:36
You're setting them to
False
in your ExitErr
..? I think you mean True
?– CLR
Mar 27 at 10:36
I would have to make them true in exiterr, right?
– Johan Larsen
Mar 28 at 6:31
I would have to make them true in exiterr, right?
– Johan Larsen
Mar 28 at 6:31
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357064%2fconcatenating-reference-with-formula-gives-runtime-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Debug this code
text1 & i & text2
you'll see there are many errors such as you haveJ0HLA\Desktop
when it should beJ0HLADesktop
and yourIFERROR
ends like;")
– Damian
Mar 26 at 12:27
1
Note that quotation marks in formulas in VBA should be doubled. E.g.
"=IF(A1=2,"","OK")"
should be"=IF(A1=2,"""",""OK"")"
– Tim Stack
Mar 26 at 12:33
I've updated the code, but still getting runtime error "1004". Tim Stack the syntax should be alright. If i write i manually the error doesn't occur. I can copy the print from my debug and then it works.
– Johan Larsen
Mar 26 at 12:54
Have you tried
preRange.Formula1:= text1 & i & text2
?– Tim Stack
Mar 26 at 13:10
I'm not exactly sure for formula1 is supposed to work? The description on the function is very vague.
– Johan Larsen
Mar 26 at 13:34