Convert selected cells formula to value across Selected Sheets
I'm using this code below to convert formula to cells, which works fine in a single sheet. But the problem is when I need to convert all selected cells which are in different sheets to their value, this code doesn't do it.
This is how I am selecting the cells in Excel:
first I select the cells in one sheet, than I go down to the tabs right-click and select specific sheets, which in Excel selects the corresponding cells in every selected sheet.
So any tips on how I can change this code to make it work across different sheets?
Sub formulaToValues()
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each cel In Selection.Cells
cel.Value = cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
End If
End Sub
excel vba selection
add a comment |
I'm using this code below to convert formula to cells, which works fine in a single sheet. But the problem is when I need to convert all selected cells which are in different sheets to their value, this code doesn't do it.
This is how I am selecting the cells in Excel:
first I select the cells in one sheet, than I go down to the tabs right-click and select specific sheets, which in Excel selects the corresponding cells in every selected sheet.
So any tips on how I can change this code to make it work across different sheets?
Sub formulaToValues()
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each cel In Selection.Cells
cel.Value = cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
End If
End Sub
excel vba selection
Are the cells always in the same location on each sheet?
– K.Dᴀᴠɪs
Nov 20 '18 at 7:03
Does every worksheet in the workbook need to be formatted or only those selected?
– rohrl77
Nov 20 '18 at 7:17
@K.Dᴀᴠɪs yes, they are always in the same location
– CKCCN
Nov 20 '18 at 7:42
@rohrl77 I would prefer only those selected
– CKCCN
Nov 20 '18 at 7:43
Thanks for clarifying that you wanted selected sheets - I updated your question and my answer.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:49
add a comment |
I'm using this code below to convert formula to cells, which works fine in a single sheet. But the problem is when I need to convert all selected cells which are in different sheets to their value, this code doesn't do it.
This is how I am selecting the cells in Excel:
first I select the cells in one sheet, than I go down to the tabs right-click and select specific sheets, which in Excel selects the corresponding cells in every selected sheet.
So any tips on how I can change this code to make it work across different sheets?
Sub formulaToValues()
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each cel In Selection.Cells
cel.Value = cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
End If
End Sub
excel vba selection
I'm using this code below to convert formula to cells, which works fine in a single sheet. But the problem is when I need to convert all selected cells which are in different sheets to their value, this code doesn't do it.
This is how I am selecting the cells in Excel:
first I select the cells in one sheet, than I go down to the tabs right-click and select specific sheets, which in Excel selects the corresponding cells in every selected sheet.
So any tips on how I can change this code to make it work across different sheets?
Sub formulaToValues()
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each cel In Selection.Cells
cel.Value = cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
End If
End Sub
excel vba selection
excel vba selection
edited Nov 20 '18 at 7:48


