Use relative references












2















Being new to VBA I would love some inputs to this code, to improve the speed of it... It doesn't feel so "VBA"-ish currently; however the "result" of the code correct...



Sub Rigtig()

Set Marketshare = Sheets("Output").Range("p40:p50")
'Select.
Sheets("Output").Select
Cells(38, 17).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 17).Copy
Cells(40, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(41, 17).Copy
Cells(41, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(3, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues


Sheets("Output").Select
Cells(38, 18).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 18).Copy
Cells(40, 18).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Cells(38, 19).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 19).Copy
Cells(40, 19).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues


I would like to do the same "copy paste" approx 10 times in rows, and then change the column.



Thanks in advance



Best



Valdemar










share|improve this question




















  • 2





    Rather than doing a load of Copy and Pasting, you should update cell values directly... Also avoid using select wherever possible - for example the first copy and past can be written Sheets("Output").Cells(33, 28).Value = Sheets("Output").Cells(38, 17).Value or even With Sheets("Output") .Cells(33, 28).Value = .Cells(38, 17).Value End With

    – Glitch_Doctor
    Nov 16 '18 at 9:36













  • It's a good idea not to use "Copy/Paste" within a VBA script: just imagine the following: you copy something on the clipboard, you launch a program, and your copy is lost (you can't paste anymore). If you don't use "Copy/Paste" in a script, the clipboard is not touched and you can still paste what you have copied before.

    – Dominique
    Nov 20 '18 at 14:52











  • Hi Dominique, thanks, i'll keep that in mind. Do you have any suggestions as to how I can create a loop instead of repeating the code just replacing the row number? I've tried to explain my next step in a new answer

    – Valdemar E. Stage
    Nov 20 '18 at 15:08


















2















Being new to VBA I would love some inputs to this code, to improve the speed of it... It doesn't feel so "VBA"-ish currently; however the "result" of the code correct...



Sub Rigtig()

Set Marketshare = Sheets("Output").Range("p40:p50")
'Select.
Sheets("Output").Select
Cells(38, 17).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 17).Copy
Cells(40, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(41, 17).Copy
Cells(41, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(3, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues


Sheets("Output").Select
Cells(38, 18).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 18).Copy
Cells(40, 18).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Cells(38, 19).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 19).Copy
Cells(40, 19).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues


I would like to do the same "copy paste" approx 10 times in rows, and then change the column.



Thanks in advance



Best



Valdemar










share|improve this question




















  • 2





    Rather than doing a load of Copy and Pasting, you should update cell values directly... Also avoid using select wherever possible - for example the first copy and past can be written Sheets("Output").Cells(33, 28).Value = Sheets("Output").Cells(38, 17).Value or even With Sheets("Output") .Cells(33, 28).Value = .Cells(38, 17).Value End With

    – Glitch_Doctor
    Nov 16 '18 at 9:36













  • It's a good idea not to use "Copy/Paste" within a VBA script: just imagine the following: you copy something on the clipboard, you launch a program, and your copy is lost (you can't paste anymore). If you don't use "Copy/Paste" in a script, the clipboard is not touched and you can still paste what you have copied before.

    – Dominique
    Nov 20 '18 at 14:52











  • Hi Dominique, thanks, i'll keep that in mind. Do you have any suggestions as to how I can create a loop instead of repeating the code just replacing the row number? I've tried to explain my next step in a new answer

    – Valdemar E. Stage
    Nov 20 '18 at 15:08
















2












2








2








Being new to VBA I would love some inputs to this code, to improve the speed of it... It doesn't feel so "VBA"-ish currently; however the "result" of the code correct...



Sub Rigtig()

