VBA Select case complication

Daktari

Board Regular
Joined
May 20, 2012
Messages
56
I'm trying to shoehorn one macro into another using the select case method:
The green part of the first macro works, and prompted me to expand the macro. The red part of the macro only works on some sheets. I kept the green part in case there is something important there.

The red part gets the input from the second macro, which also works (or good enough ;) )

  • Could a kind soul please point out where I messed up with the red part?

Code:
Sub importerTotalsummer()
[COLOR="Green"]
    WB1 = ActiveWorkbook.Name

    Workbooks.Open Filename:= _
        "C:\Users\David\Documents\korrekt volum - Kopi.xls", _
        UpdateLinks:=0
  
    WB2 = ActiveWorkbook.Name
  
    WBname = "[" & WB2 & "]"

'Return to Master workbook
    Workbooks(WB1).Activate


    ActiveWorkbook.Unprotect


    For i = 1 To 5
       
        Select Case i
            Case 1: wsname = "alfa"
            Case 2: wsname = "beta"
            Case 3: wsname = "charlie"
            Case 4: wsname = "delta"
            Case 5: wsname = "echo"
        End Select

      
            On Error Resume Next
            If Worksheets(wsname).Name = "" Then
                Sheets.Add(After:=Sheets(Sheets.Count)).Name = wsname
            End If

      
            Workbooks(WB1).Sheets(wsname).Cells.ClearContents
  
            Workbooks(WB2).Sheets(wsname).Range("A1:af404").Copy _
            Workbooks(WB1).Sheets(wsname).Range("A1")
    
            Cells.Replace what:=WBname, replacement:=""
    Next
    

    Workbooks(WB2).Saved = True
    Workbooks(WB2).Close[/COLOR]


[COLOR="Red"]Workbooks(WB1).Activate

    For i = 1 To 8
      
        Select Case i
            Case 1: wsname = "foxtrot"
            Case 2: wsname = "golf"
            Case 3: wsname = "hotel"
            Case 4: wsname = "india"
            Case 5: wsname = "juliet"
            Case 6: wsname = "kilo"
            Case 7: wsname = "lima"
            Case 8: wsname = "mike"
            
        End Select

                    
            Workbooks(WB1).Sheets(wsname).Range("c4:c260").Select
            Selection.Copy
            Workbooks(WB1).Sheets(wsname).Range("e4").End(xlToRight).Offset(0, 1).Activate
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
           
    Next

End Sub[/COLOR]

Code:
Sub Kopiertest()
    Range("c4:c260").Select
    Selection.Copy
    Range("e4").End(xlToRight).Offset(0, 1).Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I'm doing a shameless bump due to the down time.
(I had some more trials and errors in between).
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,955
Members
449,199
Latest member
Riley Johnson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top