K.Dᴀᴠɪs
7,250112439
7,250112439
asked Nov 20 '18 at 6:41
CKCCNCKCCN
162
162
Are the cells always in the same location on each sheet?
– K.Dᴀᴠɪs
Nov 20 '18 at 7:03
Does every worksheet in the workbook need to be formatted or only those selected?
– rohrl77
Nov 20 '18 at 7:17
@K.Dᴀᴠɪs yes, they are always in the same location
– CKCCN
Nov 20 '18 at 7:42
@rohrl77 I would prefer only those selected
– CKCCN
Nov 20 '18 at 7:43
Thanks for clarifying that you wanted selected sheets - I updated your question and my answer.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:49
add a comment |
Are the cells always in the same location on each sheet?
– K.Dᴀᴠɪs
Nov 20 '18 at 7:03
Does every worksheet in the workbook need to be formatted or only those selected?
– rohrl77
Nov 20 '18 at 7:17
@K.Dᴀᴠɪs yes, they are always in the same location
– CKCCN
Nov 20 '18 at 7:42
@rohrl77 I would prefer only those selected
– CKCCN
Nov 20 '18 at 7:43
Thanks for clarifying that you wanted selected sheets - I updated your question and my answer.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:49
Are the cells always in the same location on each sheet?
– K.Dᴀᴠɪs
Nov 20 '18 at 7:03
Are the cells always in the same location on each sheet?
– K.Dᴀᴠɪs
Nov 20 '18 at 7:03
Does every worksheet in the workbook need to be formatted or only those selected?
– rohrl77
Nov 20 '18 at 7:17
Does every worksheet in the workbook need to be formatted or only those selected?
– rohrl77
Nov 20 '18 at 7:17
@K.Dᴀᴠɪs yes, they are always in the same location
– CKCCN
Nov 20 '18 at 7:42
@K.Dᴀᴠɪs yes, they are always in the same location
– CKCCN
Nov 20 '18 at 7:42
@rohrl77 I would prefer only those selected
– CKCCN
Nov 20 '18 at 7:43
@rohrl77 I would prefer only those selected
– CKCCN
Nov 20 '18 at 7:43
Thanks for clarifying that you wanted selected sheets - I updated your question and my answer.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:49
Thanks for clarifying that you wanted selected sheets - I updated your question and my answer.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:49
add a comment |
3 Answers
3
active
oldest
votes
You should be able to just grab the address of the selection, then add that to each worksheet's range
Sub formulaToValues()
Dim celAddr As String
celAddr = Selection.Address
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
With ws.Range(celAddr)
.Value = .Value
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
Next ws
End Sub
1
.value = .value
is not required?
– shrivallabha.redij
Nov 20 '18 at 7:09
No, it would be the same thing as sayingApple = Apple
- only because the origination range is the same as the destination range..---- Scratch that, I see that he was using a formula - thanks for pointing that out, edit coming soon.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:10
@K.Dᴀᴠɪs If there are sheets in the workbook that don't need to be changed, then this doesn't work. Neither does my answer for that matter.
– rohrl77
Nov 20 '18 at 7:18
@rohrl77 The question does state "and select all sheets, which in excel selects the corresponding cells in every sheet", so I think we are both correct. And the title: "... across all sheets"
– K.Dᴀᴠɪs
Nov 20 '18 at 7:19
1
@K.Dᴀᴠɪs OH! Ok. I updated my answer to take only those that are selected... which was interesting to learn. I'll just include both versions.
– rohrl77
Nov 20 '18 at 7:24
|
show 4 more comments
You are attempting to write to a 3D cell collection. An interesting problem i haven't seen before. I gave it a shot.
The below code works for me. I have simply added an extra loop to search through any other sheets. Note: it is good practice to always declare your variables.
Answer1: This cycles through every sheet in the workbook
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Worksheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
Answer2: With this one it only goes throug the selected sheets
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
I actually did try to loop through the workbook too but I am too newb and couldn't get it to work, guess some mistakes was there with mine. And in my case this did the job for me. Thanks for this.
– CKCCN
Nov 20 '18 at 8:28
Hi CKCCN, glad to help. It was an interesting problem to see. One that i had not thought about before.
– rohrl77
Nov 20 '18 at 8:54
add a comment |
Thanks alot guys, this got answered pretty quickly.
I am placing my macros in personal so I ended if with this
Sub formulaToValues3()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ActiveWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = ws.Range(cel.Address).Value 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
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/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
});
}
});
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%2f53387564%2fconvert-selected-cells-formula-to-value-across-selected-sheets%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should be able to just grab the address of the selection, then add that to each worksheet's range
Sub formulaToValues()
Dim celAddr As String
celAddr = Selection.Address
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
With ws.Range(celAddr)
.Value = .Value
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
Next ws
End Sub
1
.value = .value
is not required?
– shrivallabha.redij
Nov 20 '18 at 7:09
No, it would be the same thing as sayingApple = Apple
- only because the origination range is the same as the destination range..---- Scratch that, I see that he was using a formula - thanks for pointing that out, edit coming soon.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:10
@K.Dᴀᴠɪs If there are sheets in the workbook that don't need to be changed, then this doesn't work. Neither does my answer for that matter.
– rohrl77
Nov 20 '18 at 7:18
@rohrl77 The question does state "and select all sheets, which in excel selects the corresponding cells in every sheet", so I think we are both correct. And the title: "... across all sheets"
– K.Dᴀᴠɪs
Nov 20 '18 at 7:19
1
@K.Dᴀᴠɪs OH! Ok. I updated my answer to take only those that are selected... which was interesting to learn. I'll just include both versions.
– rohrl77
Nov 20 '18 at 7:24
|
show 4 more comments
You should be able to just grab the address of the selection, then add that to each worksheet's range
Sub formulaToValues()
Dim celAddr As String
celAddr = Selection.Address
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
With ws.Range(celAddr)
.Value = .Value
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
Next ws
End Sub
1
.value = .value
is not required?
– shrivallabha.redij
Nov 20 '18 at 7:09
No, it would be the same thing as sayingApple = Apple
- only because the origination range is the same as the destination range..---- Scratch that, I see that he was using a formula - thanks for pointing that out, edit coming soon.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:10
@K.Dᴀᴠɪs If there are sheets in the workbook that don't need to be changed, then this doesn't work. Neither does my answer for that matter.
– rohrl77
Nov 20 '18 at 7:18
@rohrl77 The question does state "and select all sheets, which in excel selects the corresponding cells in every sheet", so I think we are both correct. And the title: "... across all sheets"
– K.Dᴀᴠɪs
Nov 20 '18 at 7:19
1
@K.Dᴀᴠɪs OH! Ok. I updated my answer to take only those that are selected... which was interesting to learn. I'll just include both versions.
– rohrl77
Nov 20 '18 at 7:24
|
show 4 more comments
You should be able to just grab the address of the selection, then add that to each worksheet's range
Sub formulaToValues()
Dim celAddr As String
celAddr = Selection.Address
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
With ws.Range(celAddr)
.Value = .Value
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
Next ws
End Sub
You should be able to just grab the address of the selection, then add that to each worksheet's range
Sub formulaToValues()
Dim celAddr As String
celAddr = Selection.Address
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
With ws.Range(celAddr)
.Value = .Value
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
Next ws
End Sub
edited Nov 20 '18 at 7:47
answered Nov 20 '18 at 7:08