Set Marketshare = Sheets("Output").Range("p40:p50")
'Select.
Sheets("Output").Select
Cells(38, 17).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 17).Copy
Cells(40, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(41, 17).Copy
Cells(41, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(3, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues


Sheets("Output").Select
Cells(38, 18).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 18).Copy
Cells(40, 18).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Cells(38, 19).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 19).Copy
Cells(40, 19).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues


I would like to do the same "copy paste" approx 10 times in rows, and then change the column.



Thanks in advance



Best



Valdemar










share|improve this question
















Being new to VBA I would love some inputs to this code, to improve the speed of it... It doesn't feel so "VBA"-ish currently; however the "result" of the code correct...



Sub Rigtig()

Set Marketshare = Sheets("Output").Range("p40:p50")
'Select.
Sheets("Output").Select
Cells(38, 17).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 17).Copy
Cells(40, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(41, 17).Copy
Cells(41, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(3, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues


Sheets("Output").Select
Cells(38, 18).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 18).Copy
Cells(40, 18).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Cells(38, 19).Copy
Sheets("Input").Select
Cells(33, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
Cells(40, 19).Copy
Cells(40, 19).Select
Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
Cells(23, 28).Select
Selection.PasteSpecial Paste:=xlPasteValues


I would like to do the same "copy paste" approx 10 times in rows, and then change the column.



Thanks in advance



Best



Valdemar







excel vba performance loops simplify






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 21:23









marc_s

583k13011241270




583k13011241270










asked Nov 16 '18 at 9:34









Valdemar E. StageValdemar E. Stage

143




143








  • 2





    Rather than doing a load of Copy and Pasting, you should update cell values directly... Also avoid using select wherever possible - for example the first copy and past can be written Sheets("Output").Cells(33, 28).Value = Sheets("Output").Cells(38, 17).Value or even With Sheets("Output") .Cells(33, 28).Value = .Cells(38, 17).Value End With

    – Glitch_Doctor
    Nov 16 '18 at 9:36













  • It's a good idea not to use "Copy/Paste" within a VBA script: just imagine the following: you copy something on the clipboard, you launch a program, and your copy is lost (you can't paste anymore). If you don't use "Copy/Paste" in a script, the clipboard is not touched and you can still paste what you have copied before.

    – Dominique
    Nov 20 '18 at 14:52











  • Hi Dominique, thanks, i'll keep that in mind. Do you have any suggestions as to how I can create a loop instead of repeating the code just replacing the row number? I've tried to explain my next step in a new answer

    – Valdemar E. Stage
    Nov 20 '18 at 15:08
















  • 2





    Rather than doing a load of Copy and Pasting, you should update cell values directly... Also avoid using select wherever possible - for example the first copy and past can be written Sheets("Output").Cells(33, 28).Value = Sheets("Output").Cells(38, 17).Value or even With Sheets("Output") .Cells(33, 28).Value = .Cells(38, 17).Value End With

    – Glitch_Doctor
    Nov 16 '18 at 9:36













  • It's a good idea not to use "Copy/Paste" within a VBA script: just imagine the following: you copy something on the clipboard, you launch a program, and your copy is lost (you can't paste anymore). If you don't use "Copy/Paste" in a script, the clipboard is not touched and you can still paste what you have copied before.

    – Dominique
    Nov 20 '18 at 14:52











  • Hi Dominique, thanks, i'll keep that in mind. Do you have any suggestions as to how I can create a loop instead of repeating the code just replacing the row number? I've tried to explain my next step in a new answer

    – Valdemar E. Stage
    Nov 20 '18 at 15:08










2




2





Rather than doing a load of Copy and Pasting, you should update cell values directly... Also avoid using select wherever possible - for example the first copy and past can be written Sheets("Output").Cells(33, 28).Value = Sheets("Output").Cells(38, 17).Value or even With Sheets("Output") .Cells(33, 28).Value = .Cells(38, 17).Value End With

– Glitch_Doctor
Nov 16 '18 at 9:36







Rather than doing a load of Copy and Pasting, you should update cell values directly... Also avoid using select wherever possible - for example the first copy and past can be written Sheets("Output").Cells(33, 28).Value = Sheets("Output").Cells(38, 17).Value or even With Sheets("Output") .Cells(33, 28).Value = .Cells(38, 17).Value End With

– Glitch_Doctor
Nov 16 '18 at 9:36















It's a good idea not to use "Copy/Paste" within a VBA script: just imagine the following: you copy something on the clipboard, you launch a program, and your copy is lost (you can't paste anymore). If you don't use "Copy/Paste" in a script, the clipboard is not touched and you can still paste what you have copied before.

– Dominique
Nov 20 '18 at 14:52





It's a good idea not to use "Copy/Paste" within a VBA script: just imagine the following: you copy something on the clipboard, you launch a program, and your copy is lost (you can't paste anymore). If you don't use "Copy/Paste" in a script, the clipboard is not touched and you can still paste what you have copied before.

– Dominique
Nov 20 '18 at 14:52













Hi Dominique, thanks, i'll keep that in mind. Do you have any suggestions as to how I can create a loop instead of repeating the code just replacing the row number? I've tried to explain my next step in a new answer

– Valdemar E. Stage
Nov 20 '18 at 15:08







