Reputation: 47
Right now I've created a code to copy values from one range to another range based on the value from another sheet (the copy and paste happens on one sheet).
But because this value can be one of twelve values, the range that is being copied and pasted becomes smaller.
Because I'm not adept at VBA I created dozens of copy ranges and dozens of paste ranges in Excel to handle ElseIf statements via VBA to copy and paste depending on what the cell value is in the other sheet.
I'm curious, is there a way to make my code more optimized and have less named ranges in my workbook?
Any help would be appreciated, here's my code pasted below (each named range for both the copy and paste is simply one less column due to what the selections can be in the first sheet):
SubTest()
If ws0.Range("D6") = "BUD" Then
ws1.Range("CopyFormulasFT").Select
Selection.Copy
ws1.Range("PasteFormulasFT").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F01" Then
ws1.Range("CopyFormulasFTOneEleven").Select
Selection.Copy
ws1.Range("PasteFormulasFTOneEleven").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F02" Then
ws1.Range("CopyFormulasFTTwoTen").Select
Selection.Copy
ws1.Range("PasteFormulasFTTwoTen").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F03" Then
ws1.Range("CopyFormulasFTThreeNine").Select
Selection.Copy
ws1.Range("PasteFormulasFTThreeNine").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F04" Then
ws1.Range("CopyFormulasFTFourEight").Select
Selection.Copy
ws1.Range("PasteFormulasFTFourEight").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F05" Then
ws1.Range("CopyFormulasFTFiveSeven").Select
Selection.Copy
ws1.Range("PasteFormulasFTFiveSeven").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F06" Then
ws1.Range("CopyFormulasFTSixSix").Select
Selection.Copy
ws1.Range("PasteFormulasFTSixSix").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F07" Then
ws1.Range("CopyFormulasFTSevenFive").Select
Selection.Copy
ws1.Range("PasteFormulasFTSevenFive").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F08" Then
ws1.Range("CopyFormulasFTEightFour").Select
Selection.Copy
ws1.Range("PasteFormulasFTEightFour").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F09" Then
ws1.Range("CopyFormulasFTNineThree").Select
Selection.Copy
ws1.Range("PasteFormulasFTNineThree").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F10" Then
ws1.Range("CopyFormulasFTTenTwo").Select
Selection.Copy
ws1.Range("PasteFormulasFTTenTwo").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
ElseIf ws0.Range("D6") = "F11" Then
ws1.Range("CopyFormulasFTElevenOne").Select
Selection.Copy
ws1.Range("PasteFormulasFTElevenOne").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
End If
End Sub
Upvotes: 1
Views: 2019
Reputation: 1277
Using string manipulation and a loop you could greatly reduce the size of that code:
dim arrStrings(1 to 11) as string
arrStrings(1) = "OneEleven"
arrStrings(2) = "TwoTen"
arrStrings(2) = "ThreeNine"
...
arrStrings(11) = "NineThree"
dim i as integer
for i = 1 to 11
If ws0.Range("D6") = "F"+ strings.trim(str(i)) Then
ws1.Range("CopyFormulasFT" + arrStrings(i)).Select
Selection.Copy
ws1.Range("PasteFormulasFT" + arrStrigns(i)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
end if
next i
if the actual code is something like this
"oneone", "onetwo", "onethree", ..., "oneeleven", "twoone", "twotwo", "twothree", ... "twoeleven" ...
(11x11 strings) you could use a double loop over this array:
dim arrStrings(1 to 11) as string
arrStrings(1) = "One"
arrStrings(2) = "Two"
arrStrings(2) = "Three"
...
arrStrings(11) = "Nine"
and you can create the string like this Str = "CopyFormulasFT"+ arrstrings(i) + arrstrings(j)
Upvotes: 3
Reputation: 14179
Another approach, this one much more flexible and easier to update:
Sub CondCopy()
Dim ws0 As Worksheet, ws1 As Worksheet
Dim str0 As String, str1 As String, str2 As String
Dim strCond As String, ArrLoc As Long
Dim strCopy As String, strPaste As String, strNum As String
With ThisWorkbook
Set ws0 = .Sheets("Sheet1")
Set ws1 = .Sheets("Sheet2")
End With
str0 = ";One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten;Eleven"
str1 = ";Eleven;Ten;Nine;Eight;Seven;Six;Five;Four;Three;Two;One"
str2 = "BUD;F01;F02;F03;F04;F05;F06;F07;F08;F09;F10;F11"
strCond = ws0.Range("D6").Value
ArrLoc = Application.Match(strCond, Split(str2, ";"), 0) - 1
strNum = Split(str0, ";")(ArrLoc) & Split(str1, ";")(ArrLoc)
strCopy = "CopyFormulasFT" & strNum
strPaste = "PasteFormulasFT" & strNum
With ws1
.Range(strCopy).Copy
.Range(strPaste).PasteSpecial xlPasteValues, SkipBlanks:=True
End With
End Sub
In the case that you need to add more named ranges following your pattern, just editing str0
, str1
, and str2
is enough.
Let us know if this helps.
Upvotes: 2
Reputation: 35863
is there a way to make my code more optimized and have less named ranges in my workbook?
depends on how your data organized. But now, you can slightly simplify your code:
Sub Test()
Dim destRng As String
Dim sorceRng As String
Select Case ws0.Range("D6")
Case "BUD"
sorceRng = "CopyFormulasFT": destRng = "PasteFormulasFT"
Case "F01"
sorceRng = "CopyFormulasFTOneEleven": destRng = "PasteFormulasFTOneEleven"
Case "F02"
sorceRng = "CopyFormulasFTTwoTen": destRng = "PasteFormulasFTTwoTen"
Case "F03"
sorceRng = "CopyFormulasFTThreeNine": destRng = "PasteFormulasFTThreeNine"
Case "F04"
sorceRng = "CopyFormulasFTFourEight": destRng = "PasteFormulasFTFourEight"
Case "F05"
sorceRng = "CopyFormulasFTFiveSeven": destRng = "PasteFormulasFTFiveSeven"
Case "F06"
sorceRng = "CopyFormulasFTSixSix": destRng = "PasteFormulasFTSixSix"
Case "F07"
sorceRng = "CopyFormulasFTSevenFive": destRng = "PasteFormulasFTSevenFive"
Case "F08"
sorceRng = "CopyFormulasFTEightFour": destRng = "PasteFormulasFTEightFour"
Case "F09"
sorceRng = "CopyFormulasFTNineThree": destRng = "PasteFormulasFTNineThree"
Case "F10"
sorceRng = "CopyFormulasFTTenTwo": destRng = "PasteFormulasFTTenTwo"
Case "F11"
sorceRng = "CopyFormulasFTElevenOne": destRng = "PasteFormulasFTElevenOne"
Case Else
Exit Sub
End Select
ws1.Range(sorceRng).Copy
ws1.Range(destRng).PasteSpecial Paste:=xlPasteValues, SkipBlanks:=True
End Sub
Upvotes: 2