K.DᴀᴠɪsK.Dᴀᴠɪs
7,250112439
7,250112439
1
.value = .value
is not required?
– shrivallabha.redij
Nov 20 '18 at 7:09
No, it would be the same thing as sayingApple = Apple
- only because the origination range is the same as the destination range..---- Scratch that, I see that he was using a formula - thanks for pointing that out, edit coming soon.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:10
@K.Dᴀᴠɪs If there are sheets in the workbook that don't need to be changed, then this doesn't work. Neither does my answer for that matter.
– rohrl77
Nov 20 '18 at 7:18
@rohrl77 The question does state "and select all sheets, which in excel selects the corresponding cells in every sheet", so I think we are both correct. And the title: "... across all sheets"
– K.Dᴀᴠɪs
Nov 20 '18 at 7:19
1
@K.Dᴀᴠɪs OH! Ok. I updated my answer to take only those that are selected... which was interesting to learn. I'll just include both versions.
– rohrl77
Nov 20 '18 at 7:24
|
show 4 more comments
1
.value = .value
is not required?
– shrivallabha.redij
Nov 20 '18 at 7:09
No, it would be the same thing as sayingApple = Apple
- only because the origination range is the same as the destination range..---- Scratch that, I see that he was using a formula - thanks for pointing that out, edit coming soon.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:10
@K.Dᴀᴠɪs If there are sheets in the workbook that don't need to be changed, then this doesn't work. Neither does my answer for that matter.
– rohrl77
Nov 20 '18 at 7:18
@rohrl77 The question does state "and select all sheets, which in excel selects the corresponding cells in every sheet", so I think we are both correct. And the title: "... across all sheets"
– K.Dᴀᴠɪs
Nov 20 '18 at 7:19
1
@K.Dᴀᴠɪs OH! Ok. I updated my answer to take only those that are selected... which was interesting to learn. I'll just include both versions.
– rohrl77
Nov 20 '18 at 7:24
1
1
.value = .value
is not required?– shrivallabha.redij
Nov 20 '18 at 7:09
.value = .value
is not required?– shrivallabha.redij
Nov 20 '18 at 7:09
No, it would be the same thing as saying
Apple = Apple
- only because the origination range is the same as the destination range..---- Scratch that, I see that he was using a formula - thanks for pointing that out, edit coming soon.– K.Dᴀᴠɪs
Nov 20 '18 at 7:10
No, it would be the same thing as saying
Apple = Apple
- only because the origination range is the same as the destination range..---- Scratch that, I see that he was using a formula - thanks for pointing that out, edit coming soon.– K.Dᴀᴠɪs
Nov 20 '18 at 7:10
@K.Dᴀᴠɪs If there are sheets in the workbook that don't need to be changed, then this doesn't work. Neither does my answer for that matter.
– rohrl77
Nov 20 '18 at 7:18
@K.Dᴀᴠɪs If there are sheets in the workbook that don't need to be changed, then this doesn't work. Neither does my answer for that matter.
– rohrl77
Nov 20 '18 at 7:18
@rohrl77 The question does state "and select all sheets, which in excel selects the corresponding cells in every sheet", so I think we are both correct. And the title: "... across all sheets"
– K.Dᴀᴠɪs
Nov 20 '18 at 7:19
@rohrl77 The question does state "and select all sheets, which in excel selects the corresponding cells in every sheet", so I think we are both correct. And the title: "... across all sheets"
– K.Dᴀᴠɪs
Nov 20 '18 at 7:19
1
1
@K.Dᴀᴠɪs OH! Ok. I updated my answer to take only those that are selected... which was interesting to learn. I'll just include both versions.
– rohrl77
Nov 20 '18 at 7:24
@K.Dᴀᴠɪs OH! Ok. I updated my answer to take only those that are selected... which was interesting to learn. I'll just include both versions.
– rohrl77
Nov 20 '18 at 7:24
|
show 4 more comments
You are attempting to write to a 3D cell collection. An interesting problem i haven't seen before. I gave it a shot.
The below code works for me. I have simply added an extra loop to search through any other sheets. Note: it is good practice to always declare your variables.
Answer1: This cycles through every sheet in the workbook
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Worksheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
Answer2: With this one it only goes throug the selected sheets
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
I actually did try to loop through the workbook too but I am too newb and couldn't get it to work, guess some mistakes was there with mine. And in my case this did the job for me. Thanks for this.
– CKCCN
Nov 20 '18 at 8:28
Hi CKCCN, glad to help. It was an interesting problem to see. One that i had not thought about before.
– rohrl77
Nov 20 '18 at 8:54
add a comment |
You are attempting to write to a 3D cell collection. An interesting problem i haven't seen before. I gave it a shot.
The below code works for me. I have simply added an extra loop to search through any other sheets. Note: it is good practice to always declare your variables.
Answer1: This cycles through every sheet in the workbook
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Worksheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
Answer2: With this one it only goes throug the selected sheets
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
I actually did try to loop through the workbook too but I am too newb and couldn't get it to work, guess some mistakes was there with mine. And in my case this did the job for me. Thanks for this.
– CKCCN
Nov 20 '18 at 8:28
Hi CKCCN, glad to help. It was an interesting problem to see. One that i had not thought about before.
– rohrl77
Nov 20 '18 at 8:54
add a comment |
You are attempting to write to a 3D cell collection. An interesting problem i haven't seen before. I gave it a shot.
The below code works for me. I have simply added an extra loop to search through any other sheets. Note: it is good practice to always declare your variables.
Answer1: This cycles through every sheet in the workbook
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Worksheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
Answer2: With this one it only goes throug the selected sheets
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
You are attempting to write to a 3D cell collection. An interesting problem i haven't seen before. I gave it a shot.
The below code works for me. I have simply added an extra loop to search through any other sheets. Note: it is good practice to always declare your variables.
Answer1: This cycles through every sheet in the workbook
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Worksheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
Answer2: With this one it only goes throug the selected sheets
Sub formulaToValues()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ThisWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = 2 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
edited Nov 20 '18 at 7:22
answered Nov 20 '18 at 7:07
rohrl77rohrl77
1,70742750
1,70742750
I actually did try to loop through the workbook too but I am too newb and couldn't get it to work, guess some mistakes was there with mine. And in my case this did the job for me. Thanks for this.
– CKCCN
Nov 20 '18 at 8:28
Hi CKCCN, glad to help. It was an interesting problem to see. One that i had not thought about before.
– rohrl77
Nov 20 '18 at 8:54
add a comment |
I actually did try to loop through the workbook too but I am too newb and couldn't get it to work, guess some mistakes was there with mine. And in my case this did the job for me. Thanks for this.
– CKCCN
Nov 20 '18 at 8:28
Hi CKCCN, glad to help. It was an interesting problem to see. One that i had not thought about before.
– rohrl77
Nov 20 '18 at 8:54
I actually did try to loop through the workbook too but I am too newb and couldn't get it to work, guess some mistakes was there with mine. And in my case this did the job for me. Thanks for this.
– CKCCN
Nov 20 '18 at 8:28
I actually did try to loop through the workbook too but I am too newb and couldn't get it to work, guess some mistakes was there with mine. And in my case this did the job for me. Thanks for this.
– CKCCN
Nov 20 '18 at 8:28
Hi CKCCN, glad to help. It was an interesting problem to see. One that i had not thought about before.
– rohrl77
Nov 20 '18 at 8:54
Hi CKCCN, glad to help. It was an interesting problem to see. One that i had not thought about before.
– rohrl77
Nov 20 '18 at 8:54
add a comment |
Thanks alot guys, this got answered pretty quickly.
I am placing my macros in personal so I ended if with this
Sub formulaToValues3()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ActiveWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = ws.Range(cel.Address).Value 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
add a comment |
Thanks alot guys, this got answered pretty quickly.
I am placing my macros in personal so I ended if with this
Sub formulaToValues3()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ActiveWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = ws.Range(cel.Address).Value 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
add a comment |
Thanks alot guys, this got answered pretty quickly.
I am placing my macros in personal so I ended if with this
Sub formulaToValues3()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ActiveWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = ws.Range(cel.Address).Value 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
Thanks alot guys, this got answered pretty quickly.
I am placing my macros in personal so I ended if with this
Sub formulaToValues3()
Dim cel As Range
Dim ws As Worksheet
If Selection.Cells.Count = 1 Then
Selection.Value = Selection.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Else
For Each ws In ActiveWorkbook.Windows(1).SelectedSheets
For Each cel In Selection.Cells
ws.Range(cel.Address).Value = ws.Range(cel.Address).Value 'cel.Value
Selection.Cells.Interior.ColorIndex = 0
Selection.Cells.Font.Color = vbBlack
Next cel
Next ws
End If
End Sub
answered Nov 20 '18 at 8:32
CKCCNCKCCN
162
162
add a comment |
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%2f53387564%2fconvert-selected-cells-formula-to-value-across-selected-sheets%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
Are the cells always in the same location on each sheet?
– K.Dᴀᴠɪs
Nov 20 '18 at 7:03
Does every worksheet in the workbook need to be formatted or only those selected?
– rohrl77
Nov 20 '18 at 7:17
@K.Dᴀᴠɪs yes, they are always in the same location
– CKCCN
Nov 20 '18 at 7:42
@rohrl77 I would prefer only those selected
– CKCCN
Nov 20 '18 at 7:43
Thanks for clarifying that you wanted selected sheets - I updated your question and my answer.
– K.Dᴀᴠɪs
Nov 20 '18 at 7:49