Hi Dominique, thanks, i'll keep that in mind. Do you have any suggestions as to how I can create a loop instead of repeating the code just replacing the row number? I've tried to explain my next step in a new answer

– Valdemar E. Stage
Nov 20 '18 at 15:08














2 Answers
2






active

oldest

votes


















1














As @Glitch_Doctor said - if it's just the values you're after you can do this "this cell = that cell" rather than copy/paste.



To shorten your code and make it a bit more "VBA"-ish you could put your cell reference pairs into an array and step through the array:



Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("A1", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

End Sub


You could also use a With...End With block so you don't have to use the sheet name each time:



Sub Test1()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28) = .Cells(38, 17)
End With

End Sub


If you want to copy everything (formula, formatting) then use Copy & paste in a single line:



Sub Test2()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28).Copy Destination:=.Cells(38, 17)
End With

End Sub





share|improve this answer
























  • Hi Darren, thanks for your response. What im trying to do, is to get VBA to insert a value in a cell which then calculates a value for me. Then I want to "save" this value as a number, since the next value that gets calculated is with the same formula (same input cell but new input value). So in order to get the new value without changing the old value I guess this is necessary? So what i'm basically trying to get i a matrix, where it fills the rows first (with the corresponding input) and then change column input and do it all over again...

    – Valdemar E. Stage
    Nov 20 '18 at 9:52













  • Not sure if this makes sense? I tried your code but it does not return the values I wanted :)

    – Valdemar E. Stage
    Nov 20 '18 at 9:58











  • The code you posted doesn't insert any values into cells - only the copying/pasting way. My first block of code replicates that - copies the value from AB33 to Q38 and so on. Other than that how are values inserted?

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:04











  • Hmm.. Im stil not quite sure what your first block does... Not really sure about the cell references, are they supposed to copy the same as mine?

    – Valdemar E. Stage
    Nov 20 '18 at 10:23











  • Yes, The For...Each line looks at each array of two addresses in turn and moves the values from one cell reference to the other. You could also replicate your code using Sheets("Input").Cells(33, 28) = Sheets("Output").Cells(38, 17) which replaces the first copy/paste block in your code.

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:37



















0














So I want it to be something like this:



    Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P40", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(40, 17).Value = Sheets("Output").Cells(40, 17).Value

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P41", "AB23"))
For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(41, 17).Value = Sheets("Output").Cells(41, 17).Value


Does this makes sense?



So repeating it 10x down in rows, and then move 1 column left and do it again.



Hope you get my point, and thanks :)



/Valdemar






share|improve this answer
























  • So, the next "step" here is to make this a "loop" so I wont have to repeat the code 10 times, changing P41 -> P42 and so forth; and next changing Q38 -> R38 (and fx Cells(41, 18)

    – Valdemar E. Stage
    Nov 20 '18 at 14:55






  • 1





    Please don't post questions as answers. Post a new question if you are moving beyond the scope of your original.

    – SJR
    Nov 27 '18 at 14:35












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/3.0/"u003ecc by-sa 3.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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335006%2fuse-relative-references%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









1














As @Glitch_Doctor said - if it's just the values you're after you can do this "this cell = that cell" rather than copy/paste.



To shorten your code and make it a bit more "VBA"-ish you could put your cell reference pairs into an array and step through the array:



Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("A1", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

End Sub


You could also use a With...End With block so you don't have to use the sheet name each time:



Sub Test1()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28) = .Cells(38, 17)
End With

End Sub


If you want to copy everything (formula, formatting) then use Copy & paste in a single line:



Sub Test2()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28).Copy Destination:=.Cells(38, 17)
End With

End Sub





share|improve this answer
























  • Hi Darren, thanks for your response. What im trying to do, is to get VBA to insert a value in a cell which then calculates a value for me. Then I want to "save" this value as a number, since the next value that gets calculated is with the same formula (same input cell but new input value). So in order to get the new value without changing the old value I guess this is necessary? So what i'm basically trying to get i a matrix, where it fills the rows first (with the corresponding input) and then change column input and do it all over again...

    – Valdemar E. Stage
    Nov 20 '18 at 9:52













  • Not sure if this makes sense? I tried your code but it does not return the values I wanted :)

    – Valdemar E. Stage
    Nov 20 '18 at 9:58











  • The code you posted doesn't insert any values into cells - only the copying/pasting way. My first block of code replicates that - copies the value from AB33 to Q38 and so on. Other than that how are values inserted?

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:04











  • Hmm.. Im stil not quite sure what your first block does... Not really sure about the cell references, are they supposed to copy the same as mine?

    – Valdemar E. Stage
    Nov 20 '18 at 10:23











  • Yes, The For...Each line looks at each array of two addresses in turn and moves the values from one cell reference to the other. You could also replicate your code using Sheets("Input").Cells(33, 28) = Sheets("Output").Cells(38, 17) which replaces the first copy/paste block in your code.

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:37
















1














As @Glitch_Doctor said - if it's just the values you're after you can do this "this cell = that cell" rather than copy/paste.



To shorten your code and make it a bit more "VBA"-ish you could put your cell reference pairs into an array and step through the array:



Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("A1", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

End Sub


You could also use a With...End With block so you don't have to use the sheet name each time:



Sub Test1()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28) = .Cells(38, 17)
End With

End Sub


If you want to copy everything (formula, formatting) then use Copy & paste in a single line:



Sub Test2()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28).Copy Destination:=.Cells(38, 17)
End With

End Sub





share|improve this answer
























  • Hi Darren, thanks for your response. What im trying to do, is to get VBA to insert a value in a cell which then calculates a value for me. Then I want to "save" this value as a number, since the next value that gets calculated is with the same formula (same input cell but new input value). So in order to get the new value without changing the old value I guess this is necessary? So what i'm basically trying to get i a matrix, where it fills the rows first (with the corresponding input) and then change column input and do it all over again...

    – Valdemar E. Stage
    Nov 20 '18 at 9:52













  • Not sure if this makes sense? I tried your code but it does not return the values I wanted :)

    – Valdemar E. Stage
    Nov 20 '18 at 9:58











  • The code you posted doesn't insert any values into cells - only the copying/pasting way. My first block of code replicates that - copies the value from AB33 to Q38 and so on. Other than that how are values inserted?

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:04











  • Hmm.. Im stil not quite sure what your first block does... Not really sure about the cell references, are they supposed to copy the same as mine?

    – Valdemar E. Stage
    Nov 20 '18 at 10:23











  • Yes, The For...Each line looks at each array of two addresses in turn and moves the values from one cell reference to the other. You could also replicate your code using Sheets("Input").Cells(33, 28) = Sheets("Output").Cells(38, 17) which replaces the first copy/paste block in your code.

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:37














1












1








1







As @Glitch_Doctor said - if it's just the values you're after you can do this "this cell = that cell" rather than copy/paste.



To shorten your code and make it a bit more "VBA"-ish you could put your cell reference pairs into an array and step through the array:



Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("A1", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

End Sub


You could also use a With...End With block so you don't have to use the sheet name each time:



Sub Test1()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28) = .Cells(38, 17)
End With

End Sub


If you want to copy everything (formula, formatting) then use Copy & paste in a single line:



Sub Test2()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28).Copy Destination:=.Cells(38, 17)
End With

End Sub





share|improve this answer













As @Glitch_Doctor said - if it's just the values you're after you can do this "this cell = that cell" rather than copy/paste.



To shorten your code and make it a bit more "VBA"-ish you could put your cell reference pairs into an array and step through the array:



Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("A1", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

End Sub


You could also use a With...End With block so you don't have to use the sheet name each time:



Sub Test1()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28) = .Cells(38, 17)
End With

End Sub


If you want to copy everything (formula, formatting) then use Copy & paste in a single line:



Sub Test2()

With Worksheets("Output")
Worksheets("Input").Cells(33, 28).Copy Destination:=.Cells(38, 17)
End With

End Sub






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 11:37









Darren Bartrup-CookDarren Bartrup-Cook

14k11533




14k11533













  • Hi Darren, thanks for your response. What im trying to do, is to get VBA to insert a value in a cell which then calculates a value for me. Then I want to "save" this value as a number, since the next value that gets calculated is with the same formula (same input cell but new input value). So in order to get the new value without changing the old value I guess this is necessary? So what i'm basically trying to get i a matrix, where it fills the rows first (with the corresponding input) and then change column input and do it all over again...

    – Valdemar E. Stage
    Nov 20 '18 at 9:52













  • Not sure if this makes sense? I tried your code but it does not return the values I wanted :)

    – Valdemar E. Stage
    Nov 20 '18 at 9:58











  • The code you posted doesn't insert any values into cells - only the copying/pasting way. My first block of code replicates that - copies the value from AB33 to Q38 and so on. Other than that how are values inserted?

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:04











  • Hmm.. Im stil not quite sure what your first block does... Not really sure about the cell references, are they supposed to copy the same as mine?

    – Valdemar E. Stage
    Nov 20 '18 at 10:23











  • Yes, The For...Each line looks at each array of two addresses in turn and moves the values from one cell reference to the other. You could also replicate your code using Sheets("Input").Cells(33, 28) = Sheets("Output").Cells(38, 17) which replaces the first copy/paste block in your code.

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:37



















  • Hi Darren, thanks for your response. What im trying to do, is to get VBA to insert a value in a cell which then calculates a value for me. Then I want to "save" this value as a number, since the next value that gets calculated is with the same formula (same input cell but new input value). So in order to get the new value without changing the old value I guess this is necessary? So what i'm basically trying to get i a matrix, where it fills the rows first (with the corresponding input) and then change column input and do it all over again...

    – Valdemar E. Stage
    Nov 20 '18 at 9:52













  • Not sure if this makes sense? I tried your code but it does not return the values I wanted :)

    – Valdemar E. Stage
    Nov 20 '18 at 9:58











  • The code you posted doesn't insert any values into cells - only the copying/pasting way. My first block of code replicates that - copies the value from AB33 to Q38 and so on. Other than that how are values inserted?

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:04











  • Hmm.. Im stil not quite sure what your first block does... Not really sure about the cell references, are they supposed to copy the same as mine?

    – Valdemar E. Stage
    Nov 20 '18 at 10:23











  • Yes, The For...Each line looks at each array of two addresses in turn and moves the values from one cell reference to the other. You could also replicate your code using Sheets("Input").Cells(33, 28) = Sheets("Output").Cells(38, 17) which replaces the first copy/paste block in your code.

    – Darren Bartrup-Cook
    Nov 20 '18 at 10:37

















Hi Darren, thanks for your response. What im trying to do, is to get VBA to insert a value in a cell which then calculates a value for me. Then I want to "save" this value as a number, since the next value that gets calculated is with the same formula (same input cell but new input value). So in order to get the new value without changing the old value I guess this is necessary? So what i'm basically trying to get i a matrix, where it fills the rows first (with the corresponding input) and then change column input and do it all over again...

– Valdemar E. Stage
Nov 20 '18 at 9:52







Hi Darren, thanks for your response. What im trying to do, is to get VBA to insert a value in a cell which then calculates a value for me. Then I want to "save" this value as a number, since the next value that gets calculated is with the same formula (same input cell but new input value). So in order to get the new value without changing the old value I guess this is necessary? So what i'm basically trying to get i a matrix, where it fills the rows first (with the corresponding input) and then change column input and do it all over again...

– Valdemar E. Stage
Nov 20 '18 at 9:52















Not sure if this makes sense? I tried your code but it does not return the values I wanted :)

– Valdemar E. Stage
Nov 20 '18 at 9:58





Not sure if this makes sense? I tried your code but it does not return the values I wanted :)

– Valdemar E. Stage
Nov 20 '18 at 9:58













The code you posted doesn't insert any values into cells - only the copying/pasting way. My first block of code replicates that - copies the value from AB33 to Q38 and so on. Other than that how are values inserted?

– Darren Bartrup-Cook
Nov 20 '18 at 10:04





The code you posted doesn't insert any values into cells - only the copying/pasting way. My first block of code replicates that - copies the value from AB33 to Q38 and so on. Other than that how are values inserted?

– Darren Bartrup-Cook
Nov 20 '18 at 10:04













Hmm.. Im stil not quite sure what your first block does... Not really sure about the cell references, are they supposed to copy the same as mine?

– Valdemar E. Stage
Nov 20 '18 at 10:23





Hmm.. Im stil not quite sure what your first block does... Not really sure about the cell references, are they supposed to copy the same as mine?

– Valdemar E. Stage
Nov 20 '18 at 10:23













Yes, The For...Each line looks at each array of two addresses in turn and moves the values from one cell reference to the other. You could also replicate your code using Sheets("Input").Cells(33, 28) = Sheets("Output").Cells(38, 17) which replaces the first copy/paste block in your code.

– Darren Bartrup-Cook
Nov 20 '18 at 10:37





Yes, The For...Each line looks at each array of two addresses in turn and moves the values from one cell reference to the other. You could also replicate your code using Sheets("Input").Cells(33, 28) = Sheets("Output").Cells(38, 17) which replaces the first copy/paste block in your code.

– Darren Bartrup-Cook
Nov 20 '18 at 10:37













0














So I want it to be something like this:



    Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P40", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(40, 17).Value = Sheets("Output").Cells(40, 17).Value

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P41", "AB23"))
For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(41, 17).Value = Sheets("Output").Cells(41, 17).Value


Does this makes sense?



So repeating it 10x down in rows, and then move 1 column left and do it again.



Hope you get my point, and thanks :)



/Valdemar






share|improve this answer
























  • So, the next "step" here is to make this a "loop" so I wont have to repeat the code 10 times, changing P41 -> P42 and so forth; and next changing Q38 -> R38 (and fx Cells(41, 18)

    – Valdemar E. Stage
    Nov 20 '18 at 14:55






  • 1





    Please don't post questions as answers. Post a new question if you are moving beyond the scope of your original.

    – SJR
    Nov 27 '18 at 14:35
















0














So I want it to be something like this:



    Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P40", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(40, 17).Value = Sheets("Output").Cells(40, 17).Value

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P41", "AB23"))
For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(41, 17).Value = Sheets("Output").Cells(41, 17).Value


Does this makes sense?



So repeating it 10x down in rows, and then move 1 column left and do it again.



Hope you get my point, and thanks :)



/Valdemar






share|improve this answer
























  • So, the next "step" here is to make this a "loop" so I wont have to repeat the code 10 times, changing P41 -> P42 and so forth; and next changing Q38 -> R38 (and fx Cells(41, 18)

    – Valdemar E. Stage
    Nov 20 '18 at 14:55






  • 1





    Please don't post questions as answers. Post a new question if you are moving beyond the scope of your original.

    – SJR
    Nov 27 '18 at 14:35














0












0








0







So I want it to be something like this:



    Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P40", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(40, 17).Value = Sheets("Output").Cells(40, 17).Value

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P41", "AB23"))
For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(41, 17).Value = Sheets("Output").Cells(41, 17).Value


Does this makes sense?



So repeating it 10x down in rows, and then move 1 column left and do it again.



Hope you get my point, and thanks :)



/Valdemar






share|improve this answer













So I want it to be something like this:



    Sub Test()

Dim vAddresses As Variant
Dim vRef As Variant

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P40", "AB23"))

For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(40, 17).Value = Sheets("Output").Cells(40, 17).Value

vAddresses = Array( _
Array("Q38", "AB33"), _
Array("P41", "AB23"))
For Each vRef In vAddresses
Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
Next vRef

Sheets("Output").Cells(41, 17).Value = Sheets("Output").Cells(41, 17).Value


Does this makes sense?



So repeating it 10x down in rows, and then move 1 column left and do it again.



Hope you get my point, and thanks :)



/Valdemar







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 14:42









Valdemar E. StageValdemar E. Stage

143




143













  • So, the next "step" here is to make this a "loop" so I wont have to repeat the code 10 times, changing P41 -> P42 and so forth; and next changing Q38 -> R38 (and fx Cells(41, 18)

    – Valdemar E. Stage
    Nov 20 '18 at 14:55






  • 1





    Please don't post questions as answers. Post a new question if you are moving beyond the scope of your original.

    – SJR
    Nov 27 '18 at 14:35



















  • So, the next "step" here is to make this a "loop" so I wont have to repeat the code 10 times, changing P41 -> P42 and so forth; and next changing Q38 -> R38 (and fx Cells(41, 18)

    – Valdemar E. Stage
    Nov 20 '18 at 14:55






  • 1





    Please don't post questions as answers. Post a new question if you are moving beyond the scope of your original.

    – SJR
    Nov 27 '18 at 14:35

















So, the next "step" here is to make this a "loop" so I wont have to repeat the code 10 times, changing P41 -> P42 and so forth; and next changing Q38 -> R38 (and fx Cells(41, 18)

– Valdemar E. Stage
Nov 20 '18 at 14:55





So, the next "step" here is to make this a "loop" so I wont have to repeat the code 10 times, changing P41 -> P42 and so forth; and next changing Q38 -> R38 (and fx Cells(41, 18)

– Valdemar E. Stage
Nov 20 '18 at 14:55




1




1





Please don't post questions as answers. Post a new question if you are moving beyond the scope of your original.

– SJR
Nov 27 '18 at 14:35





Please don't post questions as answers. Post a new question if you are moving beyond the scope of your original.

– SJR
Nov 27 '18 at 14:35


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335006%2fuse-relative-references%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly