type mismatch

k_babb

Board Regular
Joined
Mar 28, 2011
Messages
71
hi i need to copy columns from sheet1 to sheet2 in a certain order i have this macro it works fine with 4 or 5 entry but i need to enter this amount of entrys and i get an error runtime error 13 type mismatch how can i resolve this problem

A,z,B,z,C,z,z,D,z,z,z,z,z,z,z,z,z,z,z,z,E,F,z,z,G,z,z,z,z,z,z,H,z,z,z,z,z,z,z,z,z,z,z,z,

z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,j,z




Code:
Sub copypaste()
Dim s As String, i As Long, X

s = InputBox("Enter From column letters separated by comma, e.g. A,C,AA")
X = Split(s, ",")
For i = LBound(X) To UBound(X)
    Sheets("Sheet1").Columns(X(i)).Copy Destination:=Sheets("Sheet2").Cells(1, i + 1)
Next i
deleteheader
End Sub

incase anyone can improve my code z is a blank column that i am using to space the columns so they are in the correct placement
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
this one

Sheets("Sheet1").Columns(X(i)).Copy Destination:=Sheets("Sheet2").Cells(1, i + 1)</pre>
 
Upvote 0
I cannot immediately see why that would fail.Please post the complete code.
 
Upvote 0
Code:
Sub copypaste()
Dim s As String, i As Long, X

s = InputBox("Enter From column letters separated by comma, e.g. A,C,AA")
X = Split(s, ",")
For i = LBound(X) To UBound(X)
    Sheets("Sheet1").Columns(X(i)).Copy Destination:=Sheets("Sheet2").Cells(1, i + 1)
Next i
deleteheader
End Sub

Sub deleteheader()
Sheets("Sheet2").Select
Rows("1:1").Select
Selection.Delete Shift:=xlUp
part
End Sub

Sub part()
Static wb2path As String: wb2path = Application.GetOpenFilename("Excel files, *.xls*")
If wb2path = "False" Then Exit Sub

Static wb1 As Workbook: Set wb1 = ActiveWorkbook
Static wb2 As Workbook: Set wb2 = Workbooks.Open(wb2path)






wb2.Activate
Sheets("sheet1").Select
Range("A2:bZ30000").Select
    Selection.ClearContents
    Range("A1").Select

wb1.Sheets("Sheet2").UsedRange.Copy Destination:=wb2.Sheets("Sheet1").Cells(2, 1)
wb1.Activate
Sheets("sheet2").Select
Cells.Select
Selection.ClearContents
Range("A1").Select

wb2.Save
wb2.Close
wb1.Activate
Sheets("sheet1").Select



End Sub
 
Upvote 0
Your code works fine. The error probably results from presence of spaces or additional comas in input.

Try this (note that code below do not solve problem with additional comas):

Code:
Sub copypaste()
Dim s As String, i As Long, X

s = InputBox("Enter From column letters separated by comma, e.g. A,C,AA")

i = 0

Do While i <= VBA.Len(s)
    i = i + 1
    If VBA.Mid(s, i, 1) = " " Then
        s = Left(s, i - 1) & Right(s, VBA.Len(s) - i)
        i = i - 1
    End If
Loop
s = Trim(s)

X = Split(s, ",")
For i = LBound(X) To UBound(X)
    Sheets("Sheet1").Columns(X(i)).Copy Destination:=Sheets("Sheet2").Cells(1, i + 1)
Next i
End Sub

Hope this helps :)
 
Upvote 0
additional code for input problem with comas:
Code:
 Sub copypaste()
Dim s As String, i As Long, X

s = InputBox("Enter From column letters separated by comma, e.g. A,C,AA")
If s = vbNullString Then Exit Sub

i = 0

s = Trim(s)
Do While i <= Len(s)
    i = i + 1
    If Mid(s, i, 1) = " " Then
        s = Left(s, i - 1) & Right(s, Len(s) - i)
        i = i - 1
    End If
Loop
i=0
Do While i <= Len(s)
    i = i + 1
    If Mid(s, i, 1) = "," And Mid(s, i + 1, 1) = "," Then
        s = Left(s, i - 1) & Right(s, Len(s) - i)
        i = i - 1
    End If
Loop

If Mid(s, Len(s), 1) = "," Then s = Mid(s, 1, Len(s) - 1)
If Mid(s, 1, 1) = "," Then s = Mid(s, 2, Len(s) - 1)

X = Split(s, ",")
For i = LBound(X) To UBound(X)
    Sheets("Sheet1").Columns(X(i)).Copy Destination:=Sheets("Sheet2").Cells(1, i + 1)
Next i

deleteheader
End Sub
 
Last edited:
Upvote 0
I just tested this and it worked with no error

Code:
Sub copypaste()
Dim s As String, i As Long, X

s = InputBox("Enter From column letters separated by comma, e.g. A,C,AA")
X = Split(s, ",")
For i = LBound(X) To UBound(X)
    Sheets("Sheet1").Columns(X(i)).Copy Destination:=Sheets("Sheet2").Cells(1, i + 1)
Next i
End Sub
 
Upvote 0
Actually, you will get an error if you enter A, B instead of A,B. Try

Rich (BB code):
Sub copypaste()
Dim s As String, i As Long, X

s = InputBox("Enter From column letters separated by comma, e.g. A,C,AA")
X = Split(s, ",")
For i = LBound(X) To UBound(X)
    Sheets("Sheet1").Columns(Trim(X(i))).Copy Destination:=Sheets("Sheet2").Cells(1, i + 1)
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,605
Messages
6,179,860
Members
452,948
Latest member
UsmanAli786

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