RobertEnglish
New Member
- Joined
- Nov 25, 2013
- Messages
- 8
I've been putting together a macro that reads in cells from one specific row on <CODE>Sheet1</CODE> and places them onto a ComboBox on a <CODE>Sheet2</CODE>
<CODE>|cellA | cellB | cellC | cellD | ... |cellZ | </CODE>
</PRE>Once loaded to the ComboBox, whatever cell is selected there has it's column copied to a column on <CODE>Sheet2</CODE>.
So far I have been able to do this manually but I need it do automatically update from any changes made to the row and when a selection is made in the ComboBox.
Here's my code so far:
Option Explicit
Sub ComboBox1_Change_Open()
Dim cmbx As ComboBox
Dim myRange As Range
Dim i As Integer
Dim c As Range
Set cmbx = Sheet7.ComboBox1
cmbx.Clear
Set myRange = ActiveWorkbook.Sheets("1. Process information").Range("C4:Z4")
For Each c In myRange
If c.Value <> "" Then
cmbx.AddItem c.Value
cmbx = cmbx.Column(0, 0)
End If
Next
For i = 0 To cmbx.ListCount
If cmbx.ListIndex = i Then
With Sheets("1. Process information")
.Range(.Cells(4, i + 4), .Cells(Rows.Count, i + 4).End(xlUp)).Copy
End With
ActiveWorkbook.Sheets("Shipsheet").AutoFill.Paste Destination:=Sheets("Shipsheet").Range("B4")
End If
Next i
End Sub
Just realised this code only works when <CODE>ListIndex</CODE> is 0 and that <CODE>ListIndex</CODE> is not the right function to use. This had worked for the first item in the ComboBox.
(I'm using Microsoft Excel 2007)
Any help is appreciated. Thanks in advance
<CODE>|cellA | cellB | cellC | cellD | ... |cellZ | </CODE>
</PRE>Once loaded to the ComboBox, whatever cell is selected there has it's column copied to a column on <CODE>Sheet2</CODE>.
So far I have been able to do this manually but I need it do automatically update from any changes made to the row and when a selection is made in the ComboBox.
Here's my code so far:
Option Explicit
Sub ComboBox1_Change_Open()
Dim cmbx As ComboBox
Dim myRange As Range
Dim i As Integer
Dim c As Range
Set cmbx = Sheet7.ComboBox1
cmbx.Clear
Set myRange = ActiveWorkbook.Sheets("1. Process information").Range("C4:Z4")
For Each c In myRange
If c.Value <> "" Then
cmbx.AddItem c.Value
cmbx = cmbx.Column(0, 0)
End If
Next
For i = 0 To cmbx.ListCount
If cmbx.ListIndex = i Then
With Sheets("1. Process information")
.Range(.Cells(4, i + 4), .Cells(Rows.Count, i + 4).End(xlUp)).Copy
End With
ActiveWorkbook.Sheets("Shipsheet").AutoFill.Paste Destination:=Sheets("Shipsheet").Range("B4")
End If
Next i
End Sub
Just realised this code only works when <CODE>ListIndex</CODE> is 0 and that <CODE>ListIndex</CODE> is not the right function to use. This had worked for the first item in the ComboBox.
(I'm using Microsoft Excel 2007)
Any help is appreciated. Thanks in advance
Last edited: