我正在尝试将列从一个工作表复制到另一个工作簿。这是我的复制代码:
Private Sub CommandButton2_Click()
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = wb.Worksheets(cmb).Columns(Form.ComboBox2.Value)
Set targetColumn = ActiveWorkbook.ActiveSheet.Columns("PART NUMBER")
sourceColumn.Copy Destination:=targetColumn
End Sub
cmb
是一个全局变量。我在Run Time Error 13: Type Mismatch
Set sourceColumn = wb.Worksheets(cmb).Columns(Form.ComboBox2.Value)
有人可以指出最新情况以及如何解决这个问题?
整个代码:
Public wb As Workbook
Public cmb As String
Private Sub ComboBox1_Change()
Dim Cell As Range, rng As Range, sht As Worksheet
cmb = Form.ComboBox1.Value
Set sht = wb.Worksheets(cmb)
'assuming your headers are always on the first row...
Set rng = sht.Range(sht.Range("A1"), _
sht.Cells(1, Columns.Count).End(xlToLeft))
'add some code here to clear the lists first!...
For Each Cell In rng.Cells
If Len(Cell.Value) > 0 Then
Form.ComboBox2.AddItem (Cell.Value)
Form.ComboBox3.AddItem (Cell.Value)
Form.ComboBox4.AddItem (Cell.Value)
Form.ComboBox5.AddItem (Cell.Value)
Form.ComboBox6.AddItem (Cell.Value)
Form.ComboBox7.AddItem (Cell.Value)
Form.ComboBox8.AddItem (Cell.Value)
Form.ComboBox9.AddItem (Cell.Value)
Form.ComboBox10.AddItem (Cell.Value)
Form.ComboBox11.AddItem (Cell.Value)
Form.ComboBox12.AddItem (Cell.Value)
Form.ComboBox13.AddItem (Cell.Value)
End If
Next Cell
End Sub
Private Sub CommandButton1_Click()
Dim sFilePath As String
sFilePath = Application.GetOpenFilename()
Set wb = Workbooks.Open(sFilePath)
For Each sht In wb.Worksheets
Form.ComboBox1.AddItem sht.Name
Next sht
End Sub
Private Sub CommandButton2_Click()
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = wb.Worksheets(cmb).Columns(Form.ComboBox2.Value)
Set targetColumn = ActiveWorkbook.ActiveSheet.Columns("PART NUMBER")
sourceColumn.Copy Destination:=targetColumn
End Sub
答案 0 :(得分:0)
可能您没有将正确的值作为Columns()中的参数传递。如果使用宏录制器,您将看到Columns的参数如下所示:
"E:G"
在一个简单的宏Columns("E:G").Select
中。或者数字为Columns(5).Select
。
尝试一个非常简单的修复,将带有错误的行更改为以下内容:
Set sourceColumn = wb.Worksheets(cmb).Columns("A:D")
如果有效,请尝试重新制作